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);
}
}