Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by danderson865 · Mar 09, 2015 at 11:46 AM · collisioncollider2dontriggerenter2d

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");
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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)


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges