Unity学习笔记1

1、创建一个对象

游戏对象

Instantiate(source,position, rotation);

source:预制件,这个形参可以由unity中游戏模型拖入决定创建类型

position:位置

rotation:角度

类对象

m_Movement = m_Instance.GetComponent<TankMovement> ();

m_Movement :某个物体下面的TankMovement这个对象

m_Instance:某个游戏对象

2、加载一个场景

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

LoadScene:可以加载一个场景

SceneManager.GetActiveScene():获取当前场景的信息

buildIndex:是发布时候的索引 通过这个索引重新加载这个场景

3、物体移动(样例是Rigidbody刚体)

GetComponent<Rigidbody>().velocity = transform.forward * speed;

velocity:速度

transform.forward:控制方向(样例为物体的前方)

speed:变量,可手动设置

4、重复调用方法

InvokeRepeating("Fire", delay, fireRate);

InvokeRepeating:重复调用方法体

"Fire":被调用的方法名

delay:间隔是多少秒再调用

fireRate:初始化后多少秒调用

5、物体平面移动边界限制

float x_range = Mathf.Clamp(GetComponent<Rigidbody>().position.x,x_min,x_max);

float z_range = Mathf.Clamp(GetComponent<Rigidbody>().position.z,z_min,z_max);

GetComponent<Rigidbody>().position = new Vector3 (x_range, 0.0f, z_range);

Mathf.Clamp:夹在最小值和最大值之间的值和返回值

GetComponent<Rigidbody>().position:设置物体的位置

备注:FixedUpdate()生命周期中,根据实际需求计算物体的移动后,加上以上代码。可以达到限制物体在一定范围内移动的效果。

6、获取键盘按键的点击

Input.GetKeyDown(KeyCode.R)

7、获取鼠标的点击

Input.GetButton("Fire1")

"Fire1":鼠标左键的点击

“Fire2”:鼠标右键

“Fire3”:点击鼠标滚轮

8、改变物体位置(调用于FixedUpdate()

float moveHorizontal = Input.GetAxis("Horizontal");

float moveVertical = Input.GetAxis("Vertical");

Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical);

GetComponent<Rigidbody>().velocity = movement * speed;

GetComponent<Rigidbody>().position = new Vector3 (GetComponent<Rigidbody>().position.x,0.0f,GetComponent<Rigidbody>().position.z);

如果移动范围有限制,请参考第5条方法

9、相机平滑过渡

camera.orthographicSize = Mathf.SmoothDamp (camera.orthographicSize, requiredSize, ref m_ZoomSpeed, m_DampTime);

Orthographic:正交

orthographicSize :原来的正交距离

requiredSize:目标正交距离

m_ZoomSpeed:正交尺寸平滑阻尼的参考速度

m_DampTime:相机重新对焦的间隔时间

10、获取物体网格信息给物体上色

MeshRenderer[] renderers = m_Instance.GetComponentsInChildren<MeshRenderer> ();

 for (int i = 0; i < renderers.Length; i++) {

         renderers[i].material.color = redColor;

 }

11、

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangyang6275

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值