Hello, i want to make a little game in which the player moves according to your mouse clicks
(the click determines the direction and the player keeps moving until he hits a wall)
However, with the following code snippet i have an issue in which speed is too high if you click too far and speed is too slow when you click too close.
void Update()
{
if (!isMoving)
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
clickedPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
StartCoroutine(MoveToNextWall());
}
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.transform.gameObject.layer== LayerMask.NameToLayer("Wall"))
{
arrived = true;
}
}
IEnumerator MoveToNextWall()
{
isMoving = true;
Vector3 moveDir = (clickedPos - transform.position).normalized;
Debug.Log(((moveDir / moveDir.magnitude) * speed).magnitude);
while (!arrived)
{
rb.velocity = (moveDir / moveDir.magnitude) * speed;
yield return null;
}
rb.velocity = Vector3.zero;
arrived = false;
isMoving = false;
}
Any help would be appreciated.