• 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 JBoy · Feb 08, 2012 at 09:30 AM · positionaitarget

Rotate object towards a target on only y axis?

How do i rotate an object towards a target on only y axis?

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

5 Replies

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

Answer by Vectrex · Feb 09, 2012 at 02:11 AM

A quick way is to transform.LookAt(target) and then erase the x and z rotations. Assuming you want something like an RTS character than never leans over

 void Update ()
 {
     // Look at including x and z leaning
     transform.LookAt(target);
 
     // Euler angles are easier to deal with. You could use Quaternions here also
     // C# requires you to set the entire rotation variable. You can't set the individual x and z (UnityScript can), so you make a temp Vec3 and set it back
     Vector3 eulerAngles = transform.rotation.eulerAngles;
     eulerAngles.x = 0;
     eulerAngles.z = 0;
 
     // Set the altered rotation back
     transform.rotation = Quaternion.Euler(eulerAngles);
 }
Comment
Add comment · Show 1 · 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 JBoy · Feb 09, 2012 at 02:51 AM 0
Share

thanks - it works.

avatar image
1

Answer by tianjishu · Feb 08, 2012 at 02:02 PM

Transform.RotateAround

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
-2

Answer by xylax · Feb 08, 2012 at 11:28 AM

Use these

Learn about them here

Without more info I cannot help you further.


 var target : Transform; 
 
 // Rotate the Object every frame so it keeps looking at the target 
 function Update() {
     transform.LookAt(target);
     
 }

Dont't forget to mark the question as answered and give a possable thumbs up.

Also you could add constantForce.relativeForce = Vector3(0, 0, 1); to make the object move forward at a constant speed.

Comment
Add comment · Show 4 · 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 JBoy · Feb 08, 2012 at 11:15 PM 0
Share

Could you give me an example script

avatar image xylax · Feb 09, 2012 at 01:44 AM 0
Share

sure, you have to explain what you want to do a bit more. Is it like a tire rolling toward something or more like an object pivoting around another object?

avatar image JBoy · Feb 09, 2012 at 01:47 AM 0
Share

i'm trying to create an ai script, i just can't wrap my brains around how to make the enemy face towards the player.

avatar image xylax · Feb 09, 2012 at 01:55 AM 0
Share

Ok, I will append my answer.

avatar image
0

Answer by frozendog_bren · Sep 13, 2016 at 12:12 AM

             transform.rotation = Quaternion.LookRotation (target.transform.position - transform.position);
             transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
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 afshin_a_1 · Apr 23 at 04:55 PM

rotate towards player around world y:

 float x = transform.rotation.eulerAngles.x;
 float z = transform.rotation.eulerAngles.z;
 Quaternion tempRotation = Quaternion.LookRotation(target.transform.position - transform.position);
 transform.rotation = Quaternion.Euler(x, tempRotation.eulerAngles.y, z);

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Disable a target after trigger exit? 1 Answer

How can I ground enemies and use target transform? 1 Answer

how to refer to enemy health 2 Answers

how to move an object, but make it stay on the ground 1 Answer

sprite changes z position without any input to do so 1 Answer


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