- Home /
OnTriggerEnter2D Collision not working
I have a mouse running around and avoiding lasers that turn on and off, but the collision between the mouse and when the laser is on is not working. The mouse has a 2D circle collider and ridgidbody2D. The laser has a 2D box collider and "is Trigger" is selected. The first piece of code is for everything relating to the laser. The second part is related to the mouse's movement and interactions. The print statements for the mouse code never show up.
using UnityEngine; using System.Collections;
public class LaserScript : MonoBehaviour {
//1
public Sprite laserOnSprite;
public Sprite laserOffSprite;
//2
public float interval = 0.5f;
public float rotationSpeed = 0.0f;
//3
private bool isLaserOn = true;
private float timeUntilNextToggle;
// Use this for initialization
void Start ()
{
timeUntilNextToggle = interval;
}
// Update is called once per frame
void Update () {
}
void FixedUpdate () {
//1
timeUntilNextToggle -= Time.fixedDeltaTime;
//2
if (timeUntilNextToggle <= 0)
{
//3
isLaserOn = !isLaserOn;
//4
GetComponent<Collider2D>().enabled = isLaserOn;
//5
SpriteRenderer spriteRenderer = ((SpriteRenderer)this.GetComponent<Renderer>());
if (isLaserOn){
spriteRenderer.sprite = laserOnSprite;
}
else{
spriteRenderer.sprite = laserOffSprite;
}
//6
timeUntilNextToggle = interval;
}
//7
transform.RotateAround(transform.position, Vector3.forward, rotationSpeed * Time. fixedDeltaTime);
}
}
public class MouseController : MonoBehaviour {
public float jetpackForce = 75.0f;
public float forwardMovementSpeed = 3.0f;
public Transform groundCheckTransform;
private bool grounded;
public LayerMask groundCheckLayerMask;
Animator animator;
public ParticleSystem jetpack;
private bool dead = false;
private Rigidbody2D mouse;
void FixedUpdate ()
{
bool jetpackActive = Input.GetButton("Fire1");
jetpackActive = jetpackActive && !dead;
if (jetpackActive)
{
mouse.AddForce(new Vector2(0, jetpackForce));
}
if (!dead)
{
Vector2 newVelocity = mouse.velocity;
newVelocity.x = forwardMovementSpeed;
mouse.velocity = newVelocity;
}
UpdateGroundedState();
AdjustJetpack(jetpackActive);
}
// Use this for initialization
void Start ()
{
mouse = gameObject.GetComponent<Rigidbody2D> ();
animator = GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
}
void UpdateGroundedState()
{
//1
grounded = Physics2D.OverlapCircle(groundCheckTransform.position, 0.1f, groundCheckLayerMask);
//2
animator.SetBool("Grounded", grounded);
}
void AdjustJetpack (bool jetpackActive)
{
jetpack.enableEmission = !grounded;
jetpack.emissionRate = jetpackActive ? 300.0f : 75.0f;
}
void onTriggerEnter2D(Collider2D collider)
{
if (collider.name == "laser") {
HitByLaser ();
print ("hit");
}
}
void HitByLaser()
{
dead = true;
print ("dead");
}
}
Answer by davient · Mar 12, 2015 at 08:46 AM
Easy to miss this, the compiler wont complain - but the collision/trigger function names that you implement are all case sensitive. You just need to capitalize the mouse code **On**TriggerEnter2D
void onTriggerEnter2D(Collider2D collider)
becomes
void OnTriggerEnter2D(Collider2D collider)
Your answer
Follow this Question
Related Questions
Alternative OnTriggerEnter2D for class instances 0 Answers
OnTriggerEnter2D works without being actually triggered. 0 Answers
how do I enable collision? 0 Answers
Tilemap Collider 2D preventing objects from moving 2 Answers
OnTriggerEnter2D not working 2 Answers