1. Create a ground (plane)
click unity On the navigation bar GameObeject, choice 3D Obejct, Reselection plane You can create a ground .
2. Create a cube (cube) Used to replace players (player)
2.1 Create cube
click unity On the navigation bar GameObeject, choice 3D Obejct, Reselection cube You can create a cube .
2.2 Give cube attributes
Give cube rigid body (rigidbody) attribute , Make the cube have gravity , General physical properties such as collision .
click Hierarchy Previously created in cube, Click again inspector Medium Add component search Rigidbody You can add it successfully .
It has gravity by default .
2.3 Move the cube through the script
unity The principle of controlling object movement in is through code ( script ), Change the of an object Transform attribute
Position Represents the three-dimensional position of an object ,Rotation Represents the rotation angle of the object ,Scale Represents the scale of the object
2.3.1 Create script file
stay Assets Create a new folder in Scripts.( stay Assets Right click in , click create, Click again folder)
stay Scripts Right click in , click create, Click again C# Scripts
2.3.2 Script file implementation
Click Create move file .
using System.Collections; using System.Collections.Generic; using UnityEngine;
// Note here Move Need to correspond to your script name public class Move : MonoBehaviour { // Control the moving speed of objects public
float moveSpeed = 5f; Vector3 moveAmount; // Rigid body attributes Rigidbody rb; // Function executed at the first frame
void Start() { // Gets the rigid body attributes of the current object rb = GetComponent<Rigidbody>(); } // The function executed for each frame of the refresh
void Update() { // Monitor mouse events Vector3 moveDir = new
Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")).normalized;
// Moving distance = Moving direction * Moving speed * Time per frame moveAmount = moveDir * moveSpeed *
Time.deltaTime; } private void FixedUpdate() { // Object movement
rb.MovePosition(rb.position + moveAmount); } }
Horizontal and Vertical Location of :Edit Lower ProjectSetting
2.3.3 Mounting of script files
click cube, Click again Add Component, Search script name Move that will do .
2.4 Rotation and jumping of objects
The script file is as follows :
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Move : MonoBehaviour { public float moveSpeed = 5f; // Control rotation speed
public float rotateSpeed = 5f; // Control jump height public float jumpSpeed = 8f; Vector3
moveAmount; Rigidbody rb; void Start() { rb = GetComponent<Rigidbody>(); } void
Update() { Vector3 moveDir = new Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical")).normalized; moveAmount = moveDir * moveSpeed *
Time.deltaTime; // Get rotation direction Vector3 targetDir = Vector3.Slerp(transform.forward,
moveDir, rotateSpeed * Time.deltaTime); // Realize rotation transform.rotation =
Quaternion.LookRotation(targetDir); // Realize key jumping if (Input.GetButton("Jump")) {
transform.Translate(Vector3.up * Time.deltaTime * jumpSpeed); } } private void
FixedUpdate() { rb.MovePosition(rb.position + moveAmount); } }
Technology