美文网首页
2020-09-15 unity泡泡龙射线实现

2020-09-15 unity泡泡龙射线实现

作者: VECTOR_Y | 来源:发表于2020-09-22 10:03 被阅读0次
    using DG.Tweening;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Threading;
    using UnityEditor.SceneManagement;
    using UnityEngine;
    using UnityEngine.UIElements;
    
    /// <summary>
    /// 玩家操作状态
    /// </summary>
    enum playerStatus
    {
        [InspectorName("空闲")]
        idle,
        [InspectorName("瞄准中")]
        aim,
        [InspectorName("击发")]
        firing,
    }
    
    
    /// <summary>
    /// 玩家操作类
    /// </summary>
    public class player : MonoBehaviour
    {
        /// <summary>
        /// 球插槽
        /// </summary>
        private Transform ball_Slot;
        /// <summary>
        /// 发射球
        /// </summary>
        private SendBall ball;
        /// <summary>
        /// 发射求预制体
        /// </summary>
        private GameObject ballTemplate;
    
        /// <summary>
        /// 当前操作状态
        /// </summary>
        playerStatus currentStatus=playerStatus.idle;
    
        public static player _instacce;
    
        LineRenderer line;
    
        /// <summary>
        /// 是否符合发射角度
        /// </summary>
        bool isAllowLaunch = false;
    
        /// <summary>
        /// 所有拐点
        /// </summary>
        List<Vector3> turnArr = new List<Vector3>();
    
    
        private void Awake()
        {
            _instacce = this;
            ball_Slot = GameObject.Find("Dog_Throw_Ball").transform;
            ballTemplate = Resources.Load<GameObject>("ball/sendBall");
            CreateSendBall();
            CreateTestLine();
        }
    
        /// <summary>
        /// 随机生成用于发射的小球
        /// </summary>
        void CreateSendBall(Action callback=null) 
        {
            sendBallType type = (sendBallType) UnityEngine.Random.Range(0,(int)sendBallType.REDPACKETBALL+1);
            GameObject go = GameObject.Instantiate(ballTemplate,  ball_Slot);
            ball = go.AddComponent<SendBall>();
            ball.Init(type,callback);
        }
    
        /// <summary>
        /// 生成测试组件
        /// </summary>
        void CreateTestLine() 
        {
            line = new GameObject().AddComponent<LineRenderer>();
            line.transform.SetParent(transform);
            line.name = "line";
            line.transform.localScale = Vector3.one;
            line.transform.localPosition = Vector3.zero;
            line.material = new Material(Shader.Find("Mobile/Particles/Additive"));
            line.startWidth = 0.01f;
            line.endWidth = 0.01f;
            line.gameObject.SetActive(false);
        }
    
        private void OnMouseDown()
        {
            if (currentStatus == playerStatus.idle) 
            {
                turnArr.Clear();
                currentStatus = playerStatus.aim;
                float m_weaponAngle = Vector2.Angle(Input.mousePosition - Camera.main.WorldToScreenPoint(ball_Slot.position), Vector2.right);
                GetTurnPoint(ball_Slot.transform.position, m_weaponAngle, false, "");
                OnDrawLine();
            }
        }
    
        private void OnMouseDrag()
        {
            if (currentStatus == playerStatus.aim) 
            {
                turnArr.Clear();
                float m_weaponAngle = Vector2.Angle(Input.mousePosition - Camera.main.WorldToScreenPoint(ball_Slot.position), Vector2.right);
                GetTurnPoint(new Vector3(ball_Slot.position.x, ball_Slot.position.y), m_weaponAngle,false, "");
                OnDrawLine();
            }
        }
    
        /// <summary>
        /// 获取拐点
        /// </summary>
        void GetTurnPoint(Vector3 ori,float angle,bool isReverse, string currWall) 
        {
            if (angle < 165 && angle>15)
            {
                isAllowLaunch = true;
    
                float x = 10 * Mathf.Cos((angle) * 3.14f / 180);
                float y = 10 * Mathf.Sin((angle) * 3.14f / 180);
    
                Vector2 target ;
                if (isReverse)
                {
                    target = new Vector2(-x, y);
                }
                else
                {
                    target = new Vector2(x, y);
                }
    
                LayerMask mask = 1 << 0 | 1 << 5; 
                
                RaycastHit2D[] hitInfo = Physics2D.RaycastAll(ori, target, 1000f,mask);
    
                if (hitInfo.Length == 0) 
                {
                    isAllowLaunch = false;
                    return;
                }
                //撞到球
                foreach (RaycastHit2D item in hitInfo)
                {
                    if (item.collider.gameObject.tag.Contains("ball"))
                    {
                        turnArr.Add(item.point);
                        return;
                    }
                }
                //撞到墙
                foreach (RaycastHit2D item in hitInfo)
                {
                    if (item.collider.gameObject.name.Contains("CrashWall") && !item.collider.gameObject.name.Equals(currWall))
                    {
                        turnArr.Add(item.point);
                        GetTurnPoint(item.point, angle,!isReverse,item.collider.gameObject.name);
                        return;
                    }
                }
            }
            else
            {
                isAllowLaunch = false;
            }
        }
    
        /// <summary>
        /// 绘出瞄准线
        /// </summary>
        private void OnDrawLine()
        {
            if (turnArr.Count > 0)
            {
                line.gameObject.SetActive(true);
                line.positionCount =( turnArr.Count+1);
                line.SetPosition(0, new Vector2(ball_Slot.transform.position.x, ball_Slot.transform.position.y + 0.1f));
              
                for (int i = 0; i < turnArr.Count; i++)
                {
                    line.SetPosition(i+1,turnArr[i]);
                }
            }
            else 
            {
                line.gameObject.SetActive(false);
            }
        }
    
        private void OnMouseUp()
        {
            if (currentStatus == playerStatus.aim) 
            {
                line.gameObject.SetActive(false);
                if (isAllowLaunch)
                {
                    LaunchBall();
                }
                else 
                {
                    currentStatus = playerStatus.idle;
                }
            }
        }
    
        /// <summary>
        /// 发射
        /// </summary>
        void LaunchBall() 
        {
            currentStatus = playerStatus.firing;
            ball.BallMove(turnArr,0,()=> {
                CreateSendBall(()=> {
    
                    currentStatus = playerStatus.idle;
    
                });
            });
        }
    
       
    
    }
    

    相关文章

      网友评论

          本文标题:2020-09-15 unity泡泡龙射线实现

          本文链接:https://www.haomeiwen.com/subject/mkwxyktx.html