- Home /
How to use Lerp(Slerp) to rotate on the Z Axis.
Hey!
I've been looking at the videos and references and this place... But I can't for the life of me figure out how to rotate 35 degrees to either the right or the left. Smoothly. I can make the character rotate, but it snaps back and forth. So I though the Lerp function would work. Or Slerp... Though I have no idea how to use them. (: If anyone can help, it would be much appreciated!! ~WM
EDIT:
using UnityEngine;
using System.Collections;
public class LeaningScript: MonoBehaviour {
public float smooth = 6f;
public float target = 35f;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Q)) {
float angle = Mathf.MoveTowardsAngle (transform.eulerAngles.z, target, smooth * Time.deltaTime);
transform.eulerAngles = Vector3 (0, 0, 35);
}
if (Input.GetKeyDown (KeyCode.E)) {
float angle = Mathf.MoveTowardsAngle(transform.eulerAngles.z, target, smooth * Time.deltaTime);
transform.eulerAngles = Vector3(0, 0, -35);
}
}
}
So not quite sure what I'm doing wrong...?
Assets/LeaningScript.cs(14,49): error CS0119: Expression denotes a type', where a
variable', value' or
method group' was expected
Answer by MrProfessorTroll · Sep 26, 2013 at 08:58 PM
If you want to rotate on a specific axis, you probably have to use Mathf.Lerp / Slerp. You create a quaternion.euler and in the Z value, you put the Mathf.Lerp. Like this: http://docs.unity3d.com/Documentation/ScriptReference/Mathf.Lerp.html
All right! (: I may be wrong... But that link refers to the position ins$$anonymous$$d of the rotation correct?
Well you can use them in rotations if you want. Actually, for anything that involves numbers :)
So ... Would I substitute rotation in for position? (:
Something like this:
transform.rotation = Quaternion.Euler(0f, 0f, $$anonymous$$athf.Lerp(transform.rotation.eulerAngles.z, TARGETROTATION, Time.deltaTime* 5f));
Replace TARGETROTATION with the value you want. I'm not sure if this is correct, as I haven't tested it. Let me know if you run into any problems
Answer by Sisso · Sep 26, 2013 at 08:53 PM
Try to download some of unity3d sample projects and take a look how they solve this type of problem.
Answer by meat5000 · Sep 27, 2013 at 01:09 PM
Quaternion Slerp is what you need.
Vector3 Slerp will move an object in an arc without rotating it.
You can't call it once (like any lerp) and expect the function to do the rest. You must attend to it each frame progressively changing the value of t. This is why using deltaTime is a good option.
http://answers.unity3d.com/questions/229690/issues-with-lerps.html
Your answer
Follow this Question
Related Questions
really annoying rotation problem. 0 Answers
Changing this code to control camera with keys? 0 Answers
How to make the camera to focus on a player 1 Answer