Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 obsidianz · Jul 13, 2020 at 02:37 PM · movementaddforcetopdownmovetowards

Top Down Movement with AddForce()

I'm making a top down shooter. To make the enemies (and player) move I use the MovePosition function.

When a bullet hits an enemy it pushes the enemy using AddForce. The problem is: this force is overwitten by MovePosition right after being added.


I thought about using AddForce instead of MovePosition and somehow cap the max speed of the enemy, but I think that it will also cap the force that the bullet adds to the enemy.

What's the best way to do this?


Here's my movement script:

 public class EnemyMovement : MonoBehaviour
 {
     public Rigidbody2D body;
     public Rigidbody2D player;
     public float speed = 3f;
 
     public int stopTime;
 
     void Start()
     {
         player = null;
         stopTime = 0;
 
         GameObject temp = GameObject.Find("Player");
         if (temp) player = (Rigidbody2D) temp.GetComponent("Rigidbody2D");
     }
 
     void FixedUpdate()
     {
         if (stopTime > 0) stopTime--;
 
         if (stopTime == 0 && player != null)
         {
             Vector2 direction = (player.position - body.position).normalized;
             body.MovePosition(body.position + direction * speed * Time.fixedDeltaTime);
         }
     }
 }


The way I am dealing with this at the moment is to not let the enemy move for a few frames when a external force is applied (thats what the variable stopTime is for), but I was hoping to find a better way to deal with this problem.

Comment
Add comment
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

1 Reply

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

Answer by unity_ek98vnTRplGj8Q · Jul 13, 2020 at 02:54 PM

Definitely not an expert on physics movement controllers, but maybe you could use AddForce with a high friction value (or, even better, implement your own friction) that way you will have a terminal velocity for movement but you can still add higher impact forces if you want. The friction could be as simple as multiplying your velocity by like 0.8 or something each frame.


It also seems like using a force model will make your movement less snappy... have you thought about not using physics at all? You could use MovePosition to move the character, then keep track of impact physics yourself as a Vector3 that is added to the movement commands every frame and is scaled down every frame as well, something like

 Vector3 externalForces; //Add a value here on bullet impact
 float friction = 0.8f;
 
 void Update(){
     rigidbody.MovePosition(movement + externalForces);
     externalForces *= friction;
 }
 

 
Comment
Add comment · Show 1 · 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 obsidianz · Jul 13, 2020 at 05:57 PM 0
Share

Your last suggestion worked perfectly. Thanks a lot.

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

199 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 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

Push an object opposite direction of mouse position 0 Answers

Make object jump to fixed y position 1 Answer

Moving an object using rigidbody.AddForce & keyboard input. 1 Answer

Move camera and player OR move objects? 1 Answer

Help with topdown movement in Unity2d? 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