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 /
  • Help Room /
avatar image
0
Question by pascal281 · Dec 05, 2017 at 01:18 PM · scripting problemscript.quaternionlerp

Problem rotating and object with Quaternion.Lerp

Hey Guys

I have a problem trying to rotate an object around its y axis by pressing the A or D buttons. Whenever i try to rotate it ingame it starts to turn around randomly left and right on its y axis. What did i do wrong in this code ?

*edit: The Object needs to turn exactly 60 degrees on a button press and then stop.

Thanks in advance for your help ! :)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Camerascript : MonoBehaviour
 {
     Quaternion targetrotation;
     float speed = 1.0f;
 
     private void Update()
     {
         if (Input.GetKeyDown(KeyCode.A))
         {
             targetrotation = GameObject.Find("CameraPivotPoint").transform.rotation;
             targetrotation.y -= 60f;
             Debug.Log("targetrotation " + targetrotation);
         }
 
         if (Input.GetKeyDown(KeyCode.D))
         {
             targetrotation = GameObject.Find("CameraPivotPoint").transform.rotation;
             targetrotation.y -= 60f;
             Debug.Log("targetrotation " + targetrotation);
         }
         GameObject.Find("CameraPivotPoint").transform.rotation = Quaternion.Lerp(GameObject.Find("CameraPivotPoint").transform.rotation, targetrotation, Time.deltaTime * speed);
     }
 }
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
1
Best Answer

Answer by Hellium · Dec 05, 2017 at 02:05 PM

Unity uses Quaternions to handle rotation. You should not change the values of the x, y, z or w values directly if you don't know the mathematics behind quaternions.

Here is how I would do it:

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class Camerascript : MonoBehaviour
  {
      float speed = 1.0f;
      private Transform cameraPivotPoint ;
      private Quaternion targetrotation;
  
      private void Start()
      {
          cameraPivotPoint = GameObject.Find("CameraPivotPoint").transform ;
      }
  
      private void Update()
      {
          if (Input.GetKeyDown(KeyCode.A))
          {
              targetrotation = Quaternion.Euler( 0, -90, 0 ) * cameraPivotPoint.rotation;
              Debug.Log("targetrotation " + targetrotation.eulerAngles );
          }
  
          if (Input.GetKeyDown(KeyCode.D))
          {
              targetrotation = Quaternion.Euler( 0, 60, 0 ) * cameraPivotPoint.rotation;
              Debug.Log("targetrotation " + targetrotation.eulerAngles);
          }
          cameraPivotPoint.rotation = Quaternion.Lerp(cameraPivotPoint.rotation, targetrotation, Time.deltaTime * speed);
      }
  }
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 pascal281 · Dec 05, 2017 at 03:38 PM 0
Share

Thanks a lot for your help !

There was just a little issue with your script. Whenever the user presses a button twiche too fast, the rotation would get messed up. I fixed this by prohibiting input for 0.6 secounds after the function Get$$anonymous$$eyDown() was called.

Just in case anyone else ever needs that code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Camerascript : $$anonymous$$onoBehaviour
 {
     float speed = 20.0f;
     private Transform cameraPivotPoint;
     private Quaternion targetrotation;
     private bool rotationisfinished = true;
 
 
     private void Start()
     {
         cameraPivotPoint = GameObject.Find("CameraPivotPoint").transform;
     }
 
     private void Update()
     {
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A) && rotationisfinished)
         {
             targetrotation = Quaternion.Euler(0, +60, 0) * cameraPivotPoint.rotation;
             rotationisfinished = false;
             StartCoroutine(Timer());
         }
 
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D) && rotationisfinished)
         {
             targetrotation = Quaternion.Euler(0, -60, 0) * cameraPivotPoint.rotation;
             rotationisfinished = false;
             StartCoroutine(Timer());
         }
 
         cameraPivotPoint.rotation = Quaternion.Lerp(cameraPivotPoint.rotation, targetrotation, Time.deltaTime * speed);
     }
 
     IEnumerator Timer()
     {
         yield return new WaitForSeconds(0.6f);
         rotationisfinished = true;
     }
 }
avatar image
0

Answer by InjustticeGaming · Dec 05, 2017 at 01:31 PM

They way I would do it is with a Rigidbody. Nice and easy to use.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Test : MonoBehaviour {
     float rotateSpeed = 50f;
     Rigidbody rb;
 
     void Start(){
         rb = GetComponent<Rigidbody> ();
         rb.constraints = RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezeRotationX;
         rb.angularDrag = 3f;
         rb.useGravity = false;
     }
 
     void FixedUpdate(){
         Turn ();
     }
 
     void Turn(){
         if (Input.GetKey (KeyCode.A)) {
             rb.AddTorque (new Vector3 (0, rotateSpeed, 0));
         }
         if (Input.GetKey (KeyCode.D)) {
             rb.AddTorque (new Vector3 (0, -rotateSpeed, 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

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

174 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

Related Questions

How can I improve this code? 1 Answer

need help with simple timing code 1 Answer

Hello, I'm looking for help to achieve invisibility effect, I have a fragment of the code but not work 0 Answers

I want to do a pause panel 1 Answer

Where can I find the api document of Standard Assets 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