c# - 2D basic movement UNITY -


currently character works on keyboard, when convert movements touch, through 3 ui buttons (i tried ui image too, success) i'm not succeeding

it goes right, left, , jumps.

how should make follow these instructions:

when user presses directional, character not stop walking until user user releases button, , when press jump player jump.

this script use move through keyboard!

using unityengine; using system.collections;  public class player : monobehaviour {      public float velocity;      public transform player;     private animator animator;      public bool isgrounded;     public float force;      public float jumptime = 0.4f;     public float jumpdelay = 0.4f;     public bool jumped = false;     public transform ground;      private gerenciador gerenciador;      // use initialization     void start ()     {         gerenciador = findobjectoftype (typeof(gerenciador)) gerenciador;         animator = player.getcomponent<animator> ();         gerenciador.startgame ();     }      // update called once per frame     void update ()     {          move ();      }      void move ()     {          isgrounded = physics2d.linecast (this.transform.position, ground.position, 1 << layermask.nametolayer ("floor"));         animator.setfloat ("run", mathf.abs (input.getaxis ("horizontal")));         if (input.getaxisraw ("horizontal") > 0) {              transform.translate (vector2.right * velocity * time.deltatime);             transform.eulerangles = new vector2 (0, 0);         }         if (input.getaxisraw ("horizontal") < 0) {              transform.translate (vector2.right * velocity * time.deltatime);             transform.eulerangles = new vector2 (0, 180);         }          if (input.getbuttondown ("vertical") && isgrounded && !jumped) {              //  rigidbody2d.addforce (transform.up * force);             getcomponent<rigidbody2d> ().addforce (transform.up * force);             jumptime = jumpdelay;             animator.settrigger ("jump");             jumped = true;         }          jumptime -= time.deltatime;          if (jumptime <= 0 && isgrounded && jumped) {              animator.settrigger ("ground");             jumped = false;          }     } } 

c# if possible, remember using canvas ui these buttons =]

for jump action, need button. gameobject->ui->button. replace image own image , click "set native size". re-size button size that's game.

attach button jump button slot script below.

public button jumpbutton;  void jumpbuttoncallback() {     //  rigidbody2d.addforce (transform.up * force);     getcomponent<rigidbody2d>().addforce(transform.up * force);     jumptime = jumpdelay;     animator.settrigger("jump");     jumped = true; }   void onenable(){     //un-register button     jumpbutton.onclick.addlistener(() => jumpbuttoncallback()); }  void ondisable(){     //un-register button     jumpbutton.onclick.removealllisteners(); } 

for left , right movement buttons,you should not use button component them jump button. have implement virtual joystick make realistic. unity have assets called crossplatforminputmanager can that. have import , modify little bit in order use it. watch this understand how import it.

now, can replace input.getaxis , input.getaxisraw functions crossplatforminputmanager.getaxis("horizontal") , crossplatforminputmanager.getaxisraw("horizontal").

if work, can use below make code comptible both mobile , desktop.

#if unity_editor || unity_standalone || unity_webgl //put input.getaxis` , `input.getaxisraw` code here #elif unity_android || unity_ios  //put `crossplatforminputmanager.getaxis("horizontal")` , `crossplatforminputmanager.getaxisraw("horizontal")`. here #endif 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -