本文へスキップ
Unity Tutorial

Move a 3D object to the clicked position in Unity

Use ScreenPointToRay, Physics.Raycast, and MoveTowards to create a beginner-friendly click-to-move controller.

公開日: 2026-05-05#Unity#Raycast#3D#Movement
+
*
+
Coco, the guide character・喜ぶ
Coco
Game dev learning guide
This English lesson uses the same guided screen structure as the Japanese tutorials: a visual start, character guidance, step cards, code, mistakes, and a final checklist.
+
*
+
Hajime, the learner character・混乱
Hajime
Beginner game engine learner
I want to know what to make first, what to check, and how to tell whether I actually understood it.
Coco, the guide character
Move a 3D object to the clicked position in Unity is designed around one visible result. We will keep the first pass focused so the lesson turns into practice quickly.
Hajime, the learner character
So I should finish the exact lesson once before changing it?
Coco, the guide character
Yes. Finish the stable version first, then change one variable, setting, or layout detail to make the idea yours.

Lesson Overview

Finish one working result first

1.

Goal

This tutorial covers one of the most useful beginner patterns in Unity: reading a click on the ground and moving a character toward that point in a readable, controllable way.

2.

Time

30-40 min / Beginner. Keep the first pass small, then change one thing on your own.

3.

Result

A click on the floor sets the next destination.

Before You Start

Check the goal, inputs, and success condition

The lesson is easiest to follow when you know what should happen on screen, which control or editor action triggers it, and what counts as done.

1

This tutorial covers one of the most useful beginner patterns in Unity: reading a click on the ground and moving a character toward that point in a readable, controllable way.

2

Translate a mouse click into a 3D ray using the main camera.

3

A click on the floor sets the next destination.

1Conclusion first

Click-to-move is a great intermediate beginner exercise because it combines input, 3D space, and readable movement logic in one compact pattern. Instead of only reacting to keyboard keys, the game now reads the world, understands where the player clicked, and turns that click into a destination.

The real value of this lesson is not only moving one object. It is learning how several Unity ideas connect: a click begins in screen space, a ray converts it into 3D space, a Raycast tells you what was hit, and smooth movement logic carries the object toward that point. Once that chain is clear, many strategy, action, and simulation interactions become easier to build.

2What you will learn

  • Translate a mouse click into a 3D ray using the main camera.
  • Use Physics.Raycast to detect a valid floor or ground target.
  • Store a destination and move toward it over time instead of teleporting.
  • Separate input reading, target selection, rotation, and movement into readable responsibilities.

3Before you start

  • A floor or plane with a collider so Raycast has something to hit.
  • A player or object with a simple visible mesh that can move across the scene.
  • A LayerMask reserved for the ground, so clicks on the wrong objects do not become movement targets.
  • Basic familiarity with the Unity Inspector and scene testing workflow.

4Step by step

1
Step 1
Cast a ray from the mouse position
Convert the clicked screen position into a 3D ray by using Camera.main.ScreenPointToRay and detect the ground with Physics.Raycast.
2
Step 2
Store the hit point as the next target
When the ray hits your ground layer, cache hit.point and treat it as the destination for your object or player.
3
Step 3
Move and rotate smoothly
Use Vector3.MoveTowards for position and optionally Quaternion.LookRotation with Quaternion.Slerp so the object turns toward the destination naturally.

5Click-to-move controller

Click-to-move controller
csharp
using UnityEngine;

public class ClickMoveController : MonoBehaviour
{
    [SerializeField] private float moveSpeed = 5f;
    [SerializeField] private LayerMask groundLayer;

    private Vector3 targetPosition;
    private bool hasTarget;

    private void Start()
    {
        targetPosition = transform.position;
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out RaycastHit hit, 100f, groundLayer))
            {
                targetPosition = hit.point;
                hasTarget = true;
            }
        }

        if (!hasTarget) return;

        transform.position = Vector3.MoveTowards(
            transform.position,
            new Vector3(targetPosition.x, transform.position.y, targetPosition.z),
            moveSpeed * Time.deltaTime
        );
    }
}
  • ScreenPointToRay converts a 2D mouse position into a 3D line that starts at the camera and goes into the world.
  • The ground layer filter is important because it keeps the lesson predictable. Without it, decorative objects or other colliders can accidentally receive the click.
  • MoveTowards is chosen here because it is easy to read. It gives a stable first result before you graduate to CharacterController, NavMesh, or Rigidbody-based motion.

6Common mistakes

Clicks do nothing even though the code looks correct

The most common cause is that the floor object has no collider, or the LayerMask does not include the floor layer. The ray may be firing correctly, but there is nothing valid to hit.

The object sinks or jumps vertically

This often happens when you use the full hit.point directly even though the character should stay on a flat plane. Flattening the target with the current Y value helps keep beginner movement stable.

The object reaches the point but spins strangely

Rotation bugs often come from trying to rotate toward a direction with nearly zero length. Guarding against tiny vectors and flattening the direction helps avoid noisy or jittery turning.

7Checklist

  • A click on the floor sets the next destination.
  • The object only reacts to the ground layer you intended.
  • Movement speed stays stable because it uses Time.deltaTime.

8Practice ideas

  • Add a marker or particle effect where the player clicked so the destination becomes easier to understand visually.
  • Allow right-click to cancel the current target and stop the character in place.
  • Replace MoveTowards with NavMeshAgent later and compare the difference in pathing behavior.

9FAQ

Why not teleport to the clicked point immediately?

Teleporting confirms that the ray worked, but it does not teach movement control. A stored destination is much closer to real gameplay and introduces a reusable pattern for later systems.

Do I need NavMesh for click-to-move?

Not for the first lesson. NavMesh is very useful later, but starting with plain Transform movement keeps the learning focus on input and hit detection rather than navigation setup.

Why use a LayerMask instead of clicking any collider?

Because beginners usually want intention, not surprises. Restricting the target to a ground layer makes the behavior easier to debug and easier to explain.

Can this work for enemies or NPCs too?

Yes. The same target-and-move pattern can be reused for enemies chasing waypoints, companions following markers, or puzzle objects moving to a selected spot.

10Wrap-up

  • Click-to-move is really a chain of small ideas: screen input, ray creation, hit detection, and smooth world movement.
  • Readable beginner code is often better than advanced systems too early, because it teaches why the interaction works.
  • Once this lesson feels comfortable, you are ready for keyboard movement, target markers, or full NavMesh navigation.

11Character review

Hajime, the learner character
If I can make the result once and explain what changed, I can move on with more confidence.
Coco, the guide character
Exactly. The best next step is one small variation: adjust a value, swap an input, or connect the idea to a tiny scene of your own.
Tiny variation: finish the lesson once exactly as written, then change one value, label, axis, or UI detail. That small change is where understanding starts to stick.

Next tutorials