android - How to jump character with using JoyStick in Mobile Touch Screen? -
i want jump , move character @ same using 1 joystick. there idea?
in joystick;
- left key allow character move left.
- right key allow character move right.
- up key allow character jump.
currently, character moves left , right using crossplatforminput. want add jump feature up-key of joystick. don't want use jump button.
here codes moving character
public class platformer2dusercontrol : monobehaviour { private platformercharacter2d m_character; private bool m_jump; private void awake() { m_character = getcomponent<platformercharacter2d>(); } private void update() { if (!m_jump) { // read jump input in update button presses aren't missed. m_jump = crossplatforminputmanager.getbuttondown("jump"); } } private void fixedupdate() { // read inputs. bool crouch = input.getkey(keycode.leftcontrol); float h = crossplatforminputmanager.getaxis("horizontal"); // pass parameters character control script. m_character.move(h, crouch, m_jump); m_jump = false; } }
and other one;
private void awake() { // setting references. m_groundcheck = transform.find("groundcheck"); m_ceilingcheck = transform.find("ceilingcheck"); m_anim = getcomponent<animator>(); m_rigidbody2d = getcomponent<rigidbody2d>(); playergraphics = transform.findchild("graphics"); if (playergraphics == null) { debug.logerror("there no 'graphics' object child of player"); } } private void fixedupdate() { bool wasgrounded = m_grounded; m_grounded = false; // player grounded if circlecast groundcheck position hits designated ground // can done using layers instead sample assets not overwrite project settings. collider2d[] colliders = physics2d.overlapcircleall(m_groundcheck.position, k_groundedradius, m_whatisground); (int = 0; < colliders.length; i++) { if (colliders[i].gameobject != gameobject) m_grounded = true; } m_anim.setbool("ground", m_grounded); // set vertical animation m_anim.setfloat("vspeed", m_rigidbody2d.velocity.y); } public void move(float move, bool crouch, bool jump) { // if crouching, check see if character can stand if (!crouch && m_anim.getbool("crouch")) { // if character has ceiling preventing them standing up, keep them crouching if (physics2d.overlapcircle(m_ceilingcheck.position, k_ceilingradius, m_whatisground)) { crouch = true; } } // set whether or not character crouching in animator m_anim.setbool("crouch", crouch); //only control player if grounded or aircontrol turned on if (m_grounded || m_aircontrol) { // reduce speed if crouching crouchspeed multiplier move = (crouch ? move * m_crouchspeed : move); // speed animator parameter set absolute value of horizontal input. m_anim.setfloat("speed", mathf.abs(move)); // move character m_rigidbody2d.velocity = new vector2(move * m_maxspeed, m_rigidbody2d.velocity.y); // if input moving player right , player facing left... if (move > 0 && !m_facingright) { // ... flip player. flip(); } // otherwise if input moving player left , player facing right... else if (move < 0 && m_facingright) { // ... flip player. flip(); } } // if player should jump... if (m_grounded && jump && m_anim.getbool("ground")) { // add vertical force player. m_grounded = false; m_anim.setbool("ground", false); m_rigidbody2d.addforce(new vector2(0f, m_jumpforce)); } } private void flip() { // switch way player labelled facing. m_facingright = !m_facingright; // multiply player's x local scale -1. vector3 thescale = playergraphics.localscale; thescale.x *= -1; playergraphics.localscale = thescale; } }
since crossplatforminputmanager.getaxis("horizontal");
moving left , right crossplatforminputmanager.getaxis ("vertical");
moving up , down.
when crossplatforminputmanager.getaxis ("vertical");
returns value > 0
, up. value < 0
down. when 0
, means not moving or down.
replace update()
function in first script in question code below.
private void update() { if (!m_jump) { //read jump input in update button presses aren't missed. //m_jump = crossplatforminputmanager.getbuttondown("jump"); float checkjump = crossplatforminputmanager.getaxis ("vertical"); if(checkjump>0.3f){ //up m_jump = true; } } }
Comments
Post a Comment