U3D 常用控制代码

控制角色移动和旋转、碰撞触发、条件触发。

用键盘控制方块 前后左右移动:

1、Hierarchy – 3D Object – Cube (创建方块)
2、Cube – Componet – Physics -Rigidbody (给方块赋值刚体)
3、Cube – Inspector – Add Componet – New Script – Name:Move – Create and Add (给方块创建脚本命名 Move)
4、Cube – Inspector – Script Move – 点击齿轮 – Edit Script – 弹出 Microsoft Visual Studio(在名为 Move.cs 文件编写方块的脚本)
5、填写以下代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour {
 private Rigidbody qt; // 定义私有物体变量名为 qt
 public float speed; //把移动速读设置公共变量前台可以控制
 void Start()
 {
 qt = GetComponent<Rigidbody>(); // 获取物体赋值给 qt
 }
 void FixedUpdate()
 {
 float horiInput = Input.GetAxis("Horizontal"); //x轴 左右A、D控制
 float vertiInput = Input.GetAxis("Vertical"); // y轴 前后W、S控制
 Vector3 movement = new Vector3(horiInput, 0, vertiInput);
 //前后左右移动赋值 锁定上下
 qt.AddForce(movement * speed); //定义移动速读
 }
}

6、在 Microsoft Visual Studio 写完脚本 Ctrl+S 保存 关闭
7、回到 Unity3D Ctrl + P 运行查看效果


摄像机跟随球体一块移动:

1、Hierarchy – Camera
2、Camera – Inspector – Script CameraCtrl – 点击齿轮 – Edit Script – 弹出 Microsoft Visual Studio(在名为 CameraCtrl.cs 文件编写方块的脚本)
3、填写以下代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camera : MonoBehaviour {
 public GameObject sphere;
 private Vector3 offset;
 void Start()
 {
 offset = transform.position - sphere.transform.position ;
 }
 void LateUpdate () {
 transform.position = sphere.transform.position + offset;
 }
}

4、在 Microsoft Visual Studio 写完脚本 Ctrl+S 保存 关闭
5、Camera – Inspector – Script (把要跟随的物体拖到定义的公共变量Sphere上)
6、回到 Unity3D Ctrl + P 运行查看效果


转动物体:

1、Hierarchy – 3D Object – Cube (创建方块)
2、Cube – Inspector – Add Componet – New Script – Name:Zd
3、Cube – Inspector – Script Move – 点击齿轮 – Edit Script – 弹出 Microsoft Visual Studio(在名为 Zd.cs 文件编写方块的脚本)
4、填写以下代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class zhuan : MonoBehaviour {
 public Vector3 rot; //定义公共变量前台控制数值
 void Update () {
 transform.Rotate(rot); //转动数值 x,y,z
 }
}

5、在 Microsoft Visual Studio 写完脚本 Ctrl+S 保存 关闭
6、回到 Unity3D Ctrl + P 运行查看效果


碰撞物体消失:

1、球碰撞方块消失
2、在球的脚本填写以下代码:

 private void OnTriggerEnter(Collider other)
 {
 other.gameObject.SetActive(false);
 }

3、在方块的 Inspector – Box Collider – 选中 Is Trigger
4、Ctrl + P 运行查看效果


物体碰撞计数:

1、Hierarchy – UI -Text (创建计数标签)
2、Hierarchy – Canvas – Text – Inspector – 改名为 contText 调整好显示位置
3、在控制物体添加以下代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; //调用UI
 private int count; // 定义计数变量
 public Text contText; //关联计数标签
 void Start()
 {
 count = 0; //初始值为0
 contText.text = "Num" + count; //计数加赋值
 }
 private void OnTriggerEnter(Collider other)
 {
 other.gameObject.SetActive(false);
 count++;
 contText.text = "Num" + count;
 }
}

4、把标签拖到控制物体的定义的ContText 变量内
5、Ctrl + P 运行查看效果


数值触发:

1、计数大于3显示标签
2、代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; //调用UI
public class Move : MonoBehaviour {
 public Text win;
 void Start()
 {
 win.text = "";
 }
 private void OnTriggerEnter(Collider other)
 {
 if (count > 2)
 {
 win.text = "You Win";
 }
 }
}

发布
1、Ctrl+Shift+B – Bulid Settings – Bulid
2、选好文件命名即可发布