Just a quick question. How would you code something that would have the same effect as this code, with the smoothing at the end and everything, but without using the horizontal/vertical and without using physics? (using the a and d keys)
function Update ()
{
transform.Translate (Input.GetAxis ("Horizontal"), 0, 0);
}
Thanks!
more info as requested :)
Yes, I want to translate an object based on a button input, but smoothed out.. I want to know how the code works so I can try to apply it to other things, like, smooth scale in/out based on holding the left/right button, or smooth offset a texture based on how long I hold a button, etc.
var speed : float = 3; //just to make it a lil more configurable
function Update ()
{
var horizontal = 0;
if (Input.GetKey(KeyCode.A)) horizontal = -1;
if (Input.GetKey(KeyCode.D)) horizontal += 1;
transform.Translate (horizontal * speed * Time.deltaTime, 0, 0);
}
With smoothing:
var speed : float = 3; //just to make it a lil more configurable
var currentHorizontal : float = 0;
function Update ()
{
var horizontal = 0;
if (Input.GetKey(KeyCode.A)) horizontal = -1;
if (Input.GetKey(KeyCode.D)) horizontal += 1;
currentHorizontal = Mathf.Lerp(currentHorizontal, horizontal, Time.deltaTime);
transform.Translate (currentHorizontal * speed * Time.deltaTime, 0, 0);
}