Damping and Enable Flag(輸入衰減與使能旗飄)
阿新 • • 發佈:2018-12-09
- 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); } }