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 /
avatar image
1
Question by knobblez · Nov 05, 2019 at 12:00 AM · transform.positionmovetowards

Help using Vector2.MoveTowards

I want my enemy to move towards the player and for now I just dropped this in FixedUpdate() to test it out.

The problem is it changes Z axis for some reason and is far away from visible.

Here is the code:

         transform.position = Vector2.MoveTowards(transform.position, Player.transform.position, moveSpeed * Time.deltaTime);

The Player and the Enemy are at the same world Z axis(-1940.75) so I don't understand why it's changing so much or at all. It should just be changing X and Y, especially because it's a vector2.

The Player is a child object where as the Enemy is not a child of anything. I do have the correct GameObject referenced.

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
3
Best Answer

Answer by GrayLightGames · Nov 05, 2019 at 05:16 AM

Hi @knobblez, setting transform.position (a Vector3) equal to a Vector2 will default the z component to 0. So if your player is not already at z = 0, it will immediately move to z = 0 from this code.

Is there a reason not to use Vector3.MoveTowards? That would be easiest, but you could always do something like this if you truly want to disregard z for this type of movement:

 Vector2 tempVector2 = Vector2.MoveTowards(transform.position, Player.transform.position, moveSpeed * Time.deltaTime);
 
 transform.position = new Vector3(tempVector2.x, tempVector2.y, transform.position.z);

Hope that helps!

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 knobblez · Nov 05, 2019 at 11:28 PM 0
Share

That did the trick! I want to keep the Enemy a couple points below the Z axis of the Player so that the Player is always on top. Thank you for your help!

avatar image GrayLightGames knobblez · Nov 05, 2019 at 11:52 PM 0
Share

Glad I could help, good luck with your project!

avatar image
0

Answer by naviln · Nov 05, 2019 at 12:14 AM

hmm. I think transform.position is a Vector3. Maybe try casting that to a Vector2 first, before passing it to Vector2.MoveTowards? Here is an example:


             Vector2 v2 = new Vector2(transform.position.x, transform.position.y);
             Vector2 v2Player = new Vector2(Player.transform.position.x, Player.transform.position.y);
             transform.position = Vector2.MoveTowards(v2, v2Player, moveSpeed * Time.deltaTime);
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 knobblez · Nov 05, 2019 at 12:55 AM 0
Share

That didn't work - same result. I appreciate the reply though

avatar image naviln knobblez · Nov 05, 2019 at 01:34 AM 0
Share

Sorry mate.

I am using this tweening library (it has a free version): http://dotween.demigiant.com/documentation.php


It has a Do$$anonymous$$oveX and Do$$anonymous$$oveY, where you can explicitly move individual x and y components. It seems to work well for me - see if that is an option for your project?


DO$$anonymous$$oveX/DO$$anonymous$$oveY(float to, float duration, bool snapping) $$anonymous$$oves the target's position to the given value, tweening only the chosen axis (note that in case of rigidbodies, the other axes will still be "locked" by the tween).

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

118 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

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