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