[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 @   泥烟  阅读(109)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示