Unity中查找对象的方法:
通过GameObject找游戏对象
1) GameObject.Find() ;
2) GameObject.FindWithTag();
3) GameObject.FindGameObjectWithTag();
4) GameObject.FindGameObjectsWithTag();
通过transform找游戏对象
transform.Find();
**总结:
1.GameObject查找返回都是游戏对象;
transform.Find返回的是游戏对象身上的Transform组件
2.GameObject查找不考虑父子层级关系,
transform只能找到子层级的游戏对象
3.如果transform要想找到更深层级的物体,需要给层级关系路径
GameObject也是一样
4.GameObject只能找到激活的,但transform可以找到未激活的游戏对象**
通过键盘输入移动游戏对象,生成子弹:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TankMoveController06 : MonoBehaviour {
// 移动速度
private float moveSpeed=5f;
// 旋转速度
private float xuanSpeed = 30f;
// 子弹预设体
public GameObject Bullet;
//开火点位置
Transform FirePos;
/// <summary>
/// 1.控制移动
/// 2.发射炮弹
/// </summary>
void Start () {
// 找到组件
FirePos = transform.Find("FirePoint");
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.down * Time.deltaTime * xuanSpeed, Space.World);
}
if (Input.GetKey(KeyCode.D))
{
transform.Rotate(Vector3.up * Time.deltaTime * xuanSpeed, Space.World);
}
if (Input.GetKey(KeyCode.W))
{
transform.position += transform.forward * Time.deltaTime * moveSpeed;
}
if (Input.GetKey(KeyCode.S))
{
transform.position -= transform.forward*Time.deltaTime*moveSpeed;
}
// 发射子弹
// GameObject bullet = GameObject.Instantiate<BulletMoveScr06_02>();
if (Input.GetMouseButtonDown(0))
{
// 创建一个子弹对象
// 第一个参数:创建对象
// 第二个参数:生成位置
// 第三个参数:旋转
GameObject bullet = Instantiate(Bullet, FirePos.position,
FirePos.rotation)as GameObject ;
}
}
}
摄像机跟随:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 摄像机跟随
public class CameraFollowScr06_03 : MonoBehaviour {
// 坦克位置信息
Transform tankPos;
// 水平距离
public float horDis = 5f;
// 垂直距离
public float verDis = 4f;
void Start () {
tankPos = GameObject.Find("AMX_30_FBX").transform;
}
// Update is called once per frame
void Update () {
// 1.求出将要移动的目标点位置
Vector3 pos = tankPos.position +
tankPos.up * verDis +
tankPos.forward * horDis;
// 2.平缓的移动过去
transform.position= Vector3.Lerp(transform.position, pos, Time.deltaTime);
if (Vector3.Distance(transform.position,pos)<1f)
{
transform.position = pos;
}
// 3.注视坦克
transform.LookAt(tankPos);
}
}
控制小球寻路:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerLoopMove06 : MonoBehaviour {
// 四个小球的数组
public GameObject[] tagets;
// 要移动的目标位置
Transform targetTrans;
// 小球下标
int index = 0;
void Start () {
// 初始位置
targetTrans = tagets[0].transform;
}
// Update is called once per frame
void Update () {
// 计算到目标点的距离
float distan= Vector3.Distance(transform.position, targetTrans.position);
// 求出方向向量
Vector3 dir = targetTrans.position - transform.position;
// 求出目标方向的四元数
Quaternion q = Quaternion.LookRotation(dir);
transform.rotation= Quaternion.Lerp(transform.rotation, q, Time.deltaTime*10);
if (!(Quaternion.Angle(transform.rotation,q)<1f))
{
return;
}
// 以插值的方式移动
transform.position = Vector3.Lerp(transform.position,
targetTrans.position, Time.deltaTime);
if (distan < 0.1)
{
// 变色
targetTrans.GetComponent<MeshRenderer>().material.color = new Color(Random.value, Random.value, Random.value);
// 控制目标
targetTrans = tagets[++index % tagets.Length].transform;
}
}
}