Search Unity

Bring player to click point

Discussion in '2D' started by PlumeDeMouton, Oct 25, 2021.

  1. PlumeDeMouton

    PlumeDeMouton

    Joined:
    Jul 1, 2021
    Posts:
    13
    Hi,
    I'm quite new to Unity, and I'm trying to bring the player to a clicked point.
    What I've tried to do is in an update function, always move the player towards the last clicked point, and if he has reached it, not move him anymore.
    However this code makes Unity crash. Any idea ?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public Animator animator;
    8.     Vector2 mousePos2D;
    9.     bool hasReachedPoint = false;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {    
    18.         if (Input.GetMouseButtonDown(0))
    19.         {
    20.             Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    21.             Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
    22.         }
    23.         if(mousePos2D.x == transform.position.x && mousePos2D.y == transform.position.y)
    24.         {
    25.             hasReachedPoint = true;
    26.         }
    27.         if (!hasReachedPoint)
    28.         {
    29.             while(mousePos2D.x != transform.position.x)
    30.             {
    31.                 float movespeed = 0.0f;
    32.                 movespeed++;
    33.                 transform.position = new Vector2(transform.position.x + movespeed * Time.deltaTime, transform.position.y);
    34.             }
    35.             while (mousePos2D.y != transform.position.y)
    36.             {
    37.                 float movespeed = 0.0f;
    38.                 movespeed++;
    39.                 transform.position = new Vector2(transform.position.x, transform.position.y + movespeed * Time.deltaTime);
    40.             }
    41.         }
    42.        
    43.     }
    44.  
    45. }
    46.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    You can never ever ever while() loop forever in Unity. Unity locks up 100% solid until your code finishes executing and returns. Your code above never finishes.

    Click-to-move is one of the simplest uses of game input there is, and I could laboriously type out the 27 changes you need to the code above, but I'd probably screw up 23 of them with typos and then you'd get mad at me.

    Instead, do yourself a solid and go check out a few tutorials on click-to-move. My last googling showed there were THOUSANDS of videos for just this exact thing.

    ALSO: never check floating point positions for equality: they're almost NEVER the same. Here's why:

    https://starmanta.gitbooks.io/unitytipsredux/content/floating-point.html