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 LukeThePunk666 · Nov 26, 2019 at 05:40 PM · rigidbodyrotation axiseuler anglesquaternion.slerp

Quaternion.slerp messing with wrong axies

So I'm trying to write a script to rotate my sled between -10 and 10 degrees on the X axis when pressing the left and right arrow keys respectively. What currently happens is that the sled will flip entirely the wrong direction on the Y axis and begin to spiral out.

I have been trying to figure this out for days, can anyone help?

     private void Start()
     {
         rb.transform.rotation = Quaternion.Euler(0, 180, 20);
         AngleL = Quaternion.Euler(-10, rb.rotation.y, rb.rotation.z);
         AngleR = Quaternion.Euler(10, rb.rotation.y, rb.rotation.z);
         Physics.gravity = new Vector3(0, -400f, 0);
     }
     void FixedUpdate()
     {
         transform.Translate(new Vector3(-moveSpeed,0,0) * Time.deltaTime);
         if(Input.GetAxis("Horizontal") > 0)
         {
             transform.Translate(new Vector3(0,0,turnSpeed) * Time.deltaTime);
             //anim.SetTrigger("Right");
             currentAngle = AngleR;
             transform.rotation = Quaternion.Slerp(transform.rotation, currentAngle, 0.1f);
         }
         if (Input.GetAxis("Horizontal") < 0)
         {
             transform.Translate(new Vector3(0,0,-turnSpeed) * Time.deltaTime);
             currentAngle = AngleL;
             transform.rotation = Quaternion.Slerp(transform.rotation, currentAngle, 0.1f);
         }
     }

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 TreyH · Nov 26, 2019 at 06:08 PM 0
Share

I think you might be a bit confused about how Unity does rotations. Someone better suited for the explanation will probably come along and help, but this bit here is definitely not doing what you probably think it is:

 rb.transform.rotation = Quaternion.Euler(0, 180, 20);
 AngleL = Quaternion.Euler(-10, rb.rotation.y, rb.rotation.z);
 AngleR = Quaternion.Euler(10, rb.rotation.y, rb.rotation.z);
 Physics.gravity = new Vector3(0, -400f, 0);


Those y and z components are not in degrees, btw.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by TreyH · Nov 26, 2019 at 08:40 PM

You're getting weird behavior because the Start() function body seems to have some unit confusion and there's explicit Quaternion usage.

So for rotations, you will want to keep track of when you're dealing with the tradition Euler angles and when you're dealing with Quaternions. Unity keeps track of transform orientation with Quaternions, so this can seem a bit daunting but actually becomes pretty convenient once you learn how to use their operators / built-in functions.

For your case, I'm not sure why you're starting off with that weird rotation so I'll skip it in my example. Your goal is to gradually tilt something +/- 10 degrees on the Y-axis based on player input, so we'll just do that directly.

 using UnityEngine;
 
 public class RotateSlightly : MonoBehaviour
 {
     Rigidbody rb;
     Quaternion initialRotation;
     Quaternion desiredLeftRotation;
     Quaternion desiredRightRotation;
 
     float horizontal;
     Quaternion targetRotation;
 
     // Start is called before the first frame update
     void Start()
     {
         this.rb = this.GetComponent<Rigidbody>();
 
         // Just take whatever rotation we started with
         this.initialRotation = this.rb.rotation;
 
         // Note that the * operator lets us compound rotations.
         this.desiredLeftRotation = Quaternion.Euler(0, -10, 0) * this.initialRotation;
         this.desiredRightRotation = Quaternion.Euler(0, 10, 0) * this.initialRotation;
     }
 
     // Update is called once per frame
     void Update()
     {
         // Get our input and assume the initial rotation
         this.horizontal = Input.GetAxis("Horizontal");
         this.targetRotation = this.initialRotation;
 
         // Check if we're trying to move in some direction
         if (this.horizontal > 0) 
             this.targetRotation = this.desiredRightRotation;
         else if (horizontal < 0)
             this.targetRotation = this.desiredLeftRotation;
 
         // Apply that rotation
         this.rb.rotation = Quaternion.Slerp(this.rb.rotation, this.targetRotation, 0.1f);
     }

     void OnGUI()
     {
         GUI.HorizontalSlider(new Rect(0, Screen.height - 40, Screen.width, 40), horizontal, -1, 1);
         GUI.TextArea(new Rect(Screen.width / 2 - 100, Screen.height - 80, 200, 40), this.rb.rotation.ToString("F4"));
     }
 }



You should be able to drop that onto an object and get the following behavior:

alt text


rotate-quats.gif (329.8 kB)
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

157 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

Related Questions

Can you add a force to a rigidbody at an angle? 3 Answers

Rotation Problem 1 Answer

No more collision while Quaternion.Slerp 0 Answers

How can I create a joint between objects which only allows for rotation around an axis? 1 Answer

Bullet rifling simulation 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