Teach you how to use Unity to make a simple parkour game

Teach you how to use Unity to make a simple parkour game

Preface

In fact, it is not difficult to make games with Unity. If you want to learn, then I suggest that you find interest in making a simple parkour game, because if you keep learning some unnecessary syntax from the beginning, it will make you You lose confidence and motivation to learn Unity at the beginning, so if you first learn how to make a simple parkour map, then you will know how simple Unity is, then you will have the motivation to learn, and like this It also makes it easier for you to remember how the code is written.

positive

Prepare materials

Unity (any version will do)
Computer (as long as it can withstand it)

First create a project, you can name it whatever you want.
I will name it Simple Parkour Game.
This time I want to make a simple Parkour game. All we need to do is implement
The protagonist moves, the camera follows, and the parkour ground is built by oneselfThese most important functions
Insert image description here
open the project and create a Cube in the Hierarchy bar on the left
Right click->3D Object->Cube
You can name it whatever you want. I named it chushen_dot
directly and then named a few more.CubeNamed other_dot_numberBe sure to use English when naming, do not use Chinese, otherwise an error will be reported.
I created it here5After that
, we will create another Cube and name it zhong_dotThis block is used as the end block
Finally, create a Cube and name it playerThis block is used as our player block

Scene building

Build the parkour scene according to your own preferences, or you can do it like me.
Insert image description here
The color of the blocks actually depends on how many Materials you want to create.
Right click- > Create- > Material
Insert image description here
Then process the colors corresponding to your own preferences, and finally drag these materials to the corresponding squares in the scene

Note that the camera here must face the block named player from a third-person perspective , otherwise the code will be written and the running effect will match the real result.
Insert image description here
You can use the move tool and the rotation tool to achieve this.
Insert image description here

Assign attributes to blocks

Because the block named player needs to have gravity in physicsnatural declineattribute, so we add a Rigidbody component to it so that it can fall naturally.
Click player-> on the rightInspectorSlide the column to the bottom->Add Component->Search for Rigidbody- > Rigidbody so that we giveplayer cubeAdded Rigidbody component

Write code

existAssetsCreate a C# code file named Camera_move.cs and Player_move.cs in the folder. First open the code file of Carmera_move.cs . As you know from the file name, this code file contains the code for the camera to follow the file.

public GameObject Target; //用来接收跟随摄像头跟随的方块
public float s = 2f;  //速度
Vector3 distance;  //用来处理坐标之类的数据

In the **void Start(){}** function write

distance = transform.position - Target.transform.position; 

Used to handle the position of the camera relative to the player block
. Next, delete the original

void Update(){
    
    }

Change to

void LateUpdate(){
    
    }

Add in it

transform.position = Vector3.Lerp(Target.transform.position + distance, transform.position, Time.deltaTime * s); //此代码可以计算出摄像机在移动时保证撒丝滑坐标
transform.LookAt(Target.transform.position);  //摄像头面对玩家

The LateUpdate function is called after all Update functions have been executed. It is usually used to handle the camera's following logic. Since the camera's following operation needs to be performed after the game object moves, placing the camera's following logic in the LateUpdate function can ensure that the camera can always follow the game object.

Summarizing all the codes is as follows

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Camera_move : MonoBehaviour
{
    
    
    public GameObject Target;
    public float s = 2f;
    Vector3 distance;
    // Start is called before the first frame update
    void Start()
    {
    
    
        distance = transform.position - Target.transform.position; 
    }

    // Update is called once per frame
    void LateUpdate()
    {
    
    
        transform.position = Vector3.Lerp(Target.transform.position + distance, transform.position, Time.deltaTime * s);
        transform.LookAt(Target.transform.position);
        
    }
}

Save it, close the code and go back to Unity to compile it.
Let's continue writing.
Next we open Player_move.cs

public float moveSpeed = 10f; //移动速度
public float turnSpeed = 100f;  //旋转速度
public float jumpSpeed = 10f;  //跳跃速度

delete

void Start(){
    
    }

exist

void Update(){
    
    }

Add inside

if (Input.GetKey(KeyCode.W)) //判断按下的按键是否是W
{
    
    
    transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S)) //判断按下的按键是否是S
{
    
    
    transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))//判断按下的按键是否是A
{
    
    
    transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))//判断按下的按键是否是D
{
    
    
    transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.Space))//判断按下的按键是否是空格
{
    
    
    transform.Translate(Vector3.up * jumpSpeed * Time.deltaTime);
}

Save, return to Unity, run the program, and you will find
Success

Conclusion

In this article we introduced how to make a simple parkour game, but at this point someone will ask

A: Why is your game not over?
Answer: Actually, it’s not that I don’t want to introduce it, it’s just that I really don’t know how to write code, so I didn’t make the game end.

If anyone knows how to collide and end the game, please tell me in the comment area and we will learn together!

Guess you like

Origin blog.csdn.net/SMG_DSG/article/details/132532441