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
0
Question by deividkamui · Jul 04, 2018 at 08:29 AM · unity 5transformunity 2dquaternioneulerangles

How to rotate transform only on its Y axis?

Hi,

I'm trying to rotate an object only on its Y-axis in order to look at my player all the time, (like a turret motion)

alt text


I have tried this code:


 var step = speed * Time.deltaTime;
 
 Vector3 targetDir = target.position - transform.position;
 Debug.DrawRay(transform.position, targetDir, Color.red);
 Model.rotation = Quaternion.LookRotation(targetDir, Vector3.up);


and this one:


 var lookPos = target.position - transform.position;
 lookPos.y = 0;
 var rotation = Quaternion.LookRotation(lookPos);
 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);


But my object still rotates on all axis as seen on this image:


alt text


Any idea on how I can achieve this behaviour?



questionunity.png (10.0 kB)
questionunity.png (7.1 kB)
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

1 Reply

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

Answer by Shameness · Jul 04, 2018 at 09:03 AM

 Vector3 initialForward;
 void Start(){
     initialForward = transform.forward; // change this if its obj looking wrong way
 }
 void Update(){
     Vector3 lookPos = target.position - transform.position;
     Vector3 forward = (transform.position + initialForward) - transform.position;
     lookPos.y = 0;
  transform.eulerAngles = new Vector3(0, Vector3.SignedAngle(lookPos, forward, Vector3.down));
     Debug.DrawRay(transform.position, lookPos, Color.red);
     Debug.DrawRay(transform.position, forward, Color.green);
 }

Just tested.

Edit: added screenshot

alt text


screen-shot-2018-07-04-at-93246-pm.png (363.0 kB)
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 deividkamui · Jul 04, 2018 at 08:26 PM 0
Share

:( still moving in all axis:

alt text

questionunity.png (15.4 kB)
avatar image Shameness deividkamui · Jul 05, 2018 at 01:35 AM 0
Share

$$anonymous$$y full script.

 public class LookAtScript : $$anonymous$$onoBehaviour {
 
     public GameObject targetObj;
     public float speed;

     Transform target;
     Vector3 initialForward;
 
 
     // Use this for initialization
     void Start () {
         initialForward = transform.forward;
         target = targetObj.transform;
     }
     
     // Update is called once per frame
     void Update () {
         float h = Input.GetAxisRaw("Horizontal");
         float v = Input.GetAxisRaw("Vertical");
         if (Input.GetAxisRaw("Horizontal") > 0f || Input.GetAxisRaw("Horizontal") < 0f)
         {
             transform.position += new Vector3(h, 0) * speed * Time.deltaTime;
         }
         else if (Input.GetAxisRaw("Vertical") > 0f || Input.GetAxisRaw("Vertical") < 0f)
         {
             transform.position += new Vector3(0, 0, v) * speed * Time.deltaTime;
         }
 
 
         Vector3 lookPos = target.position - transform.position;
         Vector3 forward = (transform.position + initialForward) - transform.position;
         lookPos.y = 0;
         transform.eulerAngles = new Vector3(0, Vector3.SignedAngle(lookPos, forward, Vector3.down));
         Debug.DrawRay(transform.position, lookPos, Color.red);
         Debug.DrawRay(transform.position, forward, Color.green);
     }
 }
avatar image deividkamui Shameness · Jul 05, 2018 at 01:50 AM 0
Share

Is there any difference if my GameObject is a child Object? Because If I use the script on the parent GameObject (in this case Troll) the script works it locks the rotation on Y, but the Sprite Object which is the one I want to lock its rotation on the Y-axis continues to rotate in all axis.

alt text

questionunity.png (1.1 kB)
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

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

Having trouble with Position X location on objects 0 Answers

Commands affecting rotation not working 1 Answer

How to get Angle float of specific axis.(Turret clamping related). 2 Answers

My Obstacle Rotations Are Not Correct 0 Answers

Why transform.eulerAngles and rotation in editor transform are different? 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