- Home /
How can I find the velocity on a rigidbody which moves using Lerp?
Hello,
I need some help with finding the velocity on an object which moves using a Vector.Lerp. It seems that if you move an object the way stated, a velocity on the rigidbody is not applied.
This seems strange to me as the lerp uses a time in which it moves the object between two points.
Is there something simple that i have missed, or is there a way to find the velocity of an object which moves using lerp?
Here is some sample code, never mind some of the bad scripting since it's only a test!
Thanks in advance!
using UnityEngine;
using System.Collections;
public class MyPlayerController : MonoBehaviour
{
public Transform myTransform;
public Vector2 currentLocation;
public Vector2 newLocation;
public float newLocationY;
public float smooth;
public float nextMove = 0.1f;
public float moveRate = 0.1f;
public float tilt;
void Start()
{
myTransform = transform;
}
void Update()
{
currentLocation = transform.position;
if(Input.GetKeyDown(KeyCode.LeftArrow) && Time.time > nextMove && myTransform.position.y < 8f)
{
nextMove = Time.time + moveRate;
newLocationY = currentLocation.y + 3f;
newLocation = new Vector2(0, newLocationY);
}
else if(Input.GetKeyDown(KeyCode.RightArrow) && Time.time > nextMove && myTransform.position.y > -8f)
{
nextMove = Time.time + moveRate;
newLocationY = currentLocation.y -3f;
newLocation = new Vector2(0, newLocationY);
}
//Move
myTransform.position = Vector2.Lerp(currentLocation, newLocation, smooth * Time.deltaTime);
//Rotate
//!
//!This is always nort!
myTransform.rotation = Quaternion.Euler(0, 0, rigidbody2D.velocity.y * -tilt);
//This ALWAYS displays (0.0, 0.0)
Debug.Log(rigidbody2D.velocity);
}
}
Answer by Owen-Reynolds · Feb 01, 2014 at 09:01 PM
Velocity only applies to the built-in physics system to let the ball fall, bounce and roll on it's own. It sort of means "how much is the automatic movement?"
So, if you're moving it yourself, with Lerp or however, velocity should be zero. If it wasn't zero, the ball would always move a little extra "randomly" from where you put it.
Imagine you're letting physics drop a ball (using a rigidbody.) It gains speed, falls, and the Y velocity auto-increases. When it hits and bounces, Y-velocity flips and is reduced (based on how bouncy it is.) OK, now imagine your code 1-time teleports the ball sideways by x=2 in a single frame. That doesn't change velocity at all. The ball keeps falling at the same speed, from the new position, like a RoadRunner cartoon. Or the old dungeon teleporter trap at the bottom of a pit.
You can compute velocity yourself: new position minus old position. In line 41. currentLocation is now where you used to be, so: Vector3 vel = transform.position - currentLocation;
. You might think "but that's just how much I moved!" But that's the definition of velocity.
Wow, thanks for the very accurate and well explained answer! The reason I got so confused was that in Unity's "Space Shooter" tutorial, they use about the same setup for moving the player, but without the lerp. But I will take another look at the code with this new information, thanks again!
Your answer
Follow this Question
Related Questions
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
Add a wind vector to local velocity for calculating wind speed 2 Answers
rigidBody velocity acting up 2 Answers
Rigidbody velocity capped at 0.4 1 Answer
How do i add a boost in velocity to a 2D ball when you press it? 1 Answer