欢迎大家来到我的博客http://unity.gopedu.com/home.php?mod=space&uid=3352&do=blog&view=me&from=space
飞机大战子弹如何跟踪敌人
Quaternion(四元数)
Quaternion.LookRotation 注视旋转 方法用法:创建一个旋转,沿着forward(z轴)并且头部沿着upwards(y轴)的约束注视。也就是建立一个旋转,使z轴朝向view y轴朝向up。
注意:
如果forward方向是0,记录一个错误。
例子:
using UnityEngine; using System.Collections; public class example : MonoBehaviour { public Transformtarget; void Update() { Vector3 relativePos = target.position - transform.position; Quaternion rotation = Quaternion.LookRotation(relativePos); transform.rotation = rotation; } } 用此方法可做飞机大战的基本追踪敌人操作public class Enemy : MonoBehaviour {
public GameObject u;//子弹
public GameObject p;//玩家
public GameObject e;
// Use this for initialization
void Start () {
}
float nexttime=0.5f;
float time=0;
// Update is called once per frame
void Update () {
transform.Translate (0,0,-0.01f);
if(Time.time>time){
time=nexttime+Time.time;
Vector3 s=e.transform.position-p.transform.position;
GameObject a= (GameObject)Instantiate (u,transform.position,Quaternion.LookRotation(s));
Destroy(a,2);
}
}
}
请继续关注我的博客
http://unity.gopedu.com/home.php?mod=space&uid=3352&do=blog&view=me&from=space
更多精彩尽在http://www.gopedu.com/