c# - Unity project character only moving one way no matter where you touch on an IOS device -
this code movement when touch on ios device in 2d game creating in unity. when run game loads when touch right moves left , when touch left moves left.
i have used screentoworldpoint told use dont seem work move 1 direction
void playerwalkmobile() { // force push player float force = 0.0f; // players velocity float velocity = mathf.abs(getcomponent<rigidbody2d>().velocity.x); if (input.touchcount > 0) { counttouches++; touch h = input.touches[0]; vector2 position = camera.main.screentoworldpoint(new vector2(h.position.x, h.position.y)); debug.log("the postion of touch " + h.position); if (position.x > 0) { // if velocity of player less maxvelocity if (velocity < maxvelocity) force = speed; // turn player face right vector3 scale = transform.localscale; scale.x = 1; transform.localscale = scale; // animate walk animator.setinteger("walk", 1); } else if (position.x < 0) { // if velocity of player less maxvelocity if (velocity < maxvelocity) force = -speed; // turn player face right vector3 scale = transform.localscale; scale.x = -1; transform.localscale = scale; // animate walk animator.setinteger("walk", 1); } // add force rigid body move player getcomponent<rigidbody2d>().addforce(new vector2(force, 0)); // set idle animation if (h.phase == touchphase.ended) animator.setinteger("walk", 0); } // if input.touchcount > 0 }
in debug touches on place 0.0 far left of screen everywhere else minus position of touch (-1093.7, 1048.3)
i thought
vector2 position = camera.main.screentoworldpoint(new vector2(h.position.x, h.position.y));
was supposed make 0.0 centre
Comments
Post a Comment