1.物体正常跳跃与二段跳

1.1 物体正常跳跃

1.1.1 地面(plane)标签设置

1.1.2 物体跳跃代码
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; // 判断是否在地面上 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 (isGround) { //瞬移效果
//transform.Translate(Vector3.up * Time.deltaTime * jumpSpeed); // 实现跳跃效果
rb.AddForce(Vector3.up * jumpSpeed); // 此时物体不在地面上 isGround = false; } } }
private void FixedUpdate() { rb.MovePosition(rb.position + moveAmount); } //

if(collision.gameObject.tag == "Ground") { // 物体在地面上 isGround = true; } } }
1.2 物体二段跳

using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Move : MonoBehaviour { // 移动速度 public float moveSpeed = 5f; //

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) { //瞬移效果 //transform.Translate(Vector3.up * Time.deltaTime *
jumpSpeed); rb.AddForce(Vector3.up * jumpSpeed); // 物体正在一段跳 isJump = true; //

(isDoubleJump&&!isGround&&isJump) { // 再进行一次跳跃操作 rb.AddForce(Vector3.up *
jumpSpeed); // 物体不再一段跳 isJump = false; } } } private void FixedUpdate() {
rb.MovePosition(rb.position + moveAmount); } private void
OnCollisionEnter(Collision collision) { if(collision.gameObject.tag ==
"Ground") { isGround = true; } } }

2.物体的冲刺

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) { //瞬移效果
//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; } } // 按住左shift键实现冲刺效果 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; } } }