unity3d - How to Unity create your own button to control the player on mobile device -
i not sure how ,my goal create button control player on plane mobile touch devices.i created button trying make control player failing here player c# script
using unityengine; using system.collections; public class playermovement : monobehaviour { public float speed = 500f, jumpheight = 500f; transform mytrans; rigidbody2d mybody; vector2 movement;//used temp holder variable before applying velocity bool isgrounded = true; void start () { mytrans = this.transform; mybody = this.getcomponent<rigidbody2d>() ; } void update () { //check if player grounded or not isgrounded = physics2d.linecast(mytrans.position, gameobject.find(this.name+"/tag_ground").transform.position, layermask.nametolayer("player")); //just visualization, not needed gameplay debug.drawline (mytrans.position, gameobject.find(this.name+"/tag_ground").transform.position, color.red); //works keyboards , joysticks #if !unity_android && !unity_iphone && !unity_blackberry && !unity_winrt move (input.getaxisraw("horizontal")); if (input.getbuttondown ("jump")) jump (); #endif } // //separate move , jump can called externally touchbuttons // public void move(float horizontal_input) { movement = mybody.velocity; if(isgrounded)//we can move left , right if on ground movement.x = horizontal_input * speed * time.deltatime; //apply movement player mybody.velocity = movement; } public void jump()//we can jump if on ground { if(isgrounded) mybody.velocity += jumpheight * vector2.up * time.deltatime; } } here touch logic script
using unityengine; using system.collections; public class touchlogicv2 : monobehaviour { public static int currtouch = 0;//so other scripts can know touch on screen [hideininspector] public int touch2watch = 64; private new guitexture guitexture; public virtual void update()//if child class uses update, must call base.update(); functionality { //is there touch on screen? if(input.touches.length <= 0) { onnotouches(); } else //if there touch { //loop through the touches on screen for(int = 0; < input.touchcount; i++) { currtouch = i; //executes code current touch (i) on screen if(this.guitexture != null && (this.guitexture.hittest(input.gettouch(i).position))) { //if current touch hits our guitexture, run code if(input.gettouch(i).phase == touchphase.began) { ontouchbegan(); touch2watch = currtouch; } if(input.gettouch(i).phase == touchphase.ended) { ontouchended(); } if(input.gettouch(i).phase == touchphase.moved) { ontouchmoved(); } if(input.gettouch(i).phase == touchphase.stationary) { ontouchstayed(); } } //outside doesn't require touch on guitexture switch(input.gettouch(i).phase) { case touchphase.began: ontouchbegananywhere(); break; case touchphase.ended: ontouchendedanywhere(); break; case touchphase.moved: ontouchmovedanywhere(); break; case touchphase.stationary: ontouchstayedanywhere(); break; } } } } //the default functions, define happen if child doesn't override these functions public virtual void onnotouches(){} public virtual void ontouchbegan(){print (name + " not using ontouchbegan");} public virtual void ontouchended(){} public virtual void ontouchmoved(){} public virtual void ontouchstayed(){} public virtual void ontouchbegananywhere(){} public virtual void ontouchendedanywhere(){} public virtual void ontouchmovedanywhere(){} public virtual void ontouchstayedanywhere(){} }
i following touchlogic tuts struggling move player on mobile.any examples on touch input welcomed give me links examples.thanks
Comments
Post a Comment