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 Altissimus · May 29, 2019 at 07:51 PM · rigidbodyaddforcemoveposition

Moving Rigidbody in fixed update

Hi,

So RB should be moved in FixedUpdate. Is this the case whether it is moved by AddForce, or MovePosition, or both?

My RB is following a NavMesh waypoint path. What I'm trying to do is move my RB a shunt to its right or left on press of a key:

 public float shuntTimer, shuntForce;
 
 void Update()
     {
         if (Input.GetKeyDown(KeyCode.P))
         {
             if (shuntTimer>0)  // trigger in Update by virtue of the timer != 0
         {
             // apply a force with magnitude shuntForce; cease the force when the timer expires
             rigidbody.AddForce(transform.right*shuntForce*shuntTimer);  
             // reduce the timer and also the duration of the force
             shuntTimer -= Time.deltaTime;
             }
         }

So this doesn't work. Not sure why not.

 Vector3 velocity;
 
 void Update()
 {
         if (Input.GetKeyDown(KeyCode.P))
        {
            velocity = Vector3.Lerp(velocity, transform.right*shuntFactor, Time.deltaTime);
        }
 }
 void FixedUpdate()
 {
 rigidbody.MovePosition(rigidbody.position + velocity * Time.deltaTime)
 }


And this doesn't either. Again, not quite sure why not.

When I say "doesn't work", neither of these approaches moves the RB at all.

Any help please?

Comment
Add comment · Show 4
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 Hellium · May 30, 2019 at 08:49 AM 0
Share
  1. Is the script attached to an active gameObject?

  2. Are the values of shuntForce and shuntTimer big enough?

  3. Have you tried to call a simple Debug.Log inside the various functions (`Update` and FixedUpdate) and conditions (`if`) to see whether the functions are called and if velocity or the force you apply are not too close to 0 ?

avatar image Altissimus Hellium · May 30, 2019 at 08:58 AM 0
Share

1) yes, and it has an RB. 2) yes, in testing I have set them to many orders of magnitude higher than need be 3) yes, the functions are being called.

Are you basically saying the code should work?

Is the fact that the object is a Nav$$anonymous$$esh object and is "patrolling" (setdestination) to waypoints interfering with the application of this code?

avatar image Hellium Altissimus · May 30, 2019 at 09:12 AM 0
Share

The two versions of your script work here, but I have a single cube with a Rigidbody, no Nav$$anonymous$$eshAgent, and I believe the latter is interfering with this script, indeed.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

Answer by cs120319992 · May 30, 2019 at 12:19 PM

 Vector3 velocity;
  
  void Update()
  {
          if (Input.GetKeyDown(KeyCode.P))
         {
             velocity = velocity == -1 ? 1 : -1;
         }
  }
  void FixedUpdate()
  {
  rigidbody.transform.position += Vector3.right * velocity * Time.deltaTime;
  }
Comment
Add comment · Show 5 · 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 Hellium · May 30, 2019 at 12:22 PM 0
Share

Did you take a look at the comments under the question?

avatar image cs120319992 Hellium · May 30, 2019 at 12:26 PM 0
Share

Answered to "When I say "doesn't work", neither of these approaches moves the RB at all."

avatar image Hellium cs120319992 · May 30, 2019 at 12:30 PM 0
Share

I have indicated 3 hours ago The two versions of your script work here, but I have a single cube with a Rigidbody

Show more comments
avatar image cs120319992 Hellium · May 30, 2019 at 12:28 PM 0
Share

rigidbody.AddForce(transform.right*shuntForce*shuntTimer, Force$$anonymous$$ode2D.Force or Impulse);

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

149 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is the best way to move the player with physics without using MovePosition? 0 Answers

Can an accelerometer be used for a RigidBody based movement? 0 Answers

AddForce vs MovePosition Rigidbody. 2 Answers

AddForce to position on Rigidbody 2 Answers

MovePosition not as precise as Translate with Kinematic rigidbodies (example unitypackage included) 1 Answer


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