Alright. So, in the project I'm working on, I have an object that I want to turn to face another object. Simply put: how. Although it seems like a good option, I can't get transform variables to work. Also, for extra credit, can I make it reference a different object (with the same name) in each scene? Alternatively, the objects being rotated towards are immobile, so making the object rotate towards a given point would work as well. Thanks!
Answer by rasheedqw
Sep 15, 2013 at 04:11 AM
var lookPos = target.position - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
someone else came up with the code but it worked for me
Worked perfectly! Thanks
Answer by Mortennobel
Aug 28, 2011 at 09:12 PM
Take a look at Transform.LookAt
Alright, thanks. Is there a way to do this and make the rotation only occur about the z-axis?
Add a rigidbody to your object and check off freeze rotation under Constraints for x or y.
@FTheCloud, this won't work: rigidbody constraints only affect physics.
I'm having a similar problem. I want it to only rotate on the Y-axis.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
8 People are following this question.
rotation couldn't able to understand
Applying a rotation to a point?
Find out x at y
Preventing "LookAt" from flipping?
Make an object point to another which keeping its forward along an axis