Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 ITInnovationTeam · Apr 12, 2015 at 09:35 PM · javascriptcollisionrigidbodycollideristrigger

How do I pass through certain colliders but detect all collisions?

I am building a driving simulator for a competition, and my car has a JS script attached to it. The script detects collisions and prints the name of the object it collided with to the console. I have large obstacles such as parked cars, a shed, trash cans, signs and more that I want the car to collide with, print to console, and stop the car using normal collision physics. However, when the car hits the many cones on the course, I want it to detect each cone it hits, detect the collision and print to console BUT have the car continue moving as if the cone's not there. My car has a rigidbody component (non-kinematic) and none of the other objects have rigid bodies. Right now my collisions work just fine but if I run into a cone it stops the car and detects the collision or after setting the cone's collider to "Is Trigger", the car passes through the cone without detection of the collision. Each cone has it's own collider if that's helpful.

Here is the JS script on my car:

 #pragma strict

 function Start () {
 print("The game has started!");
 }

 function Update () {
 
 }
 function OnCollisionEnter (col : Collision) {
     var hitObject = col.gameObject.name;
 print("I collided with the " + hitObject + " !");
 }

How can I get the car to hit large objects (everything except cones) and detect the collision AND get the car to pass through cones yet still detect the collision?

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
2

Answer by _joe_ · Apr 13, 2015 at 11:38 AM

You need to use triggers in that case, Go to all your cones (inspector), and in the collider, tick "Trigger". Now your car will pass through the cones.

For you to be able to detect the collision, you need to use OnTriggerEnter instead of OnCollisionEnter (that takes a Collider instead of a Collision object)

so your code will be as such:

  function OnTriggerEnter (col : Collider) {
      var hitObject = col.gameObject.name;
  print("I collided with the " + hitObject + " !");
  }

Comment
Add comment · Show 1 · 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 Yoyoman_Blue6 · Dec 16, 2020 at 11:27 AM 0
Share

For some reason when I try this code using UnityEngine;

 public class SideTrigger : $$anonymous$$onoBehaviour
 {
     public Game$$anonymous$$anager game$$anonymous$$anager;
 
     void OnTriggerEnter()
     {
         game$$anonymous$$anager.EndGame();
     }
 }

Yet it doesn't work for some reason it gives me an error: "NullReferenceExeption: Object reference not set to an instance of an object"

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Weird collision with objects passing through each other 1 Answer

Rigidbody colliders 1 Answer

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

My Character controller is being push a little bit when i'm about the get the coins. How can i possibly detect collision and destroy the coins without actually getting affected by the colliding physics? 1 Answer

rigid bodies passing through one another. 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