- Home /
Question by
Omer.Hussain · Dec 30, 2013 at 11:47 AM ·
movementtransformlerp
Help using arrow keys to move
i have the following code, in this code we move the object left and right, what i am trying to do is to move our object right left and middle with just left and right arrow key. i am new kindly help me out. Regards
#pragma strict
public var smooth : float;
private var newPosition : Vector3;
private var newIntensity : float;
private var newColour : Color;
function Awake ()
{
newPosition = transform.position;
}
function Update ()
{
PositionChanging();
}
function PositionChanging ()
{
var positionM : Vector3 = new Vector3(39.90576,1.292813,1.095224);
var positionR : Vector3 = new Vector3(39.90576,1.292813,7.095224);
var positionL : Vector3 = new Vector3(39.90576,1.292813,-3.095224);
if(Input.GetKeyDown("right"))
newPosition = positionR;
if(Input.GetKeyDown("left"))
newPosition = positionL;
transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
ssDebug.Log("v@ " +transform.position);
}
Comment
Best Answer
Answer by vargata · Dec 30, 2013 at 11:54 AM
if(transform.position==positionR)
{
if(Input.GetKeyDown("left"))
newPosition = positionM;
}
else if(transform.position==positionM)
{
if(Input.GetKeyDown("left"))
newPosition = positionL;
if(Input.GetKeyDown("right"))
newPosition = positionR;
}
else if(transform.position==positionL)
{
if(Input.GetKeyDown("right"))
newPosition = positionM;
}
it should work if I understood your question first you have to check which position it is and then move it from that position.