I instantiate a prefab that has a mesh collider and an initial velocity. The clone can go through the enemies; damaging and applying a force only once! I tried the following code:
function OnTriggerEnter(enemy: Collider) {
Debug.Log(enemy.transform.GetInstanceID());
Physics.IgnoreCollision(enemy, transform.root.collider);
if (enemy.gameObject.layer == LayerMask.NameToLayer("Enemies")) {
var forceDirection : Vector3 = transform.TransformDirection(Vector3.forward);
enemy.rigidbody.AddForce(forceDirection * forceMax);
enemy.SendMessage("ApplyDamage", ammo.GetDamage());
}
}
What I want is that after the clone collides with an enemy, it will still continue moving on but ignoring all collisions with enemies that have collided with it already.
What happens right now is that when the clone hits an enemy, the enemy is continuously ‘hit’ until it dies. I checked the Debug.Log() to check how many times it hit the same object. And apparently, it keeps on hitting the enemy object until it dies.
Did I make sense? Should Physics.IgnoreCollision() be called outside OnTriggerEnter()?
Hmm… the solution you gave me will instantly disable the OnCollisionEnter() on any first hit. What I want to happen is to Ignore further collisions between the current collider upon initial collision. Collision detection with all other game objects should not be disabled until first hit.
The code I’ve given above doesn’t work properly.
Is there a special condition on the usage of Physics.IgnoreCollision()?
EDIT:
I currently implemented my own system of selective ignore collision by keeping an array of gameobject instance ids that have already been hit. Before doing any processing in OnTriggerEnter(), I check if the collider’s id already exists in the array.
not sure if this will help anyone after 7 years. I got the same Problem today. In a 2D game a OnTriggerEnter2D Event was fired multiple times. I wanted the Event was fired only once while the two bodies got a collision. So after a while thinking and trying complex ways around the solution was very simple and worked well.
I am very noob at coding, but if I am looking to use your code, do I need to state what isInCollision? I am currently, looking to make a game with boxes falling from the sky to the ground, and when it lands (aka. collide with the ground) I want the screen to shake. I have a CameraShake script that is attached to an empty object (I call gameManager, that has a DoShake function, and a ShakeOnCollision script that is attached to the boxes. I tried to use your code in the ShakeOnCollision script, while also calling the DoShake function from the CameraShake script.
ShakeOnCollision:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShakeOnCollision : MonoBehaviour
{
public float camShakeAmt = 0.1f;
public float camShakeLength = 0.1f;
CameraShake camShake;
public CameraShake shake; //Calling DoShake function from the CameraShake script
bool isInCollision = false;
void Update()
{
if (isInCollision == false)
{
isInCollision = true;
shake.DoShake();
}
else
isInCollision = false;
}
}
CameraShake:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraShake : MonoBehaviour {
public Camera mainCam;
float shakeAmount = 0;
void Awake()
{
if (mainCam == null)
mainCam = Camera.main;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.T))
{
Shake(0.1f, 0.2f);
}
}
public void Shake(float amt, float length)
{
shakeAmount = amt;
InvokeRepeating("DoShake", 0, 0.001f);
Invoke("StopShake", length);
}
public void DoShake()
{
if (shakeAmount > 0)
{
Vector3 camPos = mainCam.transform.position;
//float offsetX = Random.value * shakeAmount * 2 - shakeAmount;
float offsetY = Random.value * shakeAmount * 2 - shakeAmount;
//camPos.x += offsetX;
camPos.y += offsetY;
mainCam.transform.position = camPos;
}
}
void StopShake()
{
CancelInvoke("DoShake");
mainCam.transform.localPosition = Vector3.zero;
}
}
I guess long story short, how do i properly implement your script to make the camera shake ONCE when the box lands (to simulate weight/ feedback)?