I have a script that is supposed to make a my player "lean" around a wall or other surface. It is supposed to move the upper of two cubes (the bottom cube is the upper one's parent) about .15 units to the right of the bottom cube when the "Lean" key is pressed. When the key is released, the cube is supposed to return back to the center of the cube. I can make the cube move to the right when the lean key is pressed, but I can't make the cube return once the key is released. It instead slides further to the right. Here is the code:
var LeanAmt : float = 0.15;
var LeanSpeed : float = 5.0;
var Lean : float = 0;//Location player's torso is while leaning
function Update ()
{
//Lean by sliding "torso" to the side and adding minor rotation
if(Input.GetButton("Lean"))
{
if(Lean < LeanAmt)//Check player isn't already leaning
{
Lean += LeanSpeed * Time.deltaTime;
transform.Translate(Lean,0,0);
}
}
if (!(Input.GetButton("Lean")) && Lean > 0)//Check player hasn't already returned
{
Lean -= LeanSpeed * Time.deltaTime;
transform.Translate(Lean,0,0);
}
}