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 Capt_Paradox · Aug 28, 2011 at 08:57 PM · rotationpointtransforms

Making an Object Rotate to Face Another Object

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!

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
5

Answer by rasheedqw · Sep 15, 2013 at 04:11 AM

 var damping:int=2;
 var target:Transform;
 
 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

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 FlightOfOne · Aug 04, 2016 at 02:17 AM 0
Share

Worked perfectly! Thanks

avatar image redhummingbird · Jun 28, 2017 at 07:04 AM 0
Share

This solution would have taken me forever to figure out. Thank you!

avatar image
3

Answer by Mortennobel · Aug 28, 2011 at 09:12 PM

Take a look at Transform.LookAt

http://unity3d.com/support/documentation/ScriptReference/Transform.LookAt.html

Comment
Add comment · Show 7 · 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 Capt_Paradox · Aug 28, 2011 at 10:00 PM 0
Share

Alright, thanks. Is there a way to do this and make the rotation only occur about the z-axis?

avatar image FTheCloud · Aug 28, 2011 at 11:38 PM 0
Share

Add a rigidbody to your object and check off freeze rotation under Constraints for x or y.

avatar image Capt_Paradox · Aug 29, 2011 at 06:40 PM 0
Share

Awesome; thanks!

avatar image aldonaletto · Aug 29, 2011 at 08:44 PM 0
Share

@FTheCloud, this won't work: rigidbody constraints only affect physics.

avatar image Daphoeno · Jun 30, 2013 at 12:28 PM 1
Share

I'm having a similar problem. I want it to only rotate on the Y-axis.

avatar image neosca Daphoeno · Jun 28, 2017 at 10:17 AM 0
Share

first you need to store the initial rotation than after you use transform.lookat, update localRotation.x and localRotation.z only. Thus rotating and facing other object with only y rotation. Quaternion initialRotation;

 void Start(){
     //store initial rotation
     initialRotation = Quaternion.Euler(new Vector3(this.transform.localRotation.eulerAngles.x,this.transform.localRotation.eulerAngles.y,this.transform.localRotation.eulerAngles.z);
 }

 void Update()
 {
 // Rotate the camera every frame so it keeps looking at the target
 this.transform.LookAt(target);

 //keep existing "y" rotation and set "x" and "z" rotation from intitalRotation
 this.transform.localRotation = Quaternion.Euler (new Vector3 (initialRotation .x, this.transform.localRotation.y, initialRotation .z));

 }
Show more comments

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

12 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

Related Questions

Object Rotation around a point at different Heights 0 Answers

Gun should point at mouse position 3 Answers

How to rotate a quad 1 Answer

Make an object point to another which keeping its forward along an axis 0 Answers

Applying a rotation to a point? 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