Unity总结:
//只能查找非隐藏的游戏对象,setActive为true的物体
GameObject obj = GameObject.Find("hero");//直接查找名字,效率最低,不能重名
GameObject obj2 = GameObject.Find("a/b/c/hero");//按路径查找,效率很高
GameObject obj3 = GameObject.FindWithTag("标记名称")
Transform t = this.transform.Find("子节点");//可以找隐藏的或非隐藏的
int c = this.transform.childCount;//得到所有子节点长度
Transform child = this.transform.GetChild(0);//子节点下标,通过下标取得子节点
Hero[] heros = this.transform.GetComponentsInChildren<Hero>();//取出所有子节点身上的Hero对象,包括子节点的子节点。
======================== 移动 ================================
this.transform.forward【自身的正前方,0,0,1】 == Vector3.forward【世界坐标正前方0,0,1】:它们是相等的值,但是作用却不一样
[朝某个方向移动]
//1:只朝世界坐标移动,无法运用到角色控制器上,自身旋转后,无法往自身方向移动
this.transform.position += new Vector3(0, 0, 1) * Time.deltaTime * speed;
//2:只朝父对象正前方移动,自身旋转后,无法往自身方向移动,如果没有父对象 , 那就是世界坐标
this.transform.localPosition += Vector3.forward * Time.deltaTime * speed;
//朝自身旋转方向移动
this.transform.Translate(this.transform.forward * Time.deltaTime * speed,Space.World);
Translate(this.transform.forward* speed * Time.deltaTime,后面要加Sapce.World);
Translate(new Vector3(0,0,1)* speed * Time.deltaTime,后面不要加Sapce.World);
Translate(Vector3.forward* speed * Time.deltaTime,后面也不需要加Space.World);
Translate:移动的时候 ,会无视重力
//3:在updata中调用
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
this.Transform.Translate(new Vector3(h*Time.deltaTime*speed,0f,v*Time.deltaTime*speed));
[移动到某个点]
this.transform.position = Vector3.MoveTowards(this.transform.position, targetpoint, Time.deltaTime * 2);
//插值移动,先快后慢
this.transform.position = Vector3.Lerp(this.transform.position, targetpoint, Time.deltaTime * 2);
this.transform.position = Vector3.SLerp(this.transform.position, targetpoint, Time.deltaTime * 2);
[角色控制器]
controller.Move(this.transform.forward * speed * Time.deltaTime);//每一步走多完
controller.SimpleMove(this.transform.forward * speed * Time.deltaTime);//按每秒多少速度来移动,内部带重力影响
========================= 旋转 ===============================
//第一种
transform.Rotate(new Vector3(90, 0, 0));
transform.Rotate(0,25*Time.deltaTime ,0,Space.Self );
//第二种
transform.Rotate(Vector3.up ,90);
//第三种 四元数
transform.rotation = Quaternion.Euler(45, 45, 45);
//第四种 慢慢旋转
Quaternion targetRotation = Quaternion.Euler(45, 45, 45);
transform.rotation = Quaternion.Slerp(transform.rotation,targetRotation,Time.deltaTime *3);
//第五种绕某点旋转
transform.RotateAround(new Vector3(5, 5, 1), Vector3.up, 20*Time.deltaTime);
//第六种欧拉角
transform.eulerAngles = new Vector3(90, 0, 0);
transform.localEulerAngles = new Vector3(90, 0, 0);
====================== 刚体,碰撞,触发器 ===================
刚体:Rigidbody,是一个模拟物体效果的类,只需要挂载到游戏对象身上就可以。
碰撞:
碰撞条件:双方必须要有碰撞盒,且一方要有刚体
两个物体在发生碰撞时,想要检测是否碰撞,需要在任意一方加一个代码去检测:
//刚进入碰撞
void OnCollisionEnter(Collision con)
{
if(con.gameObject.tag=="wall")
{
Time.timeScale = 0;
}
}
//一直碰撞的时候执行
void OnCollisionStay(Collision con){}
//退出碰撞的时候
void OnCollisionExit(Collision con){}
触发器:
需要在任意一方的碰撞盒中勾选 isTrigger属性,当对象的isTrigger属性勾选以后,该游戏对应就没有了碰撞效果,
可以穿透,但是能碰撞的时候能执行触发代码
//进入触发
void OnTriggerEnter(Collider con)
{
if(con.gameObject.tag=="wall")
{
Time.timeScale = 0;
}
}
//一直触发的时候执行
void OnTriggerStay(Collider con){}
//退出触发的时候
void OnTriggerExit(Collider con){}
========================= 射线检测 ===============================
//创建一条指定方向的射线
void testRay1()
{
//创建一条指定方向的射线
Ray r = new Ray(this.transform.position, this.transform.forward * 30);
//显示射线,只能在scene设计窗口下能看的到
Debug.DrawRay(r.origin, r.direction * 30);
//判断射线碰撞
RaycastHit hit;//定义一个保存保存对象信息的指针
bool isHit = Physics.Raycast(r, out hit, 30);
if (isHit)
{
print(hit.point);//取出碰撞点,点是射线碰到对象的那个点
GameObject obj = hit.collider.gameObject;//取出碰到的对象
print("碰到了: " + obj.name);
}
}
//从一个点到指定方向的射线,是第一方法的缩写
void testRay2()
{
RaycastHit hit;
if(Physics.Raycast(this.transform.position, this.transform.forward,out hit,30))
{
GameObject obj = hit.collider.gameObject;//取出碰到的对象
print("碰到了: " + obj.name);
}
}
//从摄像机发出一条无限长,或指定长度的射线
void testRay3()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray,out hit,100))
{
GameObject obj = hit.collider.gameObject;//取出碰到的对象
if(obj.name=="btn")
{
//碰到的对象
}
print("碰到了: " + obj.name);
}
}
}
//射线解决物体之间高速碰撞穿透的问题
void Update ()
{
//先把运动之前的位置保存一次,也可以写在LateUpdate中,这可以在力的作用下有效果
Vector3 oldPos = this.transform.position;
//对象不断的往前运行
this.transform.Translate(this.transform.forward * Time.deltaTime * speed);
//再把移动之后的位置保存一次,获得最新坐标
Vector3 newPos = this.transform.position;
//打印射线
Debug.DrawRay(this.transform.position, (oldPos - newPos).normalized * Vector3.Distance(oldPos, newPos)*-1);
RaycastHit hit;
if(Physics.Raycast(oldPos, (oldPos - newPos).normalized, out hit,Vector3.Distance(oldPos,newPos)))
{
GameObject hitObj = hit.collider.gameObject;
if(hitObj.tag=="wall")
{
DestroyObject(hitObj);
}
}
}
======================== 动画 =============================
3D动画也叫:骨骼动画
Animation: unity版本:5.0以下动画切换方式
Animation ani = this.transform.getCompoent<Animation>();
ani.CrossFade("动画名");
Animator: unity版本:5.0以上 动画状态机
Animator ator = this.transform.getCompoent<Animator>();
ator.setInteger("towalk",1);
ator.setTrigger("towalk");
ator.setBool("towalk",true);
ator.setFloat("towalk",0.1f);
【Unity】基础总结
最新推荐文章于 2025-03-16 17:59:53 发布