Vote count:
0
this is the code for movement when touch on an IOS device in a 2D game i am creating in unity. but when i run it the game loads but when i touch to the right he moves to the left and when i touch to the left he also moves to the left.
i have used ScreenToWorldPoint as told to use but it dont seem to work he will only move one direction
void PlayerWalkMobile()
{
// force by which we will push the player
float force = 0.0f;
// the 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 the touch is " + h.position);
if (position.x > 0)
{
// if the velocity of the player is less than the maxVelocity
if (velocity < maxVelocity)
force = speed;
// turn the player to face right
Vector3 scale = transform.localScale;
scale.x = 1;
transform.localScale = scale;
// animate the walk
animator.SetInteger("walk", 1);
}
else if (position.x < 0)
{
// if the velocity of the player is less than the maxVelocity
if (velocity < maxVelocity)
force = -speed;
// turn the player to face right
Vector3 scale = transform.localScale;
scale.x = -1;
transform.localScale = scale;
// animate the walk
animator.SetInteger("walk", 1);
}
// add force to rigid body to move the player
GetComponent<Rigidbody2D>().AddForce(new Vector2(force, 0));
// set the idle animation
if (h.phase == TouchPhase.Ended)
animator.SetInteger("walk", 0);
} // if Input.touchCount > 0
}
asked 1 min ago
Unity project character only moving one way no matter where you touch on an IOS device
Aucun commentaire:
Enregistrer un commentaire