• 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
Question by thevraptor · Jan 25, 2015 at 01:42 AM · javascriptrigidbodycolliderontriggerenterontriggerenter2d

How to use OnTriggerEnter? (JS)

Ahoy,

This is a problem I have been dealing with for a while but never knew how to fix.

How do I get OnTriggerEnter to work?

In this script the player object has a rigidbody attached, so it should work, but absolutely no dice. I know it's kind of a noob question, but it's really bothering me since I need to finish this game for the GGJ 2015. The game is 2D, if that helps.

 var facingRight : boolean = true;
 var facingLeft : boolean = false;
 var jumped : boolean = false;
 var debug : boolean = false;
 var ground : Collider2D;
 
 function Update () {
 
     if (Input.GetKey(KeyCode.D)) {
         if (facingRight) {
             transform.Translate (0.1, 0, 0);
             facingRight = true;
             facingLeft = false;
         }
         if (facingLeft) {
             transform.Rotate (0, 180, 0);
             facingRight = true;
         }
     }
     
     if (Input.GetKey(KeyCode.A)) {
         if (facingLeft) {
             transform.Translate (0.1, 0, 0);
             facingLeft = true;
             facingRight = false;
         }
         if (facingRight) {
             transform.Rotate (0, 180, 0);
             facingLeft = true;
         }
     }
     
     if (Input.GetKeyDown(KeyCode.Space)) {
         if (jumped == false) {
             rigidbody2D.velocity = Vector2(0,6);
             jumped = true;
         }
     }
 }
 
 function OnTriggerStay (ground) {
 
     jumped = false;
     debug = true;
 }

Thanks.

Comment

People who like this

0 Show 0
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

Answer by persijn · Jan 25, 2015 at 02:09 AM

Question seem a bit large. http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter.html

It's triggered when you hit something with an other ridged body. Don't forget to added the type of variable to the triggered argument. Like 'ground : Collider' Dont declare a collider beforehand i'ts sendt with the event. So remove your declaration of ground. It's not needed.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can a object without a rigidbody be triggered by a trigger? 1 Answer

OnTriggerEnter works, OnTriggerExit doesn't 3 Answers

onTriggerEnter2D not acting like I thought 1 Answer

100 OnTriggerEnter Or Tags each player too much? 3 Answers

What is wrong with my HealthBar/Damage Script? 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