Trigger Once Signal(一次性触发控制)

实现jump trigger:
声明jump玩家键入控制角色
思路:jump与last jump是否不一样 如果不一样玩家则按下jump指令,否则没有按 .
代码实现:

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

public class PlayerInput : MonoBehaviour
{
    [Header("==== Key settings ====")]
    public string keyUp = "w";
    public string keyDown = "s";
    public string keyLeft = "a";
    public string keyRight = "d";
    public string keyA;
    public string keyB;
    public string keyC;
    public string keyD;

    [Header("==== Output signal ====")]
    public float Dup;
    public float Dright;
    public float Dmag;
    public Vector3 Dvec;
    //1.pressing signal   按压式
    public bool run;
    //2.trigger once signal  一次性触发
    public bool jump;//按后发
    private bool lastjump;//判断是否按 和jump一样就是没按 如果不一样就是按了
    //3.double trigger 

    [Header("==== Others ====")]
    public bool inputEnabled = true;

    private float targeDup;
    private float targeDright;
    private float velocityDup;
    private float veolcityDright;

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        //keyup keyright 转 signal
        targeDup = (Input.GetKey(keyUp) ? 1.0f : 0) - (Input.GetKey(keyDown) ? 1.0f : 0);//前后 正数向前 负数向后 最大值1
        targeDright = (Input.GetKey(keyRight) ? 1.0f : 0) - (Input.GetKey(keyLeft) ? 1.0f : 0);//左右 正数向右 负数向左 最大值1

        if(inputEnabled == false)//软开关  清零targeDup targeDright
        {
            targeDup = 0;
            targeDright = 0;
        }
        //平滑阻尼 不会很僵硬变到某一个值
        Dup = Mathf.SmoothDamp(Dup, targeDup, ref velocityDup, 0.1f);
        Dright = Mathf.SmoothDamp(Dright, targeDright, ref veolcityDright, 0.1f);

        Vector2 tempDAxis = SquareTocircle(new Vector2(Dright, Dup));
        float Dright2 = tempDAxis.x;//将output出来的新坐标给到新的坐标值  注意:以下代码要用新的坐标值
        float Dup2 = tempDAxis.y;

        Dmag = Mathf.Sqrt(Dup2 * Dup2 + Dright2 * Dright2);
        //坐标系计算+动画补间旋转
        Dvec = Dright2 * transform.right + Dup2 * transform.forward;

        run = Input.GetKey(keyA);

        bool newJump = Input.GetKey (keyB);
        //jump = tempJump;
        if(newJump != lastjump && newJump==true)
        {
            jump = true;
            print("jump trigger!!");
        }
        else
        {
            jump = false;
        }
        lastjump = newJump;
    }

    private Vector2 SquareTocircle(Vector2 input)
    {
        //将以前坐标x,y进行公式计算 output出新的坐标
        Vector2 output = Vector2.zero;
        output.x = input.x * Mathf.Sqrt(1 - (input.y * input.y) / 2.0f);
        output.y = input.y * Mathf.Sqrt(1 - (input.x * input.x) / 2.0f);
        return output;
    }

}

这样当玩家键入KeyB时console会打印jump trigger,简单实现jump trigger~

猜你喜欢

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