• 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 Spatenfrosch70 · Dec 27, 2016 at 07:10 AM · rotationgameobjectrotation axisy axis

How can rotate the gameobject only around the y axis?

The gameobject should rotate only around y axis. PlayerDistance should not be changed. i need this for another script. The gameobjects should look to the player. (when the playerdistance < 10) (my english is bad sorry).

using UnityEngine; using System.Collections;

public class NM2 : MonoBehaviour {

  public Transform player;
  public float playerDistance;
  public float rotationDamping;
  public float x = 10f;
  // Use this for initialization
  void Start()
  {
  }
  // Update is called once per frame
  void FixedUpdate()
  {
      playerDistance = Vector3.Distance(player.position, transform.position);
      if (playerDistance < x)
      {
          lookAtPlayer();
    
          
      }
      
     
  }
  void lookAtPlayer()
  {
    Quaternion rotation = Quaternion.LookRotation(player.position - transform.position);
      transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationDamping);                     
  }
   
Comment
Add comment · Show 1
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 AlwaysSunny · Dec 27, 2016 at 07:10 AM 0
Share

Please remember to format your code with the 101010 button.

If the current rotation and desired rotation only involve the Y axis, you can probably get the desired behavior by using Quaternion.Lerp() ins$$anonymous$$d of .Slerp()

If this does not work in your situation, please try to explain further.

Best,

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jshaw3 · Aug 21, 2019 at 07:27 PM

I was using ARFoundation and had a character I always wanted to face the AR Camera. I was having some trouble getting lookAt to work correctly and only rotating on the Y-axis. Using lookat the character seemed to always rotate on all 3 axis.

I found an old Reddit post talking about Mathf.Atan2. I found it really helpful to calculate the angle between two points and rotate the character on the y axis only. Found it very helpful.

https://docs.unity3d.com/ScriptReference/Mathf.Atan2.html

Original Reddit post is here: https://www.reddit.com/r/Unity3D/comments/1m1dzf/c_transformlookat_on_y_axis_only/

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Rotate an Instantiated GameObject a set amount from another instantiated GameObject? 1 Answer

Rotation Around Tilted Y-Axis 1 Answer

Gameobject rotation breaks children position 1 Answer

Mathf.MoveTowardsAngle is not working as predicted 1 Answer

Rotation help! 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