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

一、使用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
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值