GetAxis Like movement for Touch Input

Updated on April 18, 2017 in [A] 2D
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
1 on April 15, 2017

I am trying to make a 2D game for mobile in which if you press on the left side of the screen the Character goes to the left, and if you press on the right side of the screen the Player goes to the right. How can I make the touch Input feel like Input.GetAxis for the PC? All I can do is make it feel like GetAxisRaw which I do not like.


#if UNITY_STANDALONE || UNITY_WEBPLAYER

direction = Input.GetAxis("Horizontal");

#else
if (Input.touchCount > 0){

if (Input.GetTouch(0).position.x < Screen.width / 2){

direction = -1f;

}

else if (Input.GetTouch(0).position.x > Screen.width / 2){

direction = 1f;

}

}

else{

direction = 0f;

}
#endif

x = direction * speed * Time.deltaTime;

Vector2 newPosition = rb.position + new Vector2(x, upSpeed * Time.deltaTime);

newPosition.x = Mathf.Clamp(newPosition.x, -mapWidth, mapWidth);
rb.MovePosition(newPosition);

rb.transform.Rotate(0f, 0f, rotationSpeed * Time.deltaTime);


Language: C#

  • Liked by
Reply
0 on April 18, 2017

BUMP!

  • Liked by
Reply
Cancel