• 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 micha2929 · Mar 20, 2015 at 12:43 PM · ai

How do i make an object follow my player without changing its rotation(2D, C#))

I am trying to make an enemy follow my player without changing its rotation

this is the code im using

 void Update () {
         //move towards the player
         if (Vector3.Distance(transform.position,target.position)< aggroRange){
             transform.position = Vector3.MoveTowards(transform.position, target.position, speed*Time.deltaTime);
         }
     }

but the problem with this is it for some reason doesnt follow me all the way and has some sort of offset.

How do i make the enemy follow me all the way to the player.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Landern · Mar 20, 2015 at 12:46 PM

If speed*time.deltaTime becomes a negative number then it will move away.

If you have rigidbody's attached and colliders then it may collide and stop never reaching/overlapping the other 3d object.

Put a debug statement in your code and watch it:

 void Update () {
          //move towards the player
          if (Vector3.Distance(transform.position,target.position)< aggroRange){
             float newSpeed = speed * Time.deltaTime;         
             Debug.Log("My Speed: " + newSpeed.ToString());
             transform.position = Vector3.MoveTowards(transform.position, target.position, newSpeed);
          }
      }
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 DiegoSLTS · Mar 20, 2015 at 01:01 PM

You're asking for 2 different things or I'm not understanding your question.

MoveTowards takes a point and a target point, a returns a new point between those 2, so it always gives you a point closer to the target. It doesn't change the rotation of the object, and the code you provided doesn't change the rotation either. If your enemy is being rotated and you don't want that behaviour, the problem is in another script.

About the offset you'll have to be more specific, it's important to know what the transform.position actually is, maybe your player model has it's origin in a corner instead of the center, so the enemy moves to that corner.

Comment
Add comment · Show 3 · 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 micha2929 · Mar 20, 2015 at 01:54 PM 0
Share

What i meant is I'm searching for a piece of code that moves an object to another object, because a common way to do this is to just move forward and make the object face the other object but this is not what I'm looking for, I'm looking for a way to make the enemy chase my player without changing its rotation, and Vector3.MoveTowards doesnt move it all the way it has some sort of gap between the enemy and my player.

avatar image DiegoSLTS · Mar 20, 2015 at 04:10 PM 0
Share

Add this lines after the MoveTowards call:

Debug.Log("position " + transform.position); Debug.Log("target " + transform.position);

Check if they're different when the moving object stops. If they're the same then the MoveTowards code works as expected and you have a problem with your model's pivot as I said before.

Another cause could be that the target variable has a copy of the target gameobject instead of a reference, and after moving the original target the enemy moves towards the original position. It's more common to have references, but maybe you did a copy for some reason.

avatar image micha2929 · Mar 21, 2015 at 09:24 AM 0
Share

Hmm they are in the same position and its a 2D game there are no problems with the colliders nor the pivots. Also its moving away from me when im moving towards it.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

AI repeats movement and isnt randomised 0 Answers

Buggy movement for navmeshagent 0 Answers

Keeping track of lots of entities 2 Answers

Unity ML tutorial 0 Answers

Guard AI acting bonkers - Please Help! 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges