Unity Game Development | CLICK TO MOVE Functionality for 3D Games 🕹️ Unity 3D Tutorial

preview_player
Показать описание

In this Unity game development tutorial, you will learn how to move the character using a mouse click in Unity. This type of movement is commonly used in RTS (Real-time Strategy games), RPG (Role-playing Games), TPS Games.

Prerequisites for this Unity 3D Tutorial:

Basic Knowledge of C# & Unity Game Engine
NavMesh Concept
Raycasting
Create a Character Movement Demo Project
Open Unity hub.
Create a new Unity project. ( Here I am using Unity 2019.4.5f1 )
Set Project name & location.
New Unity Project
Setup 3d Environment
Download this free 3d Environment from the Unity Store.
3D Environment Asset

Download this free 3d Character from the Unity Store.
3D Character Pack Asset

After Downloading, import both assets in this unity project.
Open the "Demo_Scene_1" scene of the Toon Gas station asset as below.
Select Environment scene
Add 3D characters in the scene.
Select a character from the folder and drag it to the current scene.
You will find the location of the character as seen in the image below.
Select Character prefab
Character Look
Rename this character object to "Player".
Change the properties of the Player object as below.
set Character's position & scale
Set camera position & rotation for Top-Down view as below image.
Set Camera''s position & rotation
Setup Navigation Agent

Here we are using the Navigation concept of unity that uses to navigate player and movement.

All road, Terrain ground plane, and the obstacle cubes are marked as static as below
set all objects as static

✅ Confirm Box

Go to Window - AI - Navigation and open up the Navigation panel.
Go to the Bake menu and click the Bake button at the bottom.
Navigation Bake process
 After some time, you should see a blue color navigation mesh appear on the scene window.
Character "Player" will able to move only in the blue part.
Nav Mesh Display
Character Movement using Raycasting & Nav Mesh Agent
Add a Nav Mesh Agent to the player object by selecting the player and go to Component - Navigation - Nav Mesh Agent.
This component helps to find the shortest path and moves the character to its destination.
After that, right-click on the Project panel and select Create - C# Script.
Name the C# script as "CharacterMovementScript".
Assign this script to the Player object by drag-and-drop the script onto the player.
Then, double click the script to open up Visual Studio.
In the code, If the left mouse button is clicked then cast a ray from the position of mouse cursor on-screen toward the 3D scene.
If the ray hits anything, Unity will return the location (hit point) which you can use to set the NavMesh Agent's destination.
Assign the Player to the Agent property and the scene camera to the Camera property on its Player script.

✅ Output:

Here, In the output without any animation, the player looks like a static object. So we add some animation.

✅ Applying animation to the player character

In the Project Window, Create an animator controller file to manage animation of the Player Character as below.
Assign this new animator controller to the animator property of the Player object.
For opening the Animator window, Go to Window-Animation-Animator.
Assign Idle and run an animation clip from the animation folder to the animator controller as below image.
Create a transition between animation clips as below.
Change properties Transition arrow
Disable HasExitTime checkbox.
Add Conditions
Create a bool parameter in the animator controller window that uses to put conditions on animation clips. We will discuss more about in coding part.
Open "CharacterMovementScript" in visual studio.
Create an Animator controller variable in the script that is used to handle animation runtime.

#Unity #UnityTutorial #Unity3DTutorial

Check out the article including the code used in this Unity Game Development video:

Рекомендации по теме
Комментарии
Автор

Finally some1 who has the code in code form not a screenshot, thank you soooo much!

take-AK
Автор

it finally worked, after a sh*t-ton of tutorials, it finally worked + you gave me the script for the next thing that i was going to search (the camera script). YOU ARE F*CKING AWESOME, thank you so much

andruaj
Автор

Works from the first atempt. Thank you!

sviatoslavkovzik
Автор

Thank you very much ! A well-explained and very helpful video

sandraeise
Автор

I subscribe to you because its the only tutorial that works with my PC. Keep it up, man!

nguoixauxi
Автор

Gonna enjoy your channel, Mister.
Please, do more sort of this style videogames.
Thanks.

Ochenter
Автор

Nice video, Mister. I will share this to my friends, they are interest in making video game too !

pthyst
Автор

How about a tutorial on movement that does not use navmeshagent...

VKachan
Автор

Thanks for tutorial, do you plan to show more complicated part? Adding kick die animation and enemy ai

toslytodie
Автор

I have a question that Can I implement this for mobile or not, Thanks a lot.

hmm
Автор

i tried this. when i switched an animation because it was ugly, i repeated the steps. now, it will go from idle to running - but when it reaches the destination, it doesnt go back to idle... just runs in a circle. any advice?

moshGIBSON
Автор

the cam it not following on own video bro

raelroque
Автор

The script don't inherit a native class that can manage a script :(

tomslick
Автор

Every frame you overwrite a variable. This is not the best option.
If you want use if esle to overwrite use this
x = if y > z ? true : false;

argotroll