movement - Unity3D move a sprite when pressing a key -
i'm trying move sprite when hit w
key. have been using tutorial found on youtube , don't seem able make work. here code. perhaps i'm having problem transform; can see x position increments sprite doesn't anything.
#pragma strict var startpoint : vector3; var endpoint : vector3; var speed : float; private var increment : float; var ismoving : boolean; function start () { startpoint = transform.position; endpoint = transform.position; } function update () { if (increment <= 1 && ismoving == true) { increment += speed/100; debug.log("moving"); } else { ismoving = false; debug.log("stopped"); } if (ismoving == true) transform.position = vector3.lerp(startpoint, endpoint, increment); if (input.getkey("w") && ismoving == false) { increment = 0; ismoving = true; startpoint = transform.position; endpoint = new vector3(transform.position.x, transform.position.y, transform.position.z + 1); } if (input.getkey("s") && ismoving == false) { increment = 0; ismoving = true; startpoint = transform.position; endpoint = new vector3(transform.position.x, transform.position.y, transform.position.z - 1); } }
try this:
function update () { if (input.getkeydown(keycode.w)) { increment += speed / 100; transform.position = vector3.lerp(transform.position, transform.position + vector3.forward, increment); } if (input.getkeydown(keycode.z)) { increment += speed / 100; transform.position = vector3.lerp(transform.position, transform.position + vector3.back, increment); } }
also, check speed not 0 in inspector. if wanna continuous movement change input.getkeydown input.getkey.
Comments
Post a Comment