Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
19
Question by rdshaner · May 18, 2012 at 08:46 AM · rotationvector3

How do I rotate an object towards a Vector3 point?

I'd like to rotate an object towards a Vector3 point without using LookAt. LookAt seems to instantly lock your view on the object. Does anyone know how to do this? I'm most familiar with C#, so if you could give an example using C# that would be great!

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

Answer by asafsitner · May 18, 2012 at 10:56 AM

If I understand you correctly you want to rotate an object to 'look at' another object over time. One way to do it is with Quaternion.LookRotation and Quaternion.Slerp:

 using UnityEngine;
 
 public class SlerpToLookAt: MonoBehaviour
 {
     //values that will be set in the Inspector
     public Transform Target;
     public float RotationSpeed;
 
     //values for internal use
     private Quaternion _lookRotation;
     private Vector3 _direction;
     
     // Update is called once per frame
     void Update()
     {
         //find the vector pointing from our position to the target
         _direction = (Target.position - transform.position).normalized;
 
         //create the rotation we need to be in to look at the target
         _lookRotation = Quaternion.LookRotation(_direction);
 
         //rotate us over time according to speed until we are in the required rotation
         transform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation, Time.deltaTime * RotationSpeed);
     }
 }

Comment
Add comment · Show 11 · 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 rdshaner · May 18, 2012 at 02:48 PM 0
Share

Thanks I got it!

avatar image WhiteSkull · Jul 14, 2012 at 05:57 PM 1
Share

Thanks good response

avatar image Double_D · Dec 31, 2012 at 03:13 PM 0
Share

This was exactly what I was looking for. Thanks!

avatar image Armand · Jan 20, 2013 at 04:25 PM 0
Share

Thanks! Simple but extremely useful

avatar image clabe45 · Jul 05, 2018 at 12:15 AM 1
Share

Also, transform.rotation = Quaternion.RotateTowards(transform.rotation, _lookRotation, maxDelta) for when you don't know the percentage but rather the increment

Show more comments
avatar image
6

Answer by Phoera · Jul 03, 2015 at 07:33 AM

or simply

transform.LookAt(Target)

Comment
Add comment · Show 3 · 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 RealSoftGames · Jul 12, 2015 at 12:27 AM 0
Share

this is ok, but really bad idea to use for an actual game as it locks to the target, most people want control over their Ai to give them more realistic movement or custom movement more to say. unfortunatly unity doesnt have any other alternative way to adjust the LookAt function in terms of rotation speed.

avatar image CabinOrange_Mel · Nov 25, 2018 at 09:57 AM 0
Share

A simple read of the original question would lead you to the fact that transform.LookAt(target) did not have the desired effect.

avatar image Tyrmuzari · Jan 03, 2021 at 08:57 AM 0
Share

Clean script. Thanks!!!

avatar image
1

Answer by yoyo696 · Oct 14, 2016 at 03:37 PM

Can you make it to a 2d game please???

Comment
Add comment · Show 6 · 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 Bonfire-Boy · Oct 14, 2016 at 04:04 PM 0
Share

Have you tried the solution already provided?

avatar image yoyo696 · Oct 16, 2016 at 08:09 AM 0
Share

Yes I tried but the 2d gameobject rotates I think in a 3D way till I can't see it anymore but when I convert the from a 2d to a 3d scene in the editor It's there and I can see it like a quad.Thanks For Help.

avatar image PositiveAnion yoyo696 · Dec 08, 2016 at 01:22 AM 0
Share

Add a

transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z)

It will fix this

avatar image yemonaung · Oct 16, 2017 at 05:06 AM 3
Share

Due to Unity's power of adapting both 3D and 2D planes with the same library methods, it sometimes doesn't really makes sense to translate the same methods directly from 3D to 2D. If you're making 2D game and just simply want to make something to look at a certain vector in world space (for example, turns a top down sprite towards mouse position), there's a lot easier way to do it. Just assign whatever your directional vector to the transform.right, but make sure to cast the direction to Vector2 before you do so (only to avoid the z axis confusions, nothing special). Works like magic. Here's the code example.

 Vector2 dir = this.mouseTrack.position - this.transform.position; 
 this.transform.right = dir; 

mouseTrack is a transform I've attached to the input mouse position, which isn't really important here. You can get the dir vector anyway you like. Hope that helps.

This basically means that you're treating your transform.right as a forward directional guide. Forget the Quaternion and rotation methods, forget the angle calculations, just pull the axis around to turn something to a certain direction.

On a side note though, I think Unity needs some simpler separate ways of manipulating transform only on the 2D axes.

avatar image HeliosJack yemonaung · May 02, 2018 at 04:39 PM 0
Share

very clever. this is a great answer. I'd subscribe to your 2d tips! Do you have more?

avatar image naviln yemonaung · May 02, 2020 at 03:24 PM 0
Share

Thankyou! I ended up having to adjust the rotation by -90 degrees since my image was upward facing. But nonetheless great solution

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

23 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

Related Questions

How do you perform multiple rotations on the same object? - (in a single frame) 0 Answers

Rotating a direction Vector3 by Quaternion 2 Answers

Rotate Vector3 array around a point 1 Answer

Set rotation based on 1,1,1 style vector? How to convert vector3 to quaternion? 1 Answer

Play incorrect animations when is looking in other direction 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