• 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
2
Question by Spider_newgent · Oct 09, 2013 at 08:54 AM · movementphysicsaddforcefixedupdatefollow player

Moving an object using rigidbody.AddForce & keyboard input.

I'm making a simple top down shooter to get to grips with Unity and have $$anonymous$$t a snag.

When the player moves wit$$anonymous$$n range of an enemy, the enemy should start following the player. T$$anonymous$$s works using the following in FixedUpdate:

 function FixedUpdate()
 {
 
 if (player)
     {
          distance = Vector3.Distance( player.transform.position, transform.position);
         
         if ( distance < 1.5 && distance > 0.05 ) 
         {
             var delta = player.transform.position - transform.position;
             delta.Normalize();
     
             var speed = moveSpeed * Time.deltaTime;
             print("close");
           
             rigidbody.MovePosition(rigidbody.position + (delta * speed));
 
         }
     }
 
 }

However, It doesn't work if I use:

 rigidbody.AddForce(rigidbody.position + (delta * speed));

Furthermore, it doesn't even work if I use:

 rigidbody.AddForce(transform.forward * 10000,ForceMode.Impulse);

There's just no movement at all when using AddForce. It's probably somet$$anonymous$$ng really simple but I couldn't find an answer on the forums/online.

Has anyone encountered t$$anonymous$$s before?

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 Spider_newgent · Oct 09, 2013 at 09:53 AM 0
Share

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Tomer-Barkan · Oct 09, 2013 at 10:26 AM

Most likely your rigidbody is set to be kinematic - that means it's not affected by external forces, so AddForce() will not affect it. Make sure that the kinematic checkbox in the rigidbody is off. Also, AddForce() applies the force for the time of the FixedUpdate, so there's no need to multiply it by Time.deltaTime (and even if you did, you'd want to use Time.fixedDeltaTime when you use it in FixedUpdate()). Last, the vector you want to pass to AddForce, is in the direction of the force you want to apply, I'll assume forward.

So the code should be:

 rigidbody.AddForce(transform.forward * 100)

you can play with the number to get the accelleration you want. Keep in mind that t$$anonymous$$s

Comment
Add comment · Show 7 · 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 Spider_newgent · Oct 10, 2013 at 06:54 AM 0
Share
avatar image Tomer-Barkan · Oct 10, 2013 at 07:10 AM 0
Share
avatar image Tomer-Barkan · Oct 10, 2013 at 07:12 AM 0
Share
avatar image Spider_newgent · Oct 10, 2013 at 08:37 PM 0
Share
avatar image Tomer-Barkan · Oct 10, 2013 at 10:24 PM 0
Share
Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Realistic player movement? 2 Answers

move 2d character affected by physics with velocity and/or add force 2 Answers

Does AddForce use Time.deltaTime or TIme.fixedDeltaTime or it depends on whether it is being used in Update() or fixedUpdate()? 2 Answers

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

Add force in the OnStateUpdate. 0 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