SIKI学习——UnityAPI常用方法和类详解0105

本文详细介绍Unity中数学工具类Mathf的使用,包括平滑移动、乒乓运动效果、数值限制等,同时深入探讨Input类在按键、触摸检测及虚拟按键的应用,通过实例演示如何控制物体移动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、使用MoveTowards做匀速运动

public class API10Mthf : MonoBehaviour
{
    public Transform cube;
    public int a = 8;
    public int b = 20;
    public float t = 0;//必须是float类型
    public float speed = 3;
    void Start ()
    {
        ////度数转弧度
        //print(Mathf.Deg2Rad);
        ////弧度转度数
        //print(Mathf.Rad2Deg);
        ////无限大的数
        //print(Mathf.Infinity);
        ////无限小的数
        //print(Mathf.NegativeInfinity);
        ////π的值
        //print(Mathf.PI);
        ////大于0的小数
        //print(Mathf.Epsilon);
        //向下取整
        //Debug.Log(Mathf.Floor(10.0f));
        //Debug.Log(Mathf.Floor(10.2f));
        //Debug.Log(Mathf.Floor(10.7f));
        //Debug.Log(Mathf.Floor(-10.0f));
        //Debug.Log(Mathf.Floor(-10.2f));
        //Debug.Log(Mathf.Floor(-10.7f));
        //取得离2的次方的值最近的数,2,4,8,16,32
        //print(Mathf.ClosestPowerOfTwo(2));//4
        //print(Mathf.ClosestPowerOfTwo(3));//8
        //print(Mathf.ClosestPowerOfTwo(4));//8
        //print(Mathf.ClosestPowerOfTwo(5));//8
        //print(Mathf.ClosestPowerOfTwo(6));//8
        //print(Mathf.ClosestPowerOfTwo(30));//8
        //print(Mathf.Max(1,2));//2
        //print(Mathf.Max(1,2,5,3,10));//10
        //print(Mathf.Min(1, 2));//1
        //print(Mathf.Min(1,2,5,3,10));//1
        //print(Mathf.Pow(4,3));//64
        //print(Mathf.Sqrt(3));//1.73
        cube.position = new Vector3(0,0,0);
    }
 void Update ()
    {
        //把Time.time的值限定在1到3之间,如果比1小,就返回1,比3大就返回3,如果在1到3之间就返回它本身的值
        //cube.position = new Vector3(Mathf.Clamp(Time.time,1.0f,3.0f),0,0);
        //Debug.Log(Mathf.Clamp(Time.time, 1.0f, 3.0f));
        //插值
        //print(Mathf.Lerp(a,b,t));
        //一般不是定义,一般是用时间
        
        //float x = cube.position.x;
        //float newX= Mathf.Lerp(c,10,0.1f);
        //嫌慢的话可以乘以速度Time.deltaTime*Speed
        //float newX = Mathf.Lerp(c,10,Time.deltaTime);
        //匀速运动0.02f和-0.02f是相反方向float newX = Mathf.MoveTowards(c, 10, -0.02f);
        //float newX = Mathf.MoveTowards(x, 10,Time.deltaTime*speed);
        //cube.position = new Vector3(newX ,0,0);
        
        print(Mathf.MoveTowards(a,b,t));
 }
    //计算伤害时用
    private int hp = 100;
    void TakeDamage()
    {
        hp -= 9;
        //if (hp<0)
        //    hp = 0;
        //简单写法
        hp = Mathf.Clamp(hp,0,100);
    }
}

二、使用pingpong方法实现乒乓的来回运动效果

public class API10Mthf : MonoBehaviour
{
    public Transform cube;
    public int a = 8;
    public int b = 20;
    public float t = 0;//必须是float类型
    public float speed = 3;
    void Start ()
    {
        ////度数转弧度
        //print(Mathf.Deg2Rad);
        ////弧度转度数
        //print(Mathf.Rad2Deg);
        ////无限大的数
        //print(Mathf.Infinity);
        ////无限小的数
        //print(Mathf.NegativeInfinity);
        ////π的值
        //print(Mathf.PI);
        ////大于0的小数
        //print(Mathf.Epsilon);
        //向下取整
        //Debug.Log(Mathf.Floor(10.0f));
        //Debug.Log(Mathf.Floor(10.2f));
        //Debug.Log(Mathf.Floor(10.7f));
        //Debug.Log(Mathf.Floor(-10.0f));
        //Debug.Log(Mathf.Floor(-10.2f));
        //Debug.Log(Mathf.Floor(-10.7f));
        //取得离2的次方的值最近的数,2,4,8,16,32
        //print(Mathf.ClosestPowerOfTwo(2));//4
        //print(Mathf.ClosestPowerOfTwo(3));//8
        //print(Mathf.ClosestPowerOfTwo(4));//8
        //print(Mathf.ClosestPowerOfTwo(5));//8
        //print(Mathf.ClosestPowerOfTwo(6));//8
        //print(Mathf.ClosestPowerOfTwo(30));//8
        //print(Mathf.Max(1,2));//2
        //print(Mathf.Max(1,2,5,3,10));//10
        //print(Mathf.Min(1, 2));//1
        //print(Mathf.Min(1,2,5,3,10));//1
        //print(Mathf.Pow(4,3));//64
        //print(Mathf.Sqrt(3));//1.73
        cube.position = new Vector3(0,0,0);
    }
 void Update ()
    {
        //把Time.time的值限定在1到3之间,如果比1小,就返回1,比3大就返回3,如果在1到3之间就返回它本身的值
        //cube.position = new Vector3(Mathf.Clamp(Time.time,1.0f,3.0f),0,0);
        //Debug.Log(Mathf.Clamp(Time.time, 1.0f, 3.0f));
        //插值
        //print(Mathf.Lerp(a,b,t));
        //一般不是定义,一般是用时间
        //float x = cube.position.x;
        //float newX= Mathf.Lerp(c,10,0.1f);
        //嫌慢的话可以乘以速度Time.deltaTime*Speed
        //float newX = Mathf.Lerp(c,10,Time.deltaTime);
        //匀速运动0.02f和-0.02f是相反方向float newX = Mathf.MoveTowards(c, 10, -0.02f);
        //float newX = Mathf.MoveTowards(x, 10,Time.deltaTime*speed);
        //cube.position = new Vector3(newX ,0,0);
        //print(Mathf.MoveTowards(a,b,t));
        
        //print(Mathf.PingPong(t, 20));//返回的是0到20之间的数,0是默认最小的数t是运动速度
        cube.position = new Vector3(5+Mathf.PingPong(Time.time*speed,5),0,0);//cube在5到10之间来回运动
 }
    //计算伤害时用
    private int hp = 100;
    void TakeDamage()
    {
        hp -= 9;
        //if (hp<0)
        //    hp = 0;
        //简单写法
        hp = Mathf.Clamp(hp,0,100);
    }
}

三、Input类输入类(按键、触摸相关检测)

Input里面的getkeyXXX的使用

public class API11Input : MonoBehaviour
{
 void Update ()
    {
        //getkeyXXX的使用
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //当我们按下的那一帧返回true,只执行一次
            print("GetKeyDown");
        }
        if (Input.GetKeyUp(KeyCode.Space))
        {
            print("GetKeyUp");
        }
        if (Input.GetKey(KeyCode.Space))
        {
            print("GetKey");
        }
  }
}

鼠标按键事件的监测
键盘上所有按键的名字,上面的方法不光可以使用keycode还可以直接传键的名字
在这里插入图片描述

public class API11Input : MonoBehaviour
{
 void Update ()
    {
        //getkeyXXX的使用
        //if (Input.GetKeyDown(KeyCode.Space))
        //{
        //    //当我们按下的那一帧返回true,只执行一次
        //    print("GetKeyDown");
        //}
        //if (Input.GetKeyUp(KeyCode.Space))
        //{
        //    print("GetKeyUp");
        //}
        //if (Input.GetKey(KeyCode.Space))
        //{
        //    print("GetKey");
        //}
        if (Input.GetKeyDown("left shift"))
        {
            print("left shift");//名字容易出错,各有优劣
        }
    }
}

鼠标按键事件监测例子

public class API11Input : MonoBehaviour
{
 void Update ()
    {
        //getkeyXXX的使用
        //if (Input.GetKeyDown(KeyCode.Space))
        //{
        //    //当我们按下的那一帧返回true,只执行一次
        //    print("GetKeyDown");
        //}
        //if (Input.GetKeyUp(KeyCode.Space))
        //{
        //    print("GetKeyUp");
        //}
        //if (Input.GetKey(KeyCode.Space))
        //{
        //    print("GetKey");
        //}
        //if (Input.GetKeyDown("left shift"))
        //{
        //    print("left shift");//名字容易出错,各有优劣
        //}


        //鼠标按键事件的监测
        //if (Input.GetMouseButton(0))
        //{
        //    Debug.Log("Pressed left Click");
        //}
        //if (Input.GetMouseButton(1))
        //{
        //    Debug.Log("Pressed right Click");
        //}
        //if (Input.GetMouseButton(2))
        //{
        //    Debug.Log("Pressed middle Click");
        //}
        if (Input.GetMouseButtonDown(0))//只执行一次
        {
            Debug.Log("Pressed left Click");
        }
        if (Input.GetMouseButtonDown(1))//只执行一次
        {
            Debug.Log("Pressed right Click");
        }
        if (Input.GetMouseButtonDown(2))//只执行一次
        {
            Debug.Log("Pressed middle Click");
        }
    }
}

GetButtonXXX相关事件的监测
unity中的虚拟按键
在这里插入图片描述
在这里插入图片描述
上面的18个全是虚拟按键的名字
在这里插入图片描述
在这里插入图片描述

public class API11Input : MonoBehaviour
{
 void Update ()
    {
        //getkeyXXX的使用
        //if (Input.GetKeyDown(KeyCode.Space))
        //{
        //    //当我们按下的那一帧返回true,只执行一次
        //    print("GetKeyDown");
        //}
        //if (Input.GetKeyUp(KeyCode.Space))
        //{
        //    print("GetKeyUp");
        //}
        //if (Input.GetKey(KeyCode.Space))
        //{
        //    print("GetKey");
        //}
        //if (Input.GetKeyDown("left shift"))
        //{
        //    print("left shift");//名字容易出错,各有优劣
        //}
        //鼠标按键事件的监测
        //if (Input.GetMouseButton(0))
        //{
        //    Debug.Log("Pressed left Click");
        //}
        //if (Input.GetMouseButton(1))
        //{
        //    Debug.Log("Pressed right Click");
        //}
        //if (Input.GetMouseButton(2))
        //{
        //    Debug.Log("Pressed middle Click");
        //}
        //if (Input.GetMouseButtonDown(0))//只执行一次
        //{
        //    Debug.Log("Pressed left Click");
        //}
        //if (Input.GetMouseButtonDown(1))//只执行一次
        //{
        //    Debug.Log("Pressed right Click");
        //}
        //if (Input.GetMouseButtonDown(2))//只执行一次
        //{
        //    Debug.Log("Pressed middle Click");
        //}

        //GetButtonXXX相关事件监测
        //if (Input.GetButtonDown("Fire1"))
        //{
        //    print("Fire1 Down");
        //}
        if(Input.GetButtonDown("Horizontal"))
        {
            print("Horizontal Down");
        }
    }
}

使用GetAxis得到轴的值的变化来控制移动

public class API11Input : MonoBehaviour
{
    public Transform cube;
 void Update ()
    {
        //getkeyXXX的使用
        //if (Input.GetKeyDown(KeyCode.Space))
        //{
        //    //当我们按下的那一帧返回true,只执行一次
        //    print("GetKeyDown");
        //}
        //if (Input.GetKeyUp(KeyCode.Space))
        //{
        //    print("GetKeyUp");
        //}
        //if (Input.GetKey(KeyCode.Space))
        //{
        //    print("GetKey");
        //}
        //if (Input.GetKeyDown("left shift"))
        //{
        //    print("left shift");//名字容易出错,各有优劣
        //}
        //鼠标按键事件的监测
        //if (Input.GetMouseButton(0))
        //{
        //    Debug.Log("Pressed left Click");
        //}
        //if (Input.GetMouseButton(1))
        //{
        //    Debug.Log("Pressed right Click");
        //}
        //if (Input.GetMouseButton(2))
        //{
        //    Debug.Log("Pressed middle Click");
        //}
        //if (Input.GetMouseButtonDown(0))//只执行一次
        //{
        //    Debug.Log("Pressed left Click");
        //}
        //if (Input.GetMouseButtonDown(1))//只执行一次
        //{
        //    Debug.Log("Pressed right Click");
        //}
        //if (Input.GetMouseButtonDown(2))//只执行一次
        //{
        //    Debug.Log("Pressed middle Click");
        //}
        //GetButtonXXX相关事件监测
        //if (Input.GetButtonDown("Fire1"))
        //{
        //    print("Fire1 Down");
        //}
        //if(Input.GetButtonDown("Horizontal"))
        //{
        //    print("Horizontal Down");
        //}


        //使用GetAxis的到轴的值来控制移动
        //print(Input.GetAxis("Horizontal"));
        cube.Translate(Vector3.right*Time.deltaTime* Input.GetAxis("Horizontal")*5);//由0渐变到1有一个渐变效果(自己会运动 每秒运行一米)
        //cube.Translate(Vector3.right*Time.deltaTime* Input.GetAxisRaw("Horizontal")*5);//直接从0变到1
    }
}
内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、COSO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析模拟,揭示了生物质炉具在实际应用中的优点挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值