[Unity学习随笔3/15]quard的trigger属性,SmoothDamp函数

BUG记录:

NullReferenceException: Object reference not set to an instance of an object

可能是脚本中的对象未绑定(或者对象改名后忘记重新绑定)


 

Quard中的该属性:  用来虚化/实化空间及判断空间是否被入侵, 可拓写相应的触发函数

private void OntriggerEnter( ){ }

进入该空间

private void OntriggerStay( ){ }

在该区域停留

private void OntriggerExit( ){ }

离开该区域


getaxis的horizal和vertical只适用于键盘,对多设备输入的支持不灵活

GetAixs对设备的支持可以在edit -> projectsetting -> inputmanager设定

SmoothDamp可以使按键变动的信号过渡更加平缓自然

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 targetDup;
    private float targetDright;
    private float velocityDup;
    private float velocityDright;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        targetDup = (Input.GetKey(keyUp) ? 1.0f : 0) - (Input.GetKey(keyDown) ? 1.0f : 0);
        targetDright = (Input.GetKey(keyRight) ? 1.0f : 0) - (Input.GetKey(keyLeft) ? 1.0f : 0);

        if (inputEnabled == false)
        {
            targetDup = 0;
            targetDright = 0;
        }

        Dup = Mathf.SmoothDamp(Dup, targetDup, ref velocityDup, 0.1f);
        Dright = Mathf.SmoothDamp(Dright, targetDright, ref velocityDright, 0.1f);

    }
}

[专栏作家]Unity中Lerp与SmoothDamp函数使用误区浅析 (sohu.com)icon-default.png?t=M276https://www.sohu.com/a/211459755_667928

posted @ 2022-03-15 22:46  泥烟  阅读(95)  评论(0编辑  收藏  举报