- Home /
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 .
Answer by KittenSnipes · Mar 18, 2018 at 02:41 AM
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();
}
}
}
Hi $$anonymous$$ate , Thanks i will test in a few (just woke up drea$$anonymous$$g with this issue lol).
@$$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
Follow this Question
Related Questions
Having trouble rotating a turret 1 Answer
How to create quaternion defined by angle and up vector 2 Answers
How to tilt object towards the direction it's going, while it can be rotated to face any direction? 0 Answers
Strange rotation pattern. 0 Answers
GameObject is not rotating/scaling the way I expect it to [Hololens] 0 Answers