Unity中的Transform\GameObject\键盘操作Input\Time\Mathf\Rigidbody\鼠标事件 组件的使用

主要学习了Unity引擎中脚本对Transform\GameObject\Time\Mathf\Rigidbody\鼠标 组件的操作

1.Transform 

transform主要是用来控制某个游戏对象的位置、大小、旋转角度

除此之外还控制游戏对象的父子关系

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class trans : MonoBehaviour {

	// 获取当前脚本所挂在的游戏对象身上的 Transform 组件
    //transform

	void Start () {
        //1.控制游戏对象的位置、旋转、缩放
        //Transform中的  Position属性         是 世界坐标系 中的位置
        //              localPosition         是局部坐标系中的位置
        Vector3 v = transform.position;
        print(v);

        //一般不会直接修改Rotation的属性,因为涉及到四元数
        // Rotation
        // localScale - 游戏对象中的缩放

        //2.控制游戏对象间的父子关系

        //transform.parent 获取/重新指定当前游戏对象 父对象 的 Transform 组件,返回值是transform

        //transform.root   表示当前游戏对象的 根父 对象,返回值是transform
        //根父:子不管是属于哪一个层级,根父都是最初始的那个

        //transform.Find(字符串)  获取当前游戏对象的叫做 “字符串” 的子对象,返回值是transform
        //transform.Findchild(字符串)  也是获取当前游戏对象的子对象,
        //                             如果有多个重名的对象,只返回第一个
    }

    // 
    void Update () {
        if (Input.GetKeyDown(KeyCode.P))
        {
            //Vector3()代表一个新点
            transform.localPosition = Vector3.zero;

            //localScale 的使用
            transform.localScale = new Vector3(1f,2f,3f);

            //transform.Translate 的使用效果有累加效果,可以一直使用
            transform.Translate(new Vector3(0,1,0));

            //使用transform.Rotate让游戏对象产生旋转的效果,前面的是旋转轴,后面是每次旋转的角度
            transform.Rotate(Vector3.up , 10f);

            //eulerAngles使用欧拉角来对游戏对象进行旋转,只执行一次,赋值了一个效果
            transform.eulerAngles = new Vector3(0f,45f,0f);
        }
	}
}

2.GameObject : 主要是代表游戏对象

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//如果脚本需要挂载到游戏对象身上,就需要继承于 MonoBehaviour
//MonoBehaviour提供了许多常见的工具
public class test : MonoBehaviour {

    //可以添加自己的属性
    public int age;
    public string name;
    public void Log() {
        print(name+" "+age);
    }    

	void Start () {
        //gameObject 表示当前脚本组件所挂在的游戏对象
        // unity中输出到控制台需要使用 print 或者 Debug.log
        print("Hello");
        //只有在Unity中启动项目之后才会打印出“Hello”,并且实在Unity的Console视图中打印


        //gameObject可以获取很多信息
        print("Test 脚本挂载到了" + gameObject.name + "的身上");

        //每个游戏对象都有一个Transform组件,在脚本里面也可以调用获取
        //transform
        print(transform.position.x);
        print(transform.position.y);
        print(transform.position.z);

        //
    }
	
	// Update is called once per frame
	void Update () {
		
	}
}

3.键盘操作Input和鼠标事件

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test1 : MonoBehaviour {

	// Input 类
	void Start () {
		
	}
	
    //获取用户事件需要使用 Input 类
	
	void Update () {
        //有返回值bool类型
        //在当前这一帧中,如果用户按下了 W 就会返回true
        //每一帧都需要监听用户事件

        //按键按下的事件--之检测并且触发一次
        bool b = Input.GetKeyDown(KeyCode.W);
        if (b)
        {
            print("前进");
        }

        if (Input.GetKeyDown(KeyCode.S))
        {
            print("后退");
        }

        if (Input.GetKeyDown(KeyCode.A))
        {
            //print只能在 MonoBehaviour 的子类中使用,其他情况下只能使用Debug.Log();
            Debug.Log("向左");
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            print("向右");
        }
        //按键按下并且弹起的事件
        //Alpha1表示字母键上方的数字键
        if (Input.GetKeyUp(KeyCode.Alpha1))
        {
            print("按下并且弹起了 1 键");
        }

        //检测持续按键的事件
        if (Input.GetKey(KeyCode.F))
        {
            print("闪现");
        }

        //Input来监听鼠标的事件,返回值也是bool类型
        //0代表鼠标左键 , 1代表鼠标右键 , 2代表鼠标中键
        if (Input.GetMouseButtonDown(0))
        {
            print("按下了鼠标 左键 ");
        }
        if (Input.GetMouseButtonDown(1))
        {
            print("按下了鼠标 右键 ");
        }

        if (Input.GetMouseButtonDown(2))
        {
            print("按下了鼠标 中键 ");
        }

        //Input.GetMouseButton()   - 获取持续按下的事件
        //Input.GetMouseButtonUp() - 获取鼠标持续按下并且弹起的事件
    }
}

4.Time和Mathf类:常用的时间控制函数和数学函数

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class time : MonoBehaviour
{
    //Time类用来进行时间控制
    public float anglespeed;
    void Update()
    {
        if (Input.GetKey(KeyCode.T))
        {
            //Time.time 可以测试从游戏开始到当前的运行时间
            float t = Time.time;
            print("从游戏开始到当前所消耗的总时长为" + t + " s");
            //Time.deltaTime 可以检测从上一帧开始到当前帧的时间
            //60FPS 就是大概 1/60s
            float t1 = Time.deltaTime;
            print("从上一帧到这一帧所消耗的总时长为" + t1 + " s");

            //使用deltaTime精确控制时间,允许对象每秒钟准确地旋转30度,跟机器自身的性能表现无关
            transform.Rotate(Vector3.up, Time.deltaTime * anglespeed);

            //表示时间流逝的快慢
            // 1 - 表示正常时间流逝
            // 2 - 表示时间流逝加快,是正常速度的两倍
            // 0 - 时间停止,游戏暂停
            float ts = Time.timeScale;

            //Mathf类中封装了常用的数学方法
            //求绝对值
            int i = Mathf.Abs(-15);
            //求最大最小值
            int m = Mathf.Max(12, 4, 5, 45);
            //三角函数
            Mathf.Sin(anglespeed);
            Mathf.Cos(anglespeed);
            Mathf.Tan(anglespeed);
            //圆周率
            //Mathf.PI;
        }
    }
}

以上的操作都是Unity中脚本里面的基本操作,需要牢记

除此之外,当一个脚本组件中也可以获取同属于该游戏对象的另一个脚本:

//在另一个脚本组件Demo中也可以获取另一个脚本组件
    // GameObject 的方法 GetCompnent 能够获取当前游戏对象身上指定的类型的组件对象
	void Start () {
        //在获取另一个脚本的内容之前也需要创建一个类型的变量来接收脚本的值
        test t = GetComponent<test>();
        t.name = "dzzhyk5551";
        t.age = 20;
        t.Log();
	}

猜你喜欢

转载自blog.csdn.net/weixin_43826242/article/details/85082968
今日推荐