Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Zlug · Dec 20, 2015 at 03:10 PM · addforcejumping

AddForce changes behavior based om location.

I'm trying to make an enemy jump toward its target to attack using AddForce. but just how much force is added seems to depend greatly on where in the scene the entities are (see video: https://www.youtube.com/watch?v=MBSGNyAit_4 ) and I can't figure out what I'm doing wrong.

     void Start(){
         ani = GetComponent<Animation>();
         agent = GetComponent<NavMeshAgent>();
         rig = GetComponent<Rigidbody>();
         state = states.idle;
     }
 
     private void Attack(){
         state = states.attack;
         agent.enabled = false;
         ani.Play("attack3");
         Vector3 force = transform.forward * 300f;
         force.y = 750f;
         rig.AddForce(force);
     }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Dec 20, 2015 at 05:18 PM 0
Share

in general you have to "add force" over a number of frames, perhaps in Update. perhaps read up on this, or read the 10000s of QA about it on here. sometimes "add explosive force" can help (just look in the manual)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by wibble82 · Dec 21, 2015 at 04:53 PM

Hi

I can envisage 2 possible situation here:

first, it is possible that you are calling 'Attack' every frame from within an 'Update' function. Because 'Update' is not called at a fixed rate with respect to the physics, this would mean you applied more force if the frame rate was higher! The simplest (and arguably most correct) way to solve this would be to call your AddForce from within FixedUpdate, as it would then be correctly called once per physics step.

 //adding a fixed amount of force (which internally is scaled
 //by fixed time step) every FixedUpdate
 void FixedUpdate()
 {
     rigidBody.AddForce(new Vector3(0,10,0);
 }
 

The other option is that your transform.forward is not in the xz plane. If that was the case, then the 'flatter' your transform.forward is, the more powerful your jump would be. This is because you override the y value, so only the xz bits are scaled. If we took it to the extremes, if your transform.forward was [0,0,1], you would get [0,750,300], but if your transform.forward was [0,1,0], you would get [0,750,0].

My guess is that its the first issue though. I suspect you're calling AddForce every frame from within Update. If you do want a continuous (multi frame force), you'll need to do it from within FixedUpdate. However if the bug is that you're calling it every frame, then you should just use ForceMode.Impulse, and can call it from wherever you like.

I'd recommend reading up on FixedUpdate vs Update, and the different types of force mode. Here's another similar question I answered a little while ago:

http://answers.unity3d.com/answers/1113442/view.html

-Chris

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by unnamed1334 · Dec 21, 2015 at 04:33 PM

You might be getting some strange behavior because you have Rigidbody and NavMeshAgent enabled at the same time.

http://docs.unity3d.com/Manual/nav-MixingComponents.html

It is likely the velocity of the object is not zero when you disable the NavMeshAgent and that is causing the strange results.

Depending on what you are trying to do, you could set the Rigidbody to kinematic and only set it to non-kinematic when you need it.

You could also set to velocity to zero before the applying the force so the jump is consistent.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with rigidbody jumping off of platform. 0 Answers

Jump higher when held button 1 Answer

Jump heights are different on different devices 0 Answers

How do i make jumps follow through with rigidbody2D.addforce? 1 Answer

Character Jumping 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges