I am currently attempting to make an iOS game where the player has to climb a mountain using holds (another game object) where the player jumps to. I am trying to figure out a way to make the player move from its current position to the position of a hold based on a single touch input, and it would be nice to have the move at a slower rate than just a single frame, which is where I am at right now.
public GameObject player;
public GameObject ground;
public float speed = 10f;
void Update()
{
if (Input.touchCount == 1)
{
Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
Vector2 touchPos = new Vector2(wp.x, wp.y);
if (GetComponent<Collider2D>() == Physics2D.OverlapPoint(touchPos))
{
while (player.transform.position != transform.position)
{
Climb();
}
}
}
}
void Climb()
{
player.transform.position = Vector2.MoveTowards(player.transform.position, transform.position, speed * Time.deltaTime);
}
As soon as I touch the hold, the player seemingly teleports to the hold because it’s being performed in a single frame. Is there any way to have an object move outside of the update method?