Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 JeffreyBennett · Aug 26, 2020 at 10:44 AM · quaternionquaternion.slerp

Quaternion.Slerp Does Not Rotate Object

In Unity 2020, I have an object that I want the user to be able to drag and rotate in all three axes, then release the mouse button and have it "float" back to its original rotation. It cannot be moved with a mouse drag, just rotated.

I'm trying to use Quaternion.Slerp, but even though debug lines print to the console just before and after the Quaternion.Slerp line, it doesn't return to its original rotation.

In my Start() I get the original rotation of the object and save that in a variable. Then OnMouseUp I check to see if the object's current rotation is equal to the original. If not, then I want to use Quaternion.Slerp to rotate it from its current position to the original position over time.


Can anyone tell me what I'm doing wrong? - Below is my code: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class userRotateObject_mouse : MonoBehaviour {

 public Vector3 mPrevPos = Vector3.zero;
 public Vector3 mPosDelta = Vector3.zero;
 public float rotationSpeed = 0.2f;
 public float retRotationSpeed = 5.0f;
 private Quaternion originalRotation;
 private Vector3 mOffset;
 private float mZCoord;


 void Start()
 {
     originalRotation = transform.rotation;

 }



 

//After rotating the object with the mouse the user can release to let the object seek its original rotation. void OnMouseUp() {

     if (deviceShouldTakeInitialPos && (transform.rotation.x != originalRotation.x || transform.rotation.y != originalRotation.y || transform.rotation.z != originalRotation.z) )


     {
         Debug.Log("Inside the IF");
         transform.rotation = Quaternion.Slerp(transform.rotation, originalRotation, Time.deltaTime * retRotationSpeed);
         // StartCoroutine(waitForTime());
         Debug.Log("Exiting the IF");

     }

 }

 
 

//User can click/drag on the object to rotate in all three axes. void OnMouseDrag()

 {
     mPosDelta = Input.mousePosition - mPrevPos;

     if (Vector3.Dot(transform.up, Vector3.up) >= 0)
     {

         transform.Rotate(transform.up, -Vector3.Dot(mPosDelta, Camera.main.transform.right), Space.World);


     }
     else
     {
         transform.Rotate(transform.up, Vector3.Dot(mPosDelta, Camera.main.transform.right), Space.World);

     }

     transform.Rotate(Camera.main.transform.right, Vector3.Dot(mPosDelta, Camera.main.transform.up), Space.World);
     mPrevPos = Input.mousePosition;
  
 }

}

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 JeffreyBennett · Aug 26, 2020 at 10:46 AM 0
Share

Also, I don't know what's up with the screwy formatting of the question. Sorry about that.

1 Reply

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

Answer by xxmariofer · Aug 26, 2020 at 11:51 AM

on mouse up only gets called once, so it will not get reset completly change the code to this

  void OnMouseUp() {
     StartCoroutine(ReturnRotation());
  }
 
 IEnumerator ReturnRotation()
 {
     while (deviceShouldTakeInitialPos && (transform.rotation.x != originalRotation.x || transform.rotation.y != originalRotation.y || transform.rotation.z != originalRotation.z) )
      {
          transform.rotation = Quaternion.Slerp(transform.rotation, originalRotation, Time.deltaTime * retRotationSpeed);
          yield return null;
      }
 }
Comment
Add comment · Show 2 · 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 JeffreyBennett · Aug 26, 2020 at 12:20 PM 0
Share

Ok, that basically worked!

It gave an error on the ReturnRotation function (Cannot be an iterator), so I changed: void ReturnRotation() to: private IEnumerator ReturnRotation() and that did the trick. Thank you!

avatar image xxmariofer JeffreyBennett · Aug 26, 2020 at 12:22 PM 0
Share

yes it was pseudocode i will edit it

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

135 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

Related Questions

Quaternion Slerp 180 Away from target 1 Answer

few question about quaternion slerp 3 Answers

How do you check if Quaternion.Slerp has finished 5 Answers

How do I make an object face forward while it is orbiting another object? 1 Answer

By-pass the 'shortest route' aspect of Quaternion.Slerp 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