简体   繁体   English

Unity Raycast2D忽略了实例化的预制对象

[英]Unity Raycast2D ignored instantiated Prefab object

I'm currently making a small game for a university assignment. 我目前正在为大学分配的小游戏。 It's a simple 2D game where you shoot birds using the mouse. 这是一个简单的2D游戏,您可以使用鼠标射击鸟类。

I did collission detection by hand at first (check for x/y boundaries) but now I wanna use a collider. 首先,我手动进行了碰撞检测(检查x / y边界),但现在我想使用对撞机。

So I added a CircleCollider2D to my objects and I perform the Raycast like so 所以我将CircleCollider2D添加到对象中,然后像这样执行Raycast

Vector3 mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);

RaycastHit2D[] hits = Physics2D.RaycastAll (mousePosition, Vector3.forward, Mathf.Infinity);
        Debug.Log (hits.Length);

Now when I add my prefabs manually into my scene it works as expected. 现在,当我将预制件手动添加到场景中时,它可以按预期工作。 The log outputs 1, or even 2 if I have multiple birds on top of each other. 日志输出1,或者如果我彼此顶着多只鸟,则输出2。

But my game instantiated a lot of birds randomly and they are not detected when performing the raycast. 但是我的游戏随机地实例化了很多鸟,并且在进行光线投射时没有发现它们。 I already checked, they definitely have colliders. 我已经检查过,他们肯定有对撞机。

The instantiation code looks like the following: 实例化代码如下所示:

Bird bird = Instantiate (birdPrefab, birdPosition, Quaternion.identity) as Bird;

I googled around for 2 hours now, but nothing could actually solve my problem. 我在Google上搜索了2个小时,但是没有任何东西可以真正解决我的问题。

I don't know if this is important, but the collision detection does not happen within the bird object's attached script. 我不知道这是否重要,但是碰撞检测不会在鸟对象的附加脚本内发生。

I'm really cofused because it works with manually put objects, but not with instantiated ones. 我真的很困惑,因为它适用于手动放置的对象,但不适用于实例化的对象。 Has anybody experienced this before? 有人经历过吗?

Edit: 编辑:

The mouse position is on (x, y, -10) and the birds are on (x, y, 0) and both the manually placed ones and the instantiated ones are direct children of the scene. 鼠标位置在(x,y,-10)上 ,鸟类在(x,y,0)上 ,手动放置的和实例化的都是场景的直接子代。

Edit2: 编辑2:

Bird start 鸟开始

// Use this for initialization
void Start () {
    this.animator = this.GetComponent<Animator> ();

    this.camera = Camera.main;

    this.rigidBody = this.GetComponent<Rigidbody2D> ();
    // get the ammunition script
    this.ammunition = GameObject.Find ("Ammunition").GetComponent<Ammunition> ();
    // get the score script
    this.score      = GameObject.Find("Score").GetComponent<Score>();
}

Manually Placed Bird 手动放鸟 手动放鸟

Instantiated Bird 实例化鸟 小鸟

Both have the collider and both are on the same Z level for the RayCast. 两者都有对撞机,并且在RayCast中都处于相同的Z轴水平。

There do not seem to be any issues with your raycast or instantiation of the Bird, given the information you have provided. 根据提供的信息,您的射线广播或“鸟”的实例化似乎没有任何问题。 This leads me to believe there is a problem elsewhere in the code or in the way the scene is set up, especially if the non-instantiated bird works as expected. 这使我相信代码中其他地方或场景设置方式存在问题,特别是如果未实例化的鸟类按预期工作时,尤其如此。

If you can post a screenshot of both the manually placed Bird and instantiated Bird Gameobject information during gameplay , as well as any code performed on "bird" after instantiation, we can take a closer look. 如果您可以在游戏过程中张贴手动放置的Bird和实例化的Bird Gameobject信息的屏幕截图,以及实例化后在“ bird”上执行的任何代码,我们可以进行仔​​细研究。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM