Unity 飛機的子彈軌跡


最近公司在開發一款兒童打飛機游戲.  策划跟我說能在子彈上加上一些軌跡就好了.  比如 旋轉 左右移動呀.然后它就很愉快的跑去截其他游戲的圖啦。。。

我看見圖的時候,

ND@Q@[PDZ)0R110V19O9G{9

image

 

解決方案:

      1.   使用牛逼的算法,實現子彈軌跡的移動(第一種應該是正確的解決方案)

      2.   發射子彈次數 + 前后移動 + 左右移動 + 圍繞某點旋轉 + 自身旋轉 = 子彈軌跡.  采用組合方式實現

目前采用第二種方式:

我們來看下子彈,兩個齒輪會繞着中心旋轉, 並且向下移動. ( 圍繞某點旋轉 +  自身旋轉 + 前后移動 = 實現效果)

image

子彈的GameObject節點層次關系: (此結構和任意組合,但不要把全部功能放在同一個節點上. =。=這樣理不清還會受到干擾)

       前后移動節點(看兩個齒輪中心點向下移動)

            左右移動節點(無這個功能,無需開啟腳本)

                  圍繞父節點旋轉節點(兩個此輪圍繞中心旋轉)

                       自身旋轉節點(比如齒輪它自身會旋轉)

            image

 

下面的子彈實現方式: 自身旋轉 + 左右移動 + 上下移動 = 實現效果

image

============================================================================================================================

代碼區域:

 

左右移動腳本:

using UnityEngine;
using System.Collections;

namespace Bullet 
{
    /// <summary>
    /// 子彈橫向左右移動
    /// </summary>
    public class BulletHorizontal : MonoBehaviour
    {
        public bool isLeft;                 //是否向左移動
        public float moveLocation;          //移動的位置
        public float speed;                 //移動速度
        private Vector3 leftPosition;       //左邊的目標點
        private Vector3 rightPosition;      //右邊的目標點

        void Awake()
        {
            leftPosition = new Vector3(transform.localPosition.x - moveLocation, transform.localPosition.y, transform.localPosition.z);
            rightPosition = new Vector3(transform.localPosition.x + moveLocation, transform.localPosition.y, transform.localPosition.z);
        }

        void Update()
        {

            if (isLeft)
            {
                transform.localPosition += (Vector3.right * speed * Time.deltaTime);
                //transform.Translate(Vector3.right * speed * Time.deltaTime);

                if (transform.localPosition.x >= rightPosition.x)
                {
                    isLeft = !isLeft;
                }
            }
            else
            {
                transform.localPosition += (Vector3.left * speed * Time.deltaTime);
                //transform.Translate(Vector3.left * speed * Time.deltaTime);

                if (transform.localPosition.x <= leftPosition.x)
                {
                    isLeft = !isLeft;
                }
            }
        }
    }
}

子彈旋轉腳本:

using UnityEngine;
using System.Collections;
using BearGame;


namespace Bullet 
{
    /// <summary>
    /// 子彈旋轉
    /// </summary>
    public class BulletRotate : MonoBehaviour
    {
        public RotationDirection rotationDirection;
        public float speed;
        public Transform rotateNode;

        public void Update()
        {
            if (rotateNode != null)
            {
                //圍繞某一節點旋轉
                switch (rotationDirection)
                {
                    case RotationDirection.Left:
                        this.transform.RotateAround(rotateNode.transform.position, Vector3.forward, speed);
                        break;
                    case RotationDirection.Right:
                        this.transform.RotateAround(rotateNode.transform.position, Vector3.forward, -(speed));
                        break;
                }
            }
            else
            {
                //自身旋轉
                switch (rotationDirection)
                {
                    case RotationDirection.Left:
                        this.transform.Rotate(Vector3.forward, speed);
                        break;
                    case RotationDirection.Right:
                        this.transform.Rotate(Vector3.forward, -(speed));
                        break;
                }
            }
        }
    }

    /// <summary>
    /// 旋轉的方向
    /// </summary>
    public enum RotationDirection
    {
        Left,
        None,
        Right
    }
}

 

子彈前后移動:

using UnityEngine;
using System.Collections;
using BearGame;

namespace Bullet
{
    /// <summary>
    /// 子彈前后移動
    /// </summary>
    public class BulletVertical : MonoBehaviour
    {
        public float speed = 0;
        public BulletDirectionEnum direction;
        private Vector3 dir;

        public delegate void BulletOutScreen(GameObject gameObject);
        /// <summary>
        /// 子彈越屏之后,觸發的事件,用於銷毀子彈
        /// </summary>
        /// <param name="gameObject"></param>
        public static event BulletOutScreen OnBulletDestroy;


        void Start()
        {
            switch (direction)
            {
                case BulletDirectionEnum.Up:
                    dir = Vector3.up;
                    break;
                case BulletDirectionEnum.Bottom:
                    dir = -Vector3.up;
                    break;
                case BulletDirectionEnum.Left:
                    dir = Vector3.left;
                    break;
                case BulletDirectionEnum.Right:
                    dir = Vector3.right;
                    break;
            }
        }

        void Update()
        {    
                transform.Translate(dir * speed * Time.deltaTime);
                if (transform.localPosition.y > Screen.height)
                {
                    transform.gameObject.SetActive(false);
                    //調用子彈出屏幕事件
                   }     
        }
    }

    /// <summary>
    /// 子彈移動的方向
    /// </summary>
    public enum BulletDirectionEnum
    {
        Up,
        Bottom,
        Left,
        Right,
        None
    }
}

子彈軌跡總體腳本控制:

using UnityEngine;
using System.Collections;
using BearGame;

namespace Bullet 
{
    public class BulletMode : MonoBehaviour
    {
        /// <summary>
        /// 子彈預設
        /// </summary>
        private GameObject bullet;

        /// <summary>
        /// 子彈的名稱
        /// </summary>
        private string bulletName;

        /// <summary>
        /// 子彈爆炸特效
        /// </summary>
        public Transform bulletEffect;

        /// <summary>
        /// 向左右移動
        /// </summary>
        public BulletHorizontal bulletHorizontal;

        /// <summary>
        /// 向前后移動
        /// </summary>
        public BulletVertical bulletForce;

        /// <summary>
        /// 圍繞某一個點旋轉
        /// </summary>
        public BulletRotate bulletRotateARound;

        /// <summary>
        /// 圍繞自身旋轉
        /// </summary>
        public BulletRotate bulletRotateOneself;

        #region 屬性
        public GameObject Bullet
        {
            get
            {
                if (bullet == null)
                {
                    bullet = this.gameObject;
                }
                return bullet;
            }
            set { bullet = value; }
        }
        public string BulletName
        {
            get
            {
                if (string.IsNullOrEmpty(bulletName))
                {
                    bulletName = this.gameObject.name;
                }
                return bulletName;
            }
            set { bulletName = value; }
        }
        #endregion


        public void TrunOFFALLScript() 
        {
            if (bulletRotateOneself != null) bulletRotateOneself.enabled = false;
            if (bulletRotateARound != null) bulletRotateARound.enabled = false;
            if (bulletForce != null) bulletForce.enabled = false;
            if (bulletHorizontal != null) bulletHorizontal.enabled = false;
                
        }

        public void TrunOpenALLScript()
        {
            if (bulletRotateOneself != null) bulletRotateOneself.enabled = true;
            if (bulletRotateARound != null) bulletRotateARound.enabled = true;
            if (bulletForce != null) bulletForce.enabled = true;
            if (bulletHorizontal != null) bulletHorizontal.enabled = true;
        }
        




    }


}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM