Damping and Enable Flag(输入衰减与使能旗飘)

  1. keyup keyright转signa
using System.Collections;using System.Collections;
using System.Collections.Generic;
using UnityEngine;`

public class PlayerInput : MonoBehaviour
{

    public string keyUp = "w";
    public string keyDown = "s";
    public string keyLeft = "a";
    public string keyRight = "d";

    public float Dup;
    public float Dright;`
    }
     void Update()
    {
        //keyup keyright 转 signal
        targeDup = (Input.GetKey(keyUp) ? 1.0f : 0) - (Input.GetKey(keyDown) ? 1.0f : 0);
        targeDright = (Input.GetKey(keyRight) ? 1.0f : 0) - (Input.GetKey(keyLeft) ? 1.0f : 0);`

2.Mathf.SmoothDamp(平滑阻尼)
public static float SmoothDamp(float current,float target,ref float currenVelocity,float smoothTime,float maxSpeed = Mathf.infinity,float delta time = time,delaTime);

current:目前的值(灌进函数的初始值)。
target:调到多少值(目标值)。
currentVelocity:提供内存空间,给Smooth damp提供运算空间。
smoothtime:这次damp要花多长时间来完成,缓冲时间,时间越大缓冲速度越慢,移动也越慢。

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

public class PlayerInput : MonoBehaviour
{

    public string keyUp = "w";
    public string keyDown = "s";
    public string keyLeft = "a";
    public string keyRight = "d";

    public float Dup;
    public float Dright;

    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);
        targeDright = (Input.GetKey(keyRight) ? 1.0f : 0) - (Input.GetKey(keyLeft) ? 1.0f : 0);

        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);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值