Improving Click-to-Move Movement Script (2D)

Hello there! I’m currently working on a 2d game where one controls player units on a screen by clicking to select them, then choosing another point on the screen for them to move too. So far, I’ve created a script that does this, but only about 20% of the time. Are there any suggestions or tips on how to improve the below system to be more reliable and run smoother? Or any blaring mistakes that are causing the unreliability? Thank you for the help!

using UnityEngine;
 using System.Collections;
 
 public class CellController : MonoBehaviour
 {
     //get sprite's collider component and assign variable
     private CapsuleCollider2D collider2d;
     bool isHighlighted = false;
 
     [SerializeField] float movementSpeed = 5f;
 
     Vector3 clickPosition;
     
     // Start is called before the first frame update
     void Start()
     {
         collider2d = GetComponent<CapsuleCollider2D>();
     }
 
     IEnumerator Move()
     {
         while (isHighlighted == true) {
             if (Input.GetMouseButtonDown (0)){
             // ScreenToWorldPoint
             clickPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3 (0, 0, 10f));
 
             // Log location of click
             Debug.Log(clickPosition);
 
             float xDistance = Mathf.Abs(clickPosition.x - transform.localPosition.y);
             float yDistance = Mathf.Abs(clickPosition.y - transform.localPosition.y);
 
             float moveDistance = (xDistance + yDistance) / 2;
 
             float lerpDuration = moveDistance / movementSpeed;
 
             StartCoroutine(CalculateMove(transform.localPosition, clickPosition, lerpDuration));
             }
             yield return new WaitForSeconds(0.1f);
         }
         yield return new WaitForSeconds(0.1f);
     }
 
     IEnumerator CalculateMove (Vector3 startPos, Vector3 endPos, float seconds)
     {
         float t = 0f;
         while (t <= 1f) {
             t += Time.deltaTime/seconds;
             transform.position = Vector3.Lerp(startPos, endPos, Mathf.SmoothStep(0f, 1f, t));
             yield return new WaitForSeconds(0.1f);
         }
     }
 
     // When clicked on, change state
     // If highlighted, begin coroutine
     void OnMouseDown()
     {
         isHighlighted = !isHighlighted; // toggle
         if (isHighlighted == true){
             Debug.Log("Highlighted!");
             StartCoroutine("Move");
         }
         else
         {
             StopCoroutine("Move");
         }
     }
 }

Hi KingMatthew,

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? Maybe the calculation does not give you the expected results, or something else is wrong.

Hi Nina,
I can see my trophy in right coordination (ie: 3,3) , however when i place the trophy to game area, it appeared in (2,3) …not sure why, the cactus is showing in correct location (previously i did reset cactus parent to 0,0 and adjusted it body and it worked), however no clue on trophy even i’ve reset the parent as (0,0,0) and trophy doesn’t have body with it.

Any clue ?

Have you already animated your trophy? If so, check the animation. Maybe the position is animated as well. In that case, you’ll have to remove the position property from the list in the animation.

Yes, it is already animated, position property has been removed,
these are the info showing for base and top of Trophy.

base:rotation : (0,0,0)
top : (-1.22, 0.63, 0)

as mentioned, after selected Trophy, click on position 4,3 , it will go to 3,3 (Y-axis is correct, but not X-axis)

Did you log the values into your console or did you just assume that you clicked on (4, 3)? Also log the rounded value into your console because if you clicked on (4, 3) but the mouse position was slightly off, it might be that your code rounded the converted mouse position down. That’s impossible to tell just by staring at the code, so you’ll have to check these values during runtime.

Yup, i did log the value to console, and it shows correct value

I noticed when i chose the trophy and placed it to (4,2) , the log console will displayed (4.0, 2.0),
and on screen, it positioned in (3,2) .

from hierachy, the cloned trophy show
transform position (X=4, Y=2),
Top = (-1.22, 0.4545085) ,
Base = (-1.19, 0) ,
and i untick the star, and it showing the position (-1.18, 0.266)

Finally i removed from animation about Top, and redo again , and it work now , now experiencing after trophy added and it doesn’t add the resource value as text…

for resource value, i forgotten on adding event for addStar function, i’m good now, thanks Nina for your patience :slight_smile:

Good job on fixing the problems yourself. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms