Cannot make physics.spherecast ignore object.
Turret use spherecast to detect enemy and fire. The problem is after firing shell, spherecast will return shell as hit object, i need spherecast to ignore the shell.
In the editor, project setting, physics, ignore raycast and shell is unticked , but it does't work.
I tried this and that for few hrs, but still cannot solve it.
void Fire()
{
RaycastHit hit;
if (Physics.SphereCast(barrel.position, shellRadius, direction, out hit, attackRange))
{
s=hit.transform.name;
if(nextFire < Time.time && hit.transform.gameObject.layer ==enemyLayer)
{
nextFire = Time.time + fireRate;
GameObject shell = Instantiate(Resources.Load("RailGunShell"), barrel.position, barrel.rotation) as GameObject;
shell.GetComponent<Rigidbody>().AddForce(direction * shellSpeed);
}
}
Answer by ScaniX · Sep 07, 2016 at 12:30 PM
The physics layer settings are for collisions only. To be ignored in normal raycasts you can either put your object into the IgnoreRaycast layer, or in your case: Use a signature of the raycast method that includes a layermask parameter:
int SHELL_LAYER = 12; // your layer index here
int layerMask = ~(1 << SHELL_LAYER);
if (Physics.SphereCast(barrel.position, shellRadius, direction, out hit, attackRange, layerMask))
...
Your answer
Follow this Question
Related Questions
XCode Linking Errors. Unity 5.2.3f1 XCode 7.2 1 Answer
Instatiated object not being referenced in Start function? 2 Answers
Hello How can I Save Player position and a Gameobject which is a UI Text? 0 Answers
Showing a UI canvas when player enters a specific location 2 Answers
Draw snapping lines 0 Answers