• 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 Xeong-Hu · Mar 19, 2015 at 08:25 PM · movetowards

How do i make an object movetowards an object only on the X Axis Position?

Welp I tried it and I found that this is way of doing it keeps giving me an error. Though I don't know why. It seems logical. Welp anyways, here's my Script. Thought it was something pretty simple but guess I was wrong.

         Saccimo.gameObject.transform.position = Vector2.MoveTowards(transform.position.x, 0, Closest.YourTarget.gameObject.transform.position.x,0,0.01f);
 


Any help would be appreciated!

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

Answer by jpthek9 · Mar 19, 2015 at 10:33 PM

Unfortunately, Unity doesn't provide a default method to move towards a destination on a single axis. Instead, you can make your own.

 float Speed = 3;
 float Direction = Mathf.sign(destination.x - transform.position.x);
 Vector2 MovePos = new Vector2(
     transform.position.x + Direction * Speed, //MoveTowards on 1 axis
     transform.position.y
 );
 transform.position = MovePos;

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 Xeong-Hu · Mar 20, 2015 at 01:29 AM 1
Share

Oh Heeey! Thanks for Replying and Giving me an Answer!

But after a while I just decided to do a little work around it as you suggested.

Thanks for telling me Unity doesn't have a default $$anonymous$$ethod though. That's news to me!

I just simpley made a lock Y position function incase a certain event were to happen or is happening.

     public void LockYPos(){
         Saccimo.gameObject.transform.position = new Vector2
             (Saccimo.gameObject.transform.position.x,
              Saccimo.gameObject.transform.position.y * 0);
 
         }

I'll accept yours as an Answer though and keep it as a resource in case I'll need it. Thanks man!

avatar image jpthek9 · Mar 20, 2015 at 01:51 AM 0
Share

Glad that you found a solution!

avatar image
1

Answer by xLupey · Nov 05, 2016 at 08:57 AM

MoveTowards moves from a starting vector to a final vector, so set your starting vector with transform.position and then create a second vector to be the final vector with one component the same as your original x or y position and the other component equal to where you are going. This will cause the gameObject to only move in one direction at a constant speed.

 float speed = 2;
 
 gameObject.transform.position = Vector2.MoveTowards(transform.position, new Vector2(Closest.YourTarget.gameObject.transform.position.x, transform.position.y), Time.deltaTime * speed);

I apologize for such a late reply, but I couldn't find this particular solution anywhere else and figured others would like to know.

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

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

25 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

Related Questions

gradually raise moving speed but got different results on different devices 1 Answer

Best way to rotate a door 1 Answer

Simple GameObjects. Unexpected movement. 0 Answers

Magnet Power Up,moving the coins towards the player 1 Answer

How do I get bullet to move towards mouse when spawned. 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