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
1
Question by smirlianos · Dec 28, 2012 at 09:54 PM · collisionphysicscolliderinputtag

What's wrong with OnCollisionEnter?

I have this script attached to the player, but when he touches a cube(with the tag up or down) nothing happens. Why?

 var speed : float;
 
 var touchingUp : boolean;
 var touchingDown : boolean;
 
 function Update () {
     if(Input.GetButton("up") && !touchingUp)
     {
         transform.Translate(0, 2 * Time.deltaTime, 0);
     }
 
     if(Input.GetButton("down") && !touchingDown)
     {
         transform.Translate(0, -2 * Time.deltaTime, 0);
     }
 
 }
 
 function OnCollisionEnter (other : Collision) {
     if (other.tag == "up") {
         touchingUp = true;
     }
         if (other.tag == "down") {
         touchingDown = true;
     }
 }
 
 function OnCollisionExit (other : Collision) {
     if (other.tag == "up") {
         touchingUp = false;
     }
         if (other.tag == "down") {
         touchingDown = false;
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Seth-Bergman · Dec 28, 2012 at 10:23 PM

there are several possibilities:

1: the tag is not actually set on the object

2: more likely, one of your objects needs to have a RIGIDBODY attached. For a better idea of exactly which object combinations will send collision messages, look here:

http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html

the collision matrix at the bottom is very helpful for understanding this

3: obviously, both objects must have a collider, NOT SET to "isTrigger"

here is another helpful link on the subject:

http://unitygems.com/mistakes1/

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
avatar image
0

Answer by aldonaletto · Dec 28, 2012 at 10:38 PM

The player must have a rigidbody attached in order to generate OnCollision events. If it's a simple object (no Rigidbody, no CharacterController), add a Rigidbody to it and change the control code a little:

 function Update () {
     var speed = 0;
     if (Input.GetButton("up") && !touchingUp)
     {
        speed = 2;
     }
     else
     if (Input.GetButton("down") && !touchingDown)
     {
        speed = -2;
     }
     rigidbody.velocity = Vector3(0, speed, 0);
 }

Another possibility is to add a CharacterController instead of the Rigidbody - you would not even need the OnCollision events to make it be constrained by the up and down objects: a CharacterController doesn't go through other colliders when you move it with Move, like this:

 function Update () {
     var speed = 0;
     if (Input.GetButton("up"))
     {
        speed = 2 * Time.deltaTime;
     }
     else
     if (Input.GetButton("down"))
     {
        speed = -2 * Time.deltaTime;
     }
     GetComponent(CharacterController).Move(Vector3(0, speed, 0));
 }

NOTE: If using a CharacterController, OnCollision events aren't generated - you should use OnControllerColliderHit instead.

Comment
Add comment · Show 2 · 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
avatar image smirlianos · Dec 29, 2012 at 11:44 AM 0
Share

Thanks, but if I use a character controller, it applies gravity to the object

avatar image aldonaletto · Dec 29, 2012 at 04:43 PM 1
Share

No, you must add the gravity in your script when using $$anonymous$$ove - only Simple$$anonymous$$ove includes gravity automatically.

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

10 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

Related Questions

Mouse Movement with physics 1 Answer

How to automatically add physics/colliders to a model of a map 1 Answer

Colliders don't work 1 Answer

How to get collisions on Character controller? 1 Answer

Is it possible to tell Unity physics to ignore a collision at collision time? 1 Answer


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