How to get a velocity to use as a lerp time
Hello,
I am making a script that makes my camera tilt on the sides when i strafe, on top of that i would like to have my tilt to reset at the original pose when i hit a wall. So my guess was that i need to get my Player's velocity to use it as a lerp time float, not noticing how stupid it was it acually kind of worked but i guess just getting the velocity and detecting only when the player dont move is a better choice but it leaves me with the same problem: How can i use a vector3 as a float value ? i did notice that my playerVelocity Vector worked as a float if would specify the axis when i play with this Lerp i can tilt only facing some directions, some dont work and some makes my camera tilt jerky. Any ideas ? ===========================================================================
void LateUpdate()
{
Vector3 playerVelocity = GameObject.Find("Player").GetComponent<Rigidbody>().velocity;
float zvel = playerVelocity.x * playerVelocity.z;
/* float t = currentLerpTime / lerpTime;
t = Mathf.Sin(t * Mathf.PI * 0.5f);
//increment timer once per frame
currentLerpTime += Time.deltaTime;
if (currentLerpTime > lerpTime)
{
currentLerpTime = lerpTime;
}
float perc = currentLerpTime / lerpTime;
*/
if (Input.GetAxisRaw("Horizontal") < 0)
{
leftTilt();
}
if (Input.GetAxisRaw("Horizontal") > 0)
{
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y,
-2.5f), Time.deltaTime * lerpTime * zvel);
}
else if (Input.GetAxisRaw("Horizontal") == 0)
{
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0), Time.deltaTime * lerpTime * zvel);
}
}
void leftTilt()
{
Vector3 playerVelocity = GameObject.Find("Player").GetComponent<Rigidbody>().velocity;
float zvel = playerVelocity.z;
float xvel = playerVelocity.x;
float global = xvel * zvel;
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y,
2.5f), Time.deltaTime * lerpTime * global);
if (zvel <= 0)
{
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0), Time.deltaTime * lerpTime);
}
}
To know the velocity (speed) of a Rigidbody as a float value use:
float Speed = GameObject.Find("Player").GetComponent<Rigidbody>().velocity.magnitude;
Your answer