unity3d - Unity C# Script Moving a character 2D -
i'm trying make script control character. want character move distance right while alternating left arrow , right arrow inputs. ideally when start game, press right arrow - character moves right 10px, press left arrow - character moves right , pattern continues on alternating , never allowing 2 of same inputs after eachother eg: left arrow left arrow or right arrow right arrow.
ended going this, :d
using unityengine; using system.collections; public class playercontroller : monobehaviour { public float movespeed; public float jumpheight; private keycode lasthitkey; void start() { } void update() { if(input.getkeydown (keycode.space)) { getcomponent<rigidbody2d>().velocity = new vector2(0, jumpheight); } if(input.getkeydown (keycode.d)) { if(lasthitkey == keycode.d) { }else { getcomponent<rigidbody2d>().velocity = new vector2(movespeed, 0); lasthitkey = keycode.d; } } if(input.getkeydown (keycode.a)) { if(lasthitkey == keycode.a) { }else { getcomponent<rigidbody2d>().velocity = new vector2(movespeed, 0); lasthitkey = keycode.a; } } } }
Comments
Post a Comment