Point & Click 2D Movement Unity Tutorial

Опубликовано: 17 Июнь 2026
на канале: Lendrigan Games
6,159
34

FREE ASSETS FOR TEACHING YOURSELF ANIMATION
https://drive.google.com/drive/folder...

("Angle brackets aren't allowed in your description," so if you manage to copy/paste this code, you'll need to change the unicode "less than" and "greater than" characters.)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class [Insert Script Name Here] : MonoBehaviour {
public float speedX = 100;
public float speedY = 100;
public Rigidbody2D rb;
public Vector2 destination;
public Vector2 place;
public Vector2 journey;
public Vector2 gap;

void Start () {
rb = GetComponent⋖Rigidbody2D⋗();
place = rb.transform.position;
}

void Update () {
if (Input.GetMouseButtonDown (0)) {
destination = Camera.main.ScreenToWorldPoint (Input.mousePosition);
journey.x = destination.x - place.x;
journey.y = destination.y - place.y;
rb.velocity = new Vector2 (journey.normalized.x * speedX * Time.deltaTime, journey.normalized.y * speedY * Time.deltaTime);
}
}
void LateUpdate (){
place = rb.transform.position;
gap.x = Mathf.Abs (destination.x) - Mathf.Abs (place.x);
gap.y = Mathf.Abs (destination.y) - Mathf.Abs (place.y);
if (.05 ⋗ Mathf.Abs (gap.x))
if (.05 ⋗ Mathf.Abs (gap.y))
rb.velocity = new Vector2 (0,0);
}
}

_____________________________________________________
Some Sources:

Player Controller Tutorial
https://unity3d.com/learn/tutorials/p...

https://docs.unity3d.com/ScriptRefere...

I mis-remembered "Quarag."
https://forum.unity.com/threads/unity...

https://docs.unity3d.com/ScriptRefere...

https://docs.unity3d.com/ScriptRefere...

Update: Near the end, I was trying to write a "And" conditional statement in order to highlight that Unity doesn't have them anymore. Of course, the real problem was my own inexperience.
if ((.05 ⋗ Mathf.Abs (gap.x)) & (.05 ⋗ Mathf.Abs (gap.y)))
rb.velocity = new Vector2 (0,0);