• 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 noxteryn · Jul 28, 2019 at 03:12 PM · transform.positionmovetowards

transform.position doesn't update?

Hello!

I just started dabbling with Unity today, and I wanted to make something simple, an enemy object chasing the player (imagine top-down Asteroids style), so I thought I'd use MoveTowards, like so:

 void Update()
     {
         transform.position = Vector2.MoveTowards(transform.position, target.transform.position, speed);
     }

Where target is a public GameObject set to the player object, and speed is supposed to be the speed at which the enemy object moves. But when I start the game, the enemy object simply beelines to the player's starting position and then stops moving, completely ignoring the player.

I added a Debug.Log(target.transform.position) to see where the game thinks the player is, and it is stuck at (0.0, 0.0, 0.0), the player's starting position. So, apparently, the script isn't tracking the player's transform.position and only reads its position once at the beginning.

Why is that? I don't understand this. Shouldn't the game realize that the player's transform.position has changed each time MoveTowards is called? The odd thing is, if I run a Debug.Log(transform.position), the script can track its own object's transform.position without problems. Why can't it track other objects?

How do I make the game get the player's transform.position correctly?

Comment
Add comment · Show 2
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 Divinitize1 · Jul 28, 2019 at 04:03 PM 0
Share
  void Update()
      {
          transform.position = Vector2.$$anonymous$$oveTowards(transform.position, target.transform.position, speed * Time.deltaTime);
      }

Try * Time.deltaTime.

Not sure if that's what you need but it might help.

avatar image xxmariofer Divinitize1 · Jul 28, 2019 at 07:20 PM 0
Share

no thats not the error

1 Reply

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

Answer by SirPaddow · Jul 28, 2019 at 06:04 PM

Your code seems ok (and yes, it should be aware of modifications to the target.transform.position).

You should check your player movement, be sure that you are actually moving the referenced gameobject and not a child or any other object.

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 xxmariofer · Jul 28, 2019 at 07:21 PM 1
Share

$$anonymous$$ost likely this is the error, you can do Debug.Log(target.name); and you will see that is not the name it should be printing, maybe another object or a parent of the real target

avatar image noxteryn xxmariofer · Jul 29, 2019 at 11:30 AM 0
Share

Okay, I have identified the problem. Ins$$anonymous$$d of using the player object from the Scene, I was using the player object from the Assets. Apparently, this matters. Solved it. Thanks for the leading me down the right path!

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

111 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

Related Questions

Move player between 3 given points only by touching on the left/right side of the screen 1 Answer

How can I move an object to a (0,0) and scale it to (0,0) at the same time? 1 Answer

Using time.deltatime to move from coordinate to coordinate is too fast? 1 Answer

How to get a proper ramming effect? 1 Answer

How do I get one object to move to (the closet) another object? 2 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