方法一:
添加刚体,使用刚体位移实现跳跃
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class HeroMove : MonoBehaviour { 2 //---跳起的力量 3 public float JumpGravity = 500f; 4 //---刚体 5 public Rigidbody rg; 6 7 8 private void Start() { 9 //---查找刚体组件 10 11 rg = this.GetComponent<Rigidbody>(); 12 } 13 14 15 16 private void Update(){ 17 18 //---判断是否按下空格 19 if (Input.GetKeyDown(KeyCode.Space) ){ 20 21 //---为刚体的Y赋值一个新的高度,这个高度为跳跃重力,向上的力 22 rg.velocity = new Vector3(rg.velocity.x, JumpGravity * 23 Time.deltaTime,rg.velocity.z); 24 } 25 26 }
方法二:
使用动画效果位移跳跃(缺点,不能控制高度)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class HeroMove : MonoBehaviour { 2 3 //---动画组件应用 4 private Animator animt; 5 //---跳跃动画名,可以是一个或者多个[] 6 public string JumpName; 7 8 private void Start() { 9 //---查找动画组件赋值 10 animt = this.GetComponent<Animator>(); 11 12 } 13 14 15 16 private void Update(){ 17 18 //---判断是否按下空格 19 if (Input.GetKeyDown(KeyCode.Space) ){ 20 //---播放动画跳跃 21 animt.SetBool("JumpName",true); 22 23 } 24 25 }