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 /
  • Help Room /
avatar image
0
Question by TheDJBuntin · Jan 24, 2017 at 05:20 AM · rotationrotatelookatrotate objectrotation axis

How to object rotate on a single axis towards another object? C#

I have an object A in a 3D world (ortho view). I want object A to rotate along the X axis to face object B. Object A must not change on the y and z axis.

Visual Aid: http://i.imgur.com/6VsBfGE.png - spaceship lies at a point with rotation 0, -90, 90. It must face the planet with the front of the ship, like this: http://i.imgur.com/cE94rlI.png

I've tried a whole range of things from other threads but can't get it working. Unity v5.5

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

3 Replies

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

Answer by TheDJBuntin · Jan 24, 2017 at 03:20 PM

So I knew I was complicating things far too much. So I took it down to the basics. This is my problem in its simplest form: http://i.imgur.com/ImcmpxQ.png

From the positions we can calculate the sides of the triangle with the code

 float z = Vector3.Distance(objectB.transform.position, objectA.transform.position);
 float x = objectB.transform.position.x - objectA.transform.position.x;
 float y = objectB.transform.position.y - objectB.transform.position.y;

then we use the equation b = arccos((y2 + z2 - x2)/2yz) to calculate the angle:

 float angle = Mathf.Acos((Mathf.Pow(y, 2.0f) + Mathf.Pow(z, 2.0f) - Mathf.Pow(x, 2.0f) / 2 * y * z));

BUT Mathf.Acos gives the number back in Radiants. So to convert to Degrees do:

 angle *= Mathf.Rad2Deg;

However, if we were to put this into Rotate() right now, it would rotate the wrong way if ObjectB is on the right side of ObjectA, so we have to make the angle a negative if it is on the right hand side (ie if x position of ObjectB is larger than ObjectA's)

 if (x > 0)
             angle = -angle;

then finally, put this angle into the simple Rotate() function, rotating on the x axis

 objectA.transform.Rotate(Vector3.forward, angle);

Here is the thing in its entirety for easy copy and pasting for anyone looking this up:

  float z = Vector3.Distance(objectB.transform.position, objectA.transform.position);
  float x = objectB.transform.position.x - objectA.transform.position.x;
  float y = objectB.transform.position.y - objectB.transform.position.y;
  float angle = Mathf.Acos((Mathf.Pow(y, 2.0f) + Mathf.Pow(z, 2.0f) - Mathf.Pow(x, 2.0f) / 2 * y * z));
  angle *= Mathf.Rad2Deg;
  if (x > 0)
      angle = -angle;
  objectA.transform.Rotate(Vector3.forward, angle);
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 BlueDragonHatch · Jan 24, 2017 at 06:12 AM

Wold a Quaternion.setfromtorotation work for you? So it would be kinda lige this:

 public float speed;
     void Update() {
         float step = speed * Time.deltaTime;
         transform.rotation = Quaternion.RotateTowards(A.rotation, Quaternion.setfromtorotation(A.Forwards, (B. Transform - A.Transform) ) , step);
     }

Not sure ifølge A.forward is valid, but should work othervise, the B - A Thing is just to Construct Vector from A pointing towards B

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 BlueDragonHatch · Jan 24, 2017 at 06:14 AM 0
Share

Sorry for the spelling Errors, im om mobile and autocorrect is coding i sine :D

avatar image TheDJBuntin · Jan 24, 2017 at 10:47 AM 0
Share

Here is the result: http://i.imgur.com/HbHH$$anonymous$$yo.png - The only difference is I set its position/rotation etc in an init function, so it should instantly be set to this new rotation, to combat this I just put 360 ins$$anonymous$$d of step.

Here is a cleaned up version of your code incase anyone comes across this, with $$anonymous$$or adjustments (ie forward for me is negative up and I set rotation to max as I want it to go instantly):

         transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.FromToRotation(-transform.up, (Planet.transform.position - transform.position)), 360.0f); 

With this, the ship is facing the planet which is good. But it has also moved the other axis's which isn't. The top of the ship should always face "up". The ship on the bottom of that screenshot is how it should look, the one on the left is incorrect.

avatar image
0

Answer by _Keith · Feb 09, 2017 at 02:29 AM

 transform.rotation = Quaternion.LookRotation(Vector3.forward, target.transform.position);

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.

Follow this Question

Answers Answers and Comments

103 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

Related Questions

ROTATION STOPS when i move the char on the air! 0 Answers

have one object rotate another object on the y axis 0 Answers

How to rotate an object according to the camera's view? 1 Answer

Rotating on other axis 1 Answer

Rotate an Object while moving forward using empty gameObject 0 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