• 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 phygitalmadhouse · Mar 17, 2018 at 10:58 PM · rotationrotate objectrotation axis

cant make Quaternion Slerp work properly

Hi all mates ,

Im mainly work as 3D Artist and need a helping hand with a script code for a personal proyect,

Im very stuck and lost several hours couple days and any help will be apreciate :

Is a little game test in Kinect , and want to rotate an object on a collision with kinect hands (like flight game).I have an issue returning the object to his original rotation like a plane does ,but as much as i read , im not able to make it work and im little lost.

Here is the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RotateOnCollision : MonoBehaviour {
 
     //object to rotate (player)
 
         public Transform objectToRotate;
         public float rotationInty;
         public float smooth;
         public float speed;
     
 
        
     
         // Use this for initialization
         void Start () {
     
     
             
         }
         
         // Update is called once per frame
         void Update () {
     
             
         }
     
         void recoverPosition()
         {
     
             Debug.Log("void recover");
             objectToRotate.transform.rotation = Quaternion.Slerp(from.rotation, to.rotation, Time.time * speed);
             
             Debug.Log("void recover end");
         }
     
         void FixedUpdate()
         {
     
         }
     
         void OnCollisionStay(Collision col)
         {
             if (col.gameObject.name == "24_Thumb_Right")
             {
                 
                 
                //rotate y axis
     
                 objectToRotate.transform.RotateAround(objectToRotate.transform.position, transform.up, -rotationInty);
     
                 
     
                 //rotate z axis
     
                 
     
                 objectToRotate.transform.Rotate(Vector3.forward * Time.deltaTime * smooth);
     
                 
             }
     
             
     
         }
         void OnCollisionExit(Collision col)
         {
             if (col.gameObject.name == "24_Thumb_Right")
             {
                 print("No longer in contact with " + col.transform.name);
     
                 recoverPosition();
     
                
                 
             }
     
         }
     
     }



Thanks in advance because sure im making lot of dumb code , lol .

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

Answer by KittenSnipes · Mar 18, 2018 at 02:41 AM

@phygitalmadhouse

I think this should work but I am not absolutely sure what objectToRotate is but on Start I save the objects original rotation. Hope it works out.

 using UnityEngine;
 
 public class RotateOnCollision : MonoBehaviour {
     //Object to rotate (player)
     public Transform objectToRotate;
     public float rotationInty;
     public float smooth;
     public float speed;
 
     Vector3 objectsOriginalPosition;
     Quaternion objectsOriginalRotation;
 
     void Start()
     {
         objectsOriginalRotation = objectToRotate.transform.rotation;
     }
 
     void RecoverPosition()
     {
         Debug.Log("void recover");
         objectToRotate.transform.rotation = Quaternion.Slerp(objectToRotate.rotation, objectsOriginalRotation, Time.deltaTime * speed);
         Debug.Log("void recover end");
     }
 
     void OnCollisionStay(Collision col)
     {
         if (col.gameObject.name == "24_Thumb_Right")
         {
             //rotate y axis
             objectToRotate.transform.RotateAround(objectToRotate.transform.position, transform.up, -rotationInty);
 
             //rotate z axis
             objectToRotate.transform.Rotate(Vector3.forward * Time.deltaTime * smooth);
         }
     }
 
     void OnCollisionExit(Collision col)
     {
         if (col.gameObject.name == "24_Thumb_Right")
         {
             print("No longer in contact with " + col.transform.name);
             RecoverPosition();
         }
     }
 }
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 phygitalmadhouse · Mar 18, 2018 at 08:57 AM 0
Share

Hi $$anonymous$$ate , Thanks i will test in a few (just woke up drea$$anonymous$$g with this issue lol).

avatar image phygitalmadhouse · Mar 18, 2018 at 09:35 AM 0
Share

@$$anonymous$$ittenSnipes It works and with it i understand now better how this things work , but makes same weird stuff that im not able to understand , told you :

Looks like , in both examples , the rotation is just affected in a "frame time" , like if the function stops next frame and just return his rotation the time between frames.

The speed as is a 0,1 range in Unity Docs , if 0 no lerp and if 1 full lerp and rotate to his original position but in a frame not smooth.(????why , dont know)

$$anonymous$$aybe is the collisions that just work in a frame-frame rate , i dont know really.

I can make it work in a fake way by animators , but im really obsessed with this simple code , lol.

If you have some more clue , will be aprreciate.

P.D: objetToRotate is a flying carpet that i want to rotate with collisions (kinect avatar as a controller , all works fine as is much complex code except this f... rotation and makes me sick , lol)

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

104 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

Related Questions

Problem when i rotate a 2d object aroun Z-axis 1 Answer

Build rotation tool for level editor 0 Answers

Rotating Cube with oculus touch controllers 0 Answers

Smoothly rotate object based on GetAxis 1 Answer

Rotating a bone upwards relative to camera. 2 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