- Home /
Slowly change an objects Z rotation to certain value
I'm trying to get an object to spin from 0 to 90 degrees on the Z axis over time, instead of instantly. I've tried using Vector3.Lerp, but I'm getting an error saying "The type name 'Lerp' does not exist in the type 'Vector3'".
var finalPos = new Vector3(0, 0, 90);
var desiredPos = new Vector3.Lerp(wheel.rotation, finalPos, Time.deltaTime);
wheel.rotation = Quaternion.Euler(desiredPos);
Answer by jackmw94 · Nov 20, 2020 at 09:06 AM
The Lerp function within the Vector3 struct is static. This means you don't need an instance of a Vector3 to access it and so you don't need the 'new' keyword as that is there for making new objects via their constructors, like you did the line before.
var desiredPos = Vector3.Lerp(wheel.rotation, finalPos, Time.deltaTime);
I've assumed that this is within a loop or update function, if it's not then we might need some other changes.
Let me know if it's not or if you need anything clarified!
Answer by thevvulf · Nov 20, 2020 at 07:37 PM
@jackmw94 I put that in, but I'm getting an error saying "Argument 1: cannot convert from 'UnityEngine.Quaternion' to 'UnityEngine.Vector3'". Heres the rest of the code if that helps:
using UnityEngine;
public class Flying : MonoBehaviour
{
public float maxHeight;
public float rocketPower;
public Transform[] wheels;
public Rigidbody rb;
void Update()
{
float xPos = transform.position.x;
float yPos = transform.position.y;
if(xPos > -16 && xPos < 30){
foreach(Transform wheel in wheels){
var finalPos = new Vector3(0, 0, 90);
var desiredPos = Vector3.Lerp(wheel.rotation, finalPos, Time.deltaTime);
wheel.rotation = Quaternion.Euler(desiredPos);
}
if(Input.GetKey("space") && yPos < maxHeight){
rb.AddForce(0, rocketPower, 0, ForceMode.Impulse);
}
}
else{
foreach(Transform wheel in wheels){
wheel.rotation = Quaternion.Euler(0, 0, 0);
}
}
}
}
Ah my bad, it should have been Vector3.Lerp(wheel.eulerAngles, finalPos, Time.deltaTime);
Although now I've said that I realise that maybe it would have been better to do the rotation in Quaternions since that's what they're for.
var desiredPos = Quaternion.Lerp(wheel.rotation, Quaternion.Euler(finalPos), Time.deltaTime);
wheel.rotation = desiredPos;
Up to you :)
There we go. Thanks! Did some tweaking and now it looks perfect.
Nice! If you're happy with my help could you accept my answer? :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Help with rotation. 1 Answer
Child of a gameobject lookAt only around Y-axys 1 Answer
knockback 3d c# 1 Answer