• 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 Elysian.Zhen · Jan 16, 2012 at 11:33 AM · rotationmovementtransformanglevector

Make an object move to a given point by rotating to the correct direction first.

How do I make an object move to a given point by first rotating itself to the correct direction then move forward? BUT I don't want diagonal movements, therefore first move the x value of the position until it is the same with the target's x value. Then repeat for z as well.

For example, my character is standing at (0,0) and facing in the forward direction (0,0,1). I want him to move to (15,21). So I will first rotate him 90 degrees so he can move forward in the x axis. I'll keep him moving forward in the x axis until it is 15. Next, I'll rotate him -90 degrees so he is now facing in the z axis direction. And I'll keep moving him forward in the z axis until it is 21. Finally, stopping on the spot.

Here's my code (Which apparently doesn't work):

 var pos = transform.position;
     //pos = new Vector3((float) Mathf.Round(pos.x), (float) Mathf.Round(pos.y), (float) Mathf.Round(pos.z));
     pPos = pos;
     
     if (target == Vector3.zero || target - pos == Vector3.zero)
         return;
     Debug.Log(string.Format("Pass! Target: {0} Pos: {1}", target, transform.position));
     
     
     var angle = 0F;
     
     if (target.z != pos.z)
     {
         if (target.z > pos.z)
         {
             angle = Mathf.PI;
         }
         else if (target.z < pos.z)
         {
             angle = 0F;
         }
         
     }
     else if (target.x != pos.x)
     {
         if (target.x > pos.x)
         {
             angle = Mathf.PI/2;
         }
         else if (target.x < pos.x)
         {
             angle = Mathf.PI*3/2;
         }
         
     }
     
     
     
     

     
     
     if (Mathf.Abs(angle - currRot) > float.Epsilon)
     {
         var diff = angle - currRot;
         
         transform.RotateAround(Vector3.up, diff);
         currRot = angle;            
     }
     
     transform.position = (transform.forward) + pos;

Would anyone be kind enough to help me here? Thanks.

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

Answer by Elysian.Zhen · Jan 16, 2012 at 12:05 PM

I got it. The trick is to round the values up if it is close to the target value. So for example, 4.143 will round up to 4. And 3.5697 will round up to 4 as well. This way my script won't cause random rotations because of the small values.

 pos = transform.position;
     pos = new Vector3(
                       Mathf.RoundToInt(pos.x) == (int) target.x ? Mathf.RoundToInt(pos.x) : pos.x,
                       1.5F,
                       Mathf.RoundToInt(pos.z) == (int) target.z ? Mathf.RoundToInt(pos.z) : pos.z
                       );
     
     transform.position = pos;
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rotate an object around another object at an angle from the X axis? 1 Answer

Object not rotating when the associated trigger is triggered 1 Answer

Quaternion.FromToRotation misunderstanding 2 Answers

How do i rotate based on direction character is moving in 1 Answer

How to rotate a character facing direction of movement like sonic the hedgehog 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