Search Unity

Archery Game Spawn Arrow Too Close Issue And Arrow Colliding with Other Arrows

Discussion in 'Scripting' started by joeyxiong1994, Aug 3, 2019.

  1. joeyxiong1994

    joeyxiong1994

    Joined:
    Apr 18, 2017
    Posts:
    20
    Hi there, I'm making an archery game which will spawn arrow instance based on the prefab I provided and shoot forward along with my Camera.main.transform.forward. And when my arrow shoot something, my arrow will be converted to *isKinematic* and *boxCollider.isTrigger* which make the arrow immediately stop and stick to which it shoot. And isTrigger can prevent from collision between new arrows with this one. Also prevent my arrow collider with player after it already shoot something.

    I encounter an issue that as my arrow has a box collider and rigidbody attached. My arrow spawn pos is just a little bit in front of my character in order to avoid collision between my character and the arrow. However, if I stand against a wall. Like the shoot spawn pos is somehow inside the wall which is another box collider attached. The arrow will shoot weird.

    I know this might be a game design question but anyone has idea about how to handle this issue? How should I deal with this situation.

    Second question is how to prevent arrow collide with other arrows while it's still in air. As the arrow is only converted to be isTrigger after it shoots something. So in air, it's possibly hit other arrows, collides with them, and then change direction.

    I previously design it to use trigger all the time, however, I found if trigger all the time, onTriggerEnter will not work if the arrow has a high speed. It penetrate a thin wall. So I have to use collision. My ideal situation is even a Quad also can stop the arrow with a high speed
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    You could possible solve both problems with the same solution: Physics layers. Under the Physics settings for the game, you can choose which layers interact with other layers. Let's say you create a new layer named "Arrows", and assign it to your arrow game object. You can then configure the Arrows layer so that it doesn't collider with other objects on the Arrows layer, but it will still collider with other layers, like the Default layer. This means you'd never need to set the collider to IsTrigger.

    That would definitely solve the issue of arrows colliding with each other. You could also put the player on another layer and make arrows not collide with the player. That way, you wouldn't need to set the arrow spawn position so carefully, as the arrow won't collide with the player.

    As for hitting walls, that could be more complex, as you probably always want the arrows to hit the wall. One easy approach might be to start the arrow as IsTrigger, and then switch to be a normal collider a half second (or less) after shooting. That could still result in cases where you could shoot through objects, though.
     
    joeyxiong1994 likes this.
  3. joeyxiong1994

    joeyxiong1994

    Joined:
    Apr 18, 2017
    Posts:
    20

    Thanks so much for your reply! I think this would solve my issue. And an addition question, I want the arrow finally will collide with player as I need to detect if the player is got shot by other player. If I set them not collide with each other from layers, do I still get message from OnCollisionEnter()? If not, should I change player colliding with arrow layer back to normal after a half second
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    You're correct that if you have multiple players, and they're all on the same layer, then the arrows won't hit any of the players. So if you have multiple players, this won't work properly. Depending on how you're doing multiplayer, and how complex your game is, you could put the players on different layers, but that sounds a little messy to me. So you might need to resort to enabling the collider only after it has moved a small distance from the player.

    How many players are in your game, by the way? And it is same-screen multi-player, or network-based multiplayer?
     
  5. joeyxiong1994

    joeyxiong1994

    Joined:
    Apr 18, 2017
    Posts:
    20
    Currently there's one player in my scene, but I have some level that will spawn arrow to attack my player.

    And my final goal would be network-based multiplayer.