c# - How can i add mobile touch for my Dinosaur game on Unity -
i editing google chrome's dinosaur game on unity. want run game on mobile failed add mobile touch. want touch function game. when touch screen jump or start new game. (i don't need duck option.) hope guys me.
using unityengine; using system.collections; public class jumpandduck : monobehaviour { public level level = null; public gameobject ground = null; public collider2d standingcollider = null; public collider2d duckingcollider = null; public audiosource jumpaudiosource = null; public audioclip jumpaudioclip = null; private animator animator; private bool grounded = true; private bool ducking = false; private float jumpvelocity = 0f; private float gravity = 144f; private vector3 startvector; void start() { animator = getcomponent<animator>(); standingcollider.enabled = true; duckingcollider.enabled = false; } void update() { if (grounded) { if (input.getbutton("jump") || input.getaxis("vertical") > 0) { jump(); } else if (input.getaxis("vertical") < 0) { duck(); } else { stand(); } } else { transform.position += jumpvelocity * vector3.up * time.deltatime; jumpvelocity -= gravity * time.deltatime; if (transform.position.y < ground.transform.position.y) { grounded = true; transform.position = startvector; animator.setbool("jumping", false); } else if (3 < transform.position.y && 20 < jumpvelocity) { jumpvelocity = 20; } } } void oncollisionenter2d(collision2d collision) { if (collision.gameobject == ground) { grounded = true; transform.position = startvector; animator.setbool("jumping", false); } } void oncollisionexit2d(collision2d collision) { if (collision.gameobject == ground) { grounded = false; animator.setbool("jumping", true); } } void jump() { if (!grounded) { return; } stand(); if (jumpaudiosource && jumpaudioclip) { jumpaudiosource.playoneshot(jumpaudioclip, 1); } startvector = transform.position; jumpvelocity = 40f + level.mainspeed / 10f; grounded = false; animator.setbool("jumping", true); } void duck() { if (ducking || !grounded) { return; } standingcollider.enabled = false; duckingcollider.enabled = true; ducking = true; animator.setbool("ducking", true); } void stand() { if (!ducking) { return; } standingcollider.enabled = true; duckingcollider.enabled = false; ducking = false; animator.setbool("ducking", false); } }
code , project not mine. working on pc mouse clicks, need on mobile touch function.
you can use code
using unityengine; using system.collections; public class exampleclass : monobehaviour { void update() { if (input.getmousebuttondown(0)) debug.log("pressed left click."); if (input.getmousebuttondown(1)) debug.log("pressed right click."); if (input.getmousebuttondown(2)) debug.log("pressed middle click."); } }
you need call function update function, since state gets reset each frame. not return true until user has released mouse button , pressed again. button values 0 left button, 1 right button, 2 middle button.
url: https://docs.unity3d.com/scriptreference/input.getmousebuttondown.html
Comments
Post a Comment