- Home /
Vector3.Slerp is not working help.
Hello i want to rotate object y0 to y90 but it does not work this is what i tried:
using UnityEngine;
using System.Collections;
public class OpenDoor : MonoBehaviour {
public bool isdooropen = false;
private float dooropensmoothness = 2f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (isdooropen == true) {
Vector3 DoorOpenAngleTo = new Vector3 (0,90,0);
transform.rotation = Vector3.Slerp (transform.rotation.y, DoorOpenAngleTo.y, dooropensmoothness * Time.deltaTime);
} else if(isdooropen == false){
//transform.Rotate (new Vector3(0,10,0) * dooropensmoothness * Time.deltaTime);
}
}
}
I tried everything but nothing works... can you stell me how to fix this slerp? this is what i want to happen: https://www.youtube.com/watch?v=xcWlXRESo-8 but the video gives errors soo i am trying to use vector3.slerp to fix this.
Answer by Positive7 · Sep 03, 2015 at 03:49 PM
void Update () {
if (isdooropen == true) {
Quaternion DoorOpenAngleTo = Quaternion.Euler(0f,90f,0f);
transform.rotation = Quaternion.Slerp (transform.rotation, DoorOpenAngleTo, dooropensmoothness * Time.deltaTime);
} else{
Quaternion DoorOpenAngleTo = Quaternion.Euler(0f,0f,0f);
transform.rotation = Quaternion.Slerp (transform.rotation, DoorOpenAngleTo, dooropensmoothness * Time.deltaTime);
}
}
This is actually not quite correct. It appears to work, but it's not the correct usage of neither Lerp nor Slerp. $$anonymous$$any people do exactly that with Lerp, which turns out to have some kind of fade-out effect, but that's actually not what the method is about.
See @OG23 answer for clarification.
Thanks. I cannot accept 2 questions as the best answer. Thanks to both of you guys.
Answer by hulahoolgames · Sep 03, 2015 at 03:59 PM
I can see a few problems with your code, correct me if I am wrong:
Vector3.Slerp takes in two vectors and spherically interpolates between them. You are passing two floats to this function. Also note, Slerp treats these vectors as directions and not positions in space like Lerp.
You are setting a Quaternion, transform.rotation but Vector3.Slerp returns a Vector3. You should be seeing errors in your console for these 2 problems.
Try using Quaternion.Slerp instead.
The Slerp function moves from a to b within time t. So when t = 0 the value returned should be a and when t = 1 the value returned should be b. See the Unity example in Vector3.Slerp. They have a total time in which they want to move from source to destination, called "journeyTime" and they store the "startTime". During Slerp, they pass the fracComplete value. This is what you should be using to pass as the last parameter in Slerp functions.
Thanks. I cannot accept 2 questions as the best answer. Thanks to both of you guys.
Your answer
