Unity 2D Click To Move Tutorial | Unity Tutorial
In this video tutorial, you'll learn how to implement a Click To Move mechanic in your Unity 2D game! We’ll walk you through the process step-by-step, covering everything from setting up your scene to scripting the movement logic. By the end of this tutorial, you'll have a functional character that responds to mouse clicks, allowing for smooth and intuitive navigation.
WATCH FULL TUTORIAL ON YOUTUBE
=========================================================
Unity Projects - ON SALE!!!
1. Water Sort Puzzle 9000 Levels
2. Ball Sort Puzzle 9000 Levels
3. Sling Shot
=========================================================
Script:
PlayerController.cs
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f; // Speed of movement
private Vector3 targetPosition;
private Vector3 originalScale; // Store the original scale
void Start()
{
targetPosition = transform.position; // Initialize targetPosition
originalScale = transform.localScale; // Store the original scale
}
void Update()
{
// Move towards the target position
targetPosition.y = transform.position.y; // Keep the Y position constant
transform.position = Vector3.MoveTowards(transform.position, targetPosition, moveSpeed * Time.deltaTime);
// Check for mouse input
if (Input.GetMouseButtonDown(0))
{
// Get the mouse position in world coordinates
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePosition.z = 0; // Set Z to 0 since we are in 2D
// Set the target position to the mouse position
targetPosition = mousePosition;
// Keep the Y position constant
targetPosition.y = transform.position.y;
// Flip the player sprite based on movement direction
if (targetPosition.x > transform.position.x)
{
// Move right (facing right)
transform.localScale = new Vector3(originalScale.x, originalScale.y, originalScale.z);
}
else if (targetPosition.x < transform.position.x)
{
// Move left (facing left)
transform.localScale = new Vector3(-originalScale.x, originalScale.y, originalScale.z);
}
}
}
}