Jump Thrust(跳跃冲量)

新增一个跳跃向量,把这个跳跃向量给到rigidbody上面,应该在什么时候去修改这个thrustVec呢,应该在onjumpenter’的时候去修改。代码如下:

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

public class ActorController : MonoBehaviour
{

    public GameObject model;
    public PlayerInput pi;
    public float WalkSpeed = 2.0f;//走路速度 可视化控制
    public float runMultiplier = 2.0f;//跑步速度 可视化控制
    public float jumpVelocity = 5.0f;//跳跃冲量 可视化控制


    [SerializeField]//把Animator暂时显示到unity
    private Animator anim;
    private Rigidbody rigid;//Rigidbody 不能在updata中调用 要在Fixedupdata中调用
    private Vector3 planarVec;//用于储存玩家意图
    private Vector3 thrustVec;//跳跃向量

    private bool lockPlanar = false;//是否锁死地面移动

    // Use this for initialization
    void Awake()
    {
        //获取组件
        pi = GetComponent<PlayerInput>();// 获得玩家输入键位
        anim = model.GetComponent<Animator>();//吃一个模型
        rigid = GetComponent<Rigidbody>();//移动位移
    }

    // Update is called once per frame
    void Update()
    {
        //坐标系计算+动画补间旋转
        float targetRunMulti = ((pi.run) ? 2.0f : 1.0f);//走路到跑步切换加入缓动
        anim.SetFloat("forward", pi.Dmag*Mathf.Lerp(anim.GetFloat("forward"),targetRunMulti,0.5f));//把Dup的值喂给Animator里面的forwad
        //添加跳跃
        if (pi.jump)
        {
            anim.SetTrigger("jump");
        }

        if (pi.Dmag > 0.1f)//增加判断 如果按键时间大于0.1秒那么就不转回到前面
        {
            Vector3 targetForward = Vector3.Slerp(model.transform.forward, pi.Dvec, 0.3f);//转身缓动 使模型在一个球面上旋转
            model.transform.forward = targetForward;
        }
        if(lockPlanar == false)
        {
            planarVec = pi.Dmag * model.transform.forward * WalkSpeed * ((pi.run) ? runMultiplier : 1.0f);//如果lockPlanaer等于false就更新planarvec的值,如果等于true那么就锁死在当前值
        }
        //planarVec = pi.Dmag * model.transform.forward*WalkSpeed*((pi.run)?runMultiplier:1.0f);//玩家操作存量大小*当下模型正面=玩家意图 把玩家意图存在这个变量里面
    }
    private void FixedUpdate() //undata是每秒60帧,达到和显示屏一样的刷新速度 而fixedupdata(物理引擎) 每秒50帧
    {
        //rigid.position += planarVec * Time.fixedDeltaTime;//增加一段距离()每秒1个单位  速度乘时间来改位移
        rigid.velocity = new Vector3(planarVec.x, rigid.velocity.y, planarVec.z)+thrustVec;//直接指派速度 + 一个跳跃向量 然后清零跳跃向量
        thrustVec = Vector3.zero;
    }
    public void OnjumpEnter()
    {
        pi.inputEnabled = false;     //跳起来之后不能移动
        //print("OnJump Enter!!!!!!!");//测试FSMOnEnter的讯号有没有发送到此层级
        lockPlanar = true;
        thrustVec = new Vector3(0,jumpVelocity,0);//修改thrustVec
    }
    public void OnjumpExit() 
    {
        pi.inputEnabled = true;     //落地可以移动
        //print("OnJump Exit!!!!!!!");//测试FSMOnExit的讯号有没有发送到此层级
        lockPlanar = false;
    }
}

这样会出现一个问题:下落时会失去下落的动力。

猜你喜欢

转载自blog.csdn.net/weixin_44025382/article/details/84985120