Unity打飞机基本脚本

欢迎大家来到我的博客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/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值