1. Normal jumping and two-stage jumping of objects

1.1 Normal jump of object

Restrict objects to jump only once when the jump key is pressed . Instead of being able to jump indefinitely .

The specific implementation idea is : Label the ground , Detect whether the object collides with the ground .

1.1.1 ground (plane) Label settings

We want objects to jump normally , You need to add a label to the ground .

Click on the ground plane Click again tag Below AddTag option

stay AddTag Page click ➕ number , establish Ground label

After creation , stay plane Select as in the upper label Ground

 

1.1.2 Object jump code
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Move : MonoBehaviour { public float moveSpeed = 5f; public float
rotateSpeed = 5f; public float jumpSpeed = 8f; // Judge whether it is on the ground private bool
isGround = true; 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; Vector3 targetDir =
Vector3.Slerp(transform.forward, moveDir, rotateSpeed * Time.deltaTime);
transform.rotation = Quaternion.LookRotation(targetDir); if
(Input.GetButtonDown("Jump")) { // If the object is on the ground if (isGround) { // Blinking effect
//transform.Translate(Vector3.up * Time.deltaTime * jumpSpeed); // Achieve jumping effect
rb.AddForce(Vector3.up * jumpSpeed); // At this time, the object is not on the ground isGround = false; } } }
private void FixedUpdate() { rb.MovePosition(rb.position + moveAmount); } //
collision detection private void OnCollisionEnter(Collision collision) { // The object touches the ground
if(collision.gameObject.tag == "Ground") { // Objects on the ground isGround = true; } } }
1.2 Object two-stage jump

Realization of two-stage jump : Add two variables , Can a control object make a two-stage jump , A monitoring object is jumping

Conditions for two-stage jump of objects : When the object is not on the ground && The object can make a two-stage jump && The object is jumping for a while .

Object two segment jump code :
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Move : MonoBehaviour { // Moving speed public float moveSpeed = 5f; //
Rotation speed public float rotateSpeed = 5f; // Jump speed public float jumpSpeed = 8f; //
Is it on the ground private bool isGround = true; // Whether the object can make a two-stage jump , Because it is set to be steerable , So for public public
bool isDoubleJump = false; // Whether the object is jumping in a section private bool isJump = true; 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; Vector3 targetDir = Vector3.Slerp(transform.forward, moveDir,
rotateSpeed * Time.deltaTime); transform.rotation =
Quaternion.LookRotation(targetDir); if (Input.GetButtonDown("Jump")) { if
(isGround) { // Blinking effect //transform.Translate(Vector3.up * Time.deltaTime *
jumpSpeed); rb.AddForce(Vector3.up * jumpSpeed); // The object is jumping for a while isJump = true; //
The object is not on the ground isGround = false; } // If the object can make a two-stage jump && The object is not on the ground && The object is jumping else if
(isDoubleJump&&!isGround&&isJump) { // Perform another jump operation rb.AddForce(Vector3.up *
jumpSpeed); // The object no longer jumps isJump = false; } } } private void FixedUpdate() {
rb.MovePosition(rb.position + moveAmount); } private void
OnCollisionEnter(Collision collision) { if(collision.gameObject.tag ==
"Ground") { isGround = true; } } }
Remember to tick here , The object can make a second jump .

 2. Object sprint

set up sprintSpeed Change the speed of the object when pressing the sprint key to sprintSpeed that will do .
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Move : MonoBehaviour { public float moveSpeed = 5f; public float
rotateSpeed = 5f; public float jumpSpeed = 8f; public float sprintSpeed = 10f;
private bool isGround = true; public bool isDoubleJump = false; private bool
isJump = true; 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; Vector3 targetDir =
Vector3.Slerp(transform.forward, moveDir, rotateSpeed * Time.deltaTime);
transform.rotation = Quaternion.LookRotation(targetDir); if
(Input.GetButtonDown("Jump")) { if (isGround) { // Blinking effect
//transform.Translate(Vector3.up * Time.deltaTime * jumpSpeed);
rb.AddForce(Vector3.up * jumpSpeed); isJump = true; isGround = false; } else if
(isDoubleJump&&!isGround&&isJump) { rb.AddForce(Vector3.up * jumpSpeed); isJump
= false; } } // Press and hold left shift Key to achieve sprint effect if (Input.GetKey(KeyCode.LeftShift)) {
moveAmount = moveDir * sprintSpeed * Time.deltaTime; } } private void
FixedUpdate() { rb.MovePosition(rb.position + moveAmount); } private void
OnCollisionEnter(Collision collision) { if(collision.gameObject.tag ==
"Ground") { isGround = true; } } }

Technology
©2019-2020 Toolsou All rights reserved,
C++ of string of compare usage Python Study notes ( one )evo Tool usage problems ——Degenerate covariance rank, Umeyama alignment is not possibleRISC-V_GD32VF103-TIMER0 timer interrupt java Array subscript variable _Java Basic grammar : array be based on stm32 Control four-wheel trolley motor drive ( one ) be based on redis Design of liking function Software engineering career planning mysql Query random data by conditions _MySQL Random query of several qualified records centos7 install RabbitMq