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 /
  • Help Room /
avatar image
1
Question by Koaten · Jan 30, 2016 at 01:26 PM · 2drigidbody2dphysics2dfollow player5.3

Rigidbody follow player and maintain all physical properties

I created a simple script that allowed one Rigidbody object (AI/Enemy) to smoothly chase after the Rigidbody player. However, it has one problem; the 'Enemy' is able to push the player across the screen with no regard for physics, and the player is powerless to push back, not matter how much force is applied.

Current AI/'Enemy' script:

 #pragma strict
 
 var rb2D: Rigidbody2D;
 var player : GameObject;
 var playerPOS : Vector2;
 var followVector : Vector2;
 var selfPOS : Vector2;
 
 function Start ()
 {
     rb2D = GetComponent.<Rigidbody2D>();
     player = GameObject.Find("CellPlayer");
 }
 
 function FixedUpdate () {
     playerPOS = player.transform.position;
     selfPOS = transform.position;
     followVector = Vector2.MoveTowards(selfPOS, playerPOS, 0.06);
     rb2D.MovePosition(followVector);
 }

The player's Rigidbody is moved using AddForce, which allows other forces to act against it, such as drag and collisions. However, my current method of moving the 'Enemy' requires MovePosition, which appears to ignore weight, drag, collisions, and basically all other forces.

I tried several ways to have the 'Enemy' follow the player through AddForce, but getting it to work to the point where it's smooth and reliable seems overly complicated.

Is there another form of movement that I missed that allows you to tell a Rigidbody object to move (or attempt moving) to a vector while preserving its physical properties? Or is there an easier way to accomplish this with AddForce?

Edit: This is for a 2D game on Unity 5.3.

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

Answer by Trigary · Jan 30, 2016 at 01:57 PM

MovePosition basically teleports the objects, don't use it if you want physics to work.

The other way is to use velocity vectors: [rigidbody2D].velocity = Vector2

In a top-down game you could do: EnemyRigidbody.velocity = transform.up * speed.y;

Comment
Add comment · Show 2 · 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 Koaten · Jan 30, 2016 at 07:29 PM 0
Share

Thank you for your answer! Unfortunately, velocity = Vector2 seems to work in a similar fashion to AddForce(Vector2), as in it continually applies force in a direction relative to itself, rather than towards a vector point on the map. To achieve following the player, I require either a method that allows adding force or velocity towards a vector point on the map, or a way to deter$$anonymous$$e the player's direction and distance from the 'Enemy' and then somehow transform that into an appropriate vector for AddForce or velocity.

avatar image Trigary · Feb 01, 2016 at 08:08 AM 0
Share

See the answer to this question: http://answers.unity3d.com/questions/954939/using-direction-and-rigidbody2daddforce-to-move-to.html

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

59 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

Related Questions

Arkanoid-like bounce troubles 1 Answer

Climbing code problem (2d physic). 0 Answers

Adding velocity to an object in a brick breaker game 0 Answers

Trying to add force to an object... is this done correctly? 0 Answers

2D Top-Down Character: How to make my character move using physics? 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