filmov
tv
Unity Game Development | CLICK TO MOVE Functionality for 3D Games 🕹️ Unity 3D Tutorial
Показать описание
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:
Комментарии