- Home /
Quaternion Slerp help
I've been looking around and even if I use the exact code people are using I still can't get this to work for the life of me so I've been just working on my own code to no avail
public float speed;
private Transform CurrentPos;
void Start() {
CurrentPos = transform.rotation;
}
void OnColisionEnter ()
{
CurrentPos = Quaternion.Slerp(CurrentPos, Quaternion.Euler(0,90,0), Time.deltaTime * speed);
}
I get an error after start on line 5
CurrentPos = transform.rotation;
error CS0029: Cannot implicitly convert type
UnityEngine.Quaternion' > to
UnityEngine.Transform'
I then get 2 errors on line 10
CurrentPos = Quaternion.Slerp(CurrentPos, Quaternion.Euler(0,90,0), Time.deltaTime * speed);
The best overloaded method match for
UnityEngine.Quaternion.Slerp(UnityEngine.Quaternion, > UnityEngine.Quaternion, float)' has > some invalid arguments > error CS1503: Argument
#1' cannot convertUnityEngine.Transform' > expression to type >
UnityEngine.Quaternion'
No matter how I try to approach it I get errors, I can't seem to grasp Quaternion by the looks of it, I have tried setting it to Transform.Rotation,Y = 90 as the target position but I still get errors
I understand that Quaternion uses X,Y,Z,W but it even says on the Scripting API that you can use Quaternion.Euler which gives a Vector3 and you still get an error
I feel unity API could explain these a lot better, from what I'm reading none of the methods seem to work
Try This:
private Vector3 currentAngle;
void Start() {
currentAngle = child.transform.localEulerAngles;
}
void OnColisionEnter(){
transform.localEulerAngles = Vector3.Slerp(currentAngle, new Vector3( 0, 90, 0), Time.deltaTime * speed);
}
You should use OnCollisionStay, with Slerp(), ins$$anonymous$$d of OnCollisionEnter/Exit.
I changed it to void OnTriggerStay()
and it works and I get the message that the collision happened but it only happens once, it doesn't update it to 90 it just does 1 frame then stops
Answer by Bluntweapon · Sep 16, 2014 at 05:00 PM
The following code assumes you've already changed to OnCollisionStay, and that this script is attached to whichever object you want to rotate on contact.
public float speed;
private Quaternion targetRotation;
void OnCollisionEnter(){
//rotation * euler(0,90,0) should give you the current rotation rotated 90 degrees
targetRotation = transform.rotation * Quaternion.euler( 0, 90, 0 );
}
void OnCollisionStay ()
{
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * speed);
}
You'll notice you don't really need whatever was in Start(), since calling transform.rotation is basically the same.
EDIT: I just realized something. OnCollisionStay is called every frame something stays in the collider. If you just destroy the colliding object then it makes sense that OnCollisionStay only fires off once and you only rotate for 1 frame.
EDIT 2: Man I don't know how I totally confused rotations for positions...
When I have it onTriggerStay the guys goes through the box which is what I want but after that it just stays
http://puu.sh/bBxeu/4f3fa47eb3.png
As you can see the box is still there but it's not moving the door more than the 3 it already did
Ignore what I said. I apparently confused rotations for positions.
Edited above answer.
I changed it to update and I noticed that it didn't move, it just stuck depending on what the speed was
I managed to get it working in a way that is better for the purpose of opening a door by using
void OnTriggerEnter()
{
Debug.Log ("Checking If Req $$anonymous$$et");
Debug.Log (front$$anonymous$$ey);
if(front$$anonymous$$ey == 1)
{
doorOpen.enabled = true;
}
}
And my doorOpen is
void Start() {
currentAngle = rightDoor.localEulerAngles;
currentAngle = leftDoor.localEulerAngles;
}
void Update()
{
timeTime = timeTime + Time.deltaTime;
rightDoor.localEulerAngles = Vector3.Slerp(currentAngle, new Vector3( 0, 90, 0), timeTime * speed);
leftDoor.localEulerAngles = Vector3.Slerp(currentAngle, new Vector3( 0, -90, 0), timeTime * speed);
if (rightDoor.eulerAngles.y >= 90)
{
this.enabled = false;
}
}
This seems to work quite well, after the door is open it disables itself to stop Update draining resources
I will also try your code tomorrow to see if that works, might help with future scripts
This way is probably better, as it doesn't depend on colliders being in other colliders.