代码之家  ›  专栏  ›  技术社区  ›  StuckInPhDNoMore

我的光线投射似乎没有任何范围-仅检测放置在发射游戏对象旁边的对象

  •  0
  • StuckInPhDNoMore  · 技术社区  · 6 年前

    我正在使用以下代码(来自: https://gist.github.com/valryon/566c02f3c5808dcd9968 )在我的游戏中生成2D激光。激光器有起始部分、中间部分和结束部分。组成激光器并作为父游戏对象的子对象。

    public class LaserScript : MonoBehaviour
    {
        [Header("Laser pieces")]
        public GameObject laserStart;
        public GameObject laserMiddle;
        public GameObject laserEnd;
    
        private GameObject start;
        private GameObject middle;
        private GameObject end;
    
        void Update()
        {
            // Create the laser start from the prefab
            if (start == null)
            {
                start = Instantiate(laserStart) as GameObject;
                start.transform.parent = this.transform;
                start.transform.localPosition = Vector2.zero;
            }
    
            // Laser middle
            if (middle == null)
            {
                middle = Instantiate(laserMiddle) as GameObject;
                middle.transform.parent = this.transform;
                middle.transform.localPosition = Vector2.zero;
            }
    
            // Define an "infinite" size, not too big but enough to go off screen
            float maxLaserSize = 200f;
            float currentLaserSize = maxLaserSize;
    
            // Raycast at the right as our sprite has been design for that
            Vector2 laserDirection = this.transform.right;
            RaycastHit2D hit = Physics2D.Raycast(this.transform.position, laserDirection, maxLaserSize);
            Debug.Log(hit.collider.tag);
            if (hit.collider.tag == "Enemy")
            {
                // We touched something!
    
                // -- Get the laser length
                currentLaserSize = Vector2.Distance(hit.point, this.transform.position);
    
                // -- Create the end sprite
                if (end == null)
                {
                    end = Instantiate(laserEnd) as GameObject;
                    end.transform.parent = this.transform;
                    end.transform.localPosition = Vector2.zero;
                }
            }
            else
            {
                // Nothing hit
                // -- No more end
                if (end != null) Destroy(end);
            }
    
            // Place things
            // -- Gather some data
            float startSpriteWidth = start.GetComponent<Renderer>().bounds.size.x;
            float endSpriteWidth = 0f;
            if (end != null) endSpriteWidth = end.GetComponent<Renderer>().bounds.size.x;
    
            // -- the middle is after start and, as it has a center pivot, have a size of half the laser (minus start and end)
            middle.transform.localScale = new Vector3(currentLaserSize - startSpriteWidth, middle.transform.localScale.y, middle.transform.localScale.z);
            middle.transform.localPosition = new Vector2((currentLaserSize / 2f), 0f);
    
            // End?
            if (end != null)
            {
                end.transform.localPosition = new Vector2(currentLaserSize, 0f);
            }
    
        }
    }
    

    脚本应具有如下gif所示的效果:

    enter image description here

    但是我的激光只在物体正前方探测到物体,当激光的起始部分真正接触到敌人时,它才探测到 Debug.Log Enemy 另一方面,这是未知的。

    我获得了gif所示的理想效果,但我的光线投射在发射激光的玩家站在敌人旁边之前不会检测到任何敌人。

    谢谢

    1 回复  |  直到 6 年前
        1
  •  5
  •   Savaria    6 年前

    Physics.Raycast() boolean ,而不是 RaycastHit .

    使用以下语法返回碰撞器的信息:

    public static bool Raycast(Vector3 origin, Vector3 direction, out RaycastHit hitInfo, float maxDistance);

    此代码检查光线投射是否击中了某个物体,并将碰撞器的信息存储在hit中:

    RaycastHit hit;
    
    if(Physics.Raycast(transform.position, laserDirection, out hit, maxLaserSize))
    {
        Debug.Log(hit.collider.tag);
    }