Hi there,
I have first person shooting game where the camera is fixed. The player shoots at moving shapes.
-I have a cube that has a rigidbody and a bunch of child colliders that break it up into zones.
-I also have invisible walls that prevent the shapes from moving off camera or too close to the camera (and therefore the bullet must always pass through one or more invisible walls to get to a shape). The invisible walls are box colliders.
-When the bullet is fired, I use IgnoreCollision on all the invisible walls with the bullet.
-The bullet has a rigidbody with continuous dynamic collision detection and a sphere collider
-The cube has discrete collision detection (rigidbody/collider setup mentioned above)
This works fine when the shape is near the center of the screen or has distance from the invisible walls. However if the shape is very close to an invisible wall and very close to the edge of the screen (sharp angle from projectile), the bullet will pass through the shape and hit the wall behind it. I have attached to both of them this “DontGoThroughThings.js” script that creates raycasts every frame to ensure that it will not pass through things:
// http://www.unifycommunity.com/wiki/index.php?title=DontGoThroughThings
#pragma strict
var layerMask : LayerMask; //make sure we aren't in this layer
var skinWidth : float = 0.1; //probably doesn't need to be changed
private var minimumExtent : float;
private var partialExtent : float;
private var sqrMinimumExtent : float;
private var previousPosition : Vector3;
private var myRigidbody : Rigidbody;
//initialize values
function Awake() {
if (name.Contains("zone")) {
myRigidbody = transform.parent.rigidbody;
}
else {
myRigidbody = rigidbody;
}
previousPosition = myRigidbody.position;
minimumExtent = Mathf.Min(Mathf.Min(collider.bounds.extents.x, collider.bounds.extents.y), collider.bounds.extents.z);
partialExtent = minimumExtent*(1.0 - skinWidth);
sqrMinimumExtent = minimumExtent*minimumExtent;
}
function FixedUpdate() {
//have we moved more than our minimum extent?
var movementThisStep : Vector3 = myRigidbody.position - previousPosition;
var movementSqrMagnitude : float = movementThisStep.sqrMagnitude;
if (movementSqrMagnitude > sqrMinimumExtent) {
var movementMagnitude : float = Mathf.Sqrt(movementSqrMagnitude);
var hitInfo : RaycastHit;
//check for obstructions we might have missed
if (Physics.Raycast(previousPosition, movementThisStep, hitInfo, movementMagnitude, layerMask.value)) {
if (hitInfo.transform.gameObject.tag!="InvisWall") {
myRigidbody.position = hitInfo.point - (movementThisStep/movementMagnitude)*partialExtent;
}
}
}
previousPosition = myRigidbody.position;
}
Does anyone have any insight into why this might happen?
Thanks,
Candace
EDIT
Solution: Removed invisible walls and changed shapes to follow a waypoint system for movement.