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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by KitsuShadow · Jan 02, 2014 at 04:20 PM · collidermeshontriggerentermeshcollider

How to make a trigger when a Collider hits a different collider

public GameObject planetone;

//Makes Planet One

 void Planetone () {
 
 int coinflip = Random.Range(1,3);
 
 if (coinflip == 1){
 
 int size = Random.Range (300, 500);
 
 //int x = Random.Range(-8000,-10000);
 
 int x = -8000;
 
 int y = 0;
 
 int z = 0;
 
 planetone = GameObject.CreatePrimitive(PrimitiveType.Sphere);
 
 planetone.name = "Planet One";
 
 planetone.transform.localScale = new Vector3 (size, size, size);
 
 planetone.transform.position += new Vector3(x,y,z);
             
 SphereCollider ponesSphereCollider = planetone.GetComponent();
 
 ponesSphereCollider.radius = 2;
 
 ponesSphereCollider.isTrigger = true;
             
 Rigidbody ponesRigidBody = planetone.AddComponent();
 
 ponesRigidBody.constraints = RigidbodyConstraints.FreezePositionY;
 
 ponesRigidBody.angularDrag = 0;
 
 ponesRigidBody.useGravity = false;
 
     }
 
 }

 void OnTriggerEnter (Collider planetone) {
     Application.LoadLevel ("PlanetOne");
     }

I want to make it so that when another object with a collider or rigidbody passes through planet ones sphere collider it will switch to the PlanetOne Scene.

do i need to put anything in reference to the void OnTriggerEnter (Collider planetone) { Application.LoadLevel (1); } in my Start or Update ? and if so, how should i write it

Comment
Add comment · Show 4
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 Key_Less · Jan 02, 2014 at 11:53 PM 0
Share

It may be possible that the trigger event is registering, but Applicaion.LoadLevel() isn't working as expected. I would suggest adding a simple output message to ensure the OnTriggerEnter callback is/isn't executing.

 // Put inside OnTriggerEnter.
 Debug.Log("Trigger has been hit.");

In case the trigger is being hit, make sure you have added your "PlanetOne" scene into the build settings by going to File -> Build Settings... and dragging and dropping your scene onto the Scenes In Build window, otherwise it will not load. Once you've added the scene, it will be assigned an index (to the right) that you should use to load your scene. For example:

 Application.LoadLevel(1); // Or whatever index was assigned.
avatar image KitsuShadow · Jan 03, 2014 at 12:21 AM 0
Share

Where should I put the OnTriggerEnter function? in Update? and if so what should the return value be. Also I fixed Application.LoadLevel (1);

avatar image Key_Less · Jan 03, 2014 at 12:47 AM 1
Share

Nope, you can leave your OnTriggerEnter() method as is, there is no need to call this yourself as Unity will do it for you once a collider enters the trigger.

There are a few main things you need to keep in $$anonymous$$d for the trigger to work:

  • The script with your OnTriggerEnter() callback needs to be attached to an object containing a collider that is flagged as "Is Trigger".

  • Both objects (the trigger and the object colliding with it) need a collider component, and one of the colliders $$anonymous$$UST also have a rigidbody component in order for the trigger event to be sent.

avatar image Key_Less · Jan 03, 2014 at 02:04 AM 0
Share

Also, I've just noticed on Line 31 that you'll want to specify which component you are adding to the object.

 Rigidbody ponesRigidBody = planetone.AddComponent<Rigidbody>();

The same goes for when getting a component from the object, such as Line 25.

 SphereCollider ponesSphereCollider = planetone.GetComponent<SphereCollider>();

Ins$$anonymous$$d of creating primitives, colliders, and rigidbodies inside of the PlanetOne function. I would highly recommend creating a Sphere in Unity, adding all of the needed components such as the sphere collider and rigidbody, and set each components properties in the Inspector. Then attach this script to the Sphere object and initialize your values inside of Start().

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by haim96 · Jan 02, 2014 at 04:43 PM

did you check that the colider marked as trigger on your gameobject?

Comment
Add comment · Show 3 · 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 KitsuShadow · Jan 02, 2014 at 09:43 PM 0
Share

Yes its marked as trigger when I run the script.

do i need to put anything in reference to the void OnTriggerEnter (Collider planetone) { Application.LoadLevel ("PlanetOne"); } in my Start or Update ? and if so, how should i write it

avatar image haim96 · Jan 02, 2014 at 10:33 PM 0
Share

did you add "PlanetOne" scene to the project scene list?

avatar image KitsuShadow · Jan 02, 2014 at 10:51 PM 0
Share

Yes, its there. I think im missing something with the trigger function and it not being used in Start or Update.

avatar image
0

Answer by KitsuShadow · Jan 03, 2014 at 03:47 AM

So as it stands now i can make the scene transfer but its instantaneous. I changed the code to look like this and now it wont scene transfer again.

public class SpawnSun : MonoBehaviour {

 public GameObject sun;
 public GameObject planetone;
 public SphereCollider ponesSphereCollider;
 public GameObject planettwo;
 public GameObject planetthree;
 public GameObject planetfour;
 public GameObject planetfive;
 public GameObject planetsix;
 public GameObject planetseven;
 public GameObject planeteight;
 public GameObject planetnine;
 private float BaseOrbitSpeed = 0.05f;


void Planetone () {

     int coinflip = Random.Range(1,3);

     if (coinflip == 1){

         int size = Random.Range (300, 500);

         //int x = Random.Range(-8000,-10000);
         int x = -8000;
         int y = 0;
         int z = 0;

         planetone = GameObject.CreatePrimitive(PrimitiveType.Sphere);
         planetone.name = "Planet One";
         planetone.transform.localScale = new Vector3 (size, size, size);
         planetone.transform.position += new Vector3(x,y,z);
         
         ponesSphereCollider = planetone.GetComponent<SphereCollider>();
         ponesSphereCollider.radius = 2;
         ponesSphereCollider.isTrigger = true;
         
         Rigidbody ponesRigidBody = planetone.AddComponent<Rigidbody>();
         ponesRigidBody.constraints = RigidbodyConstraints.FreezePositionY;
         ponesRigidBody.angularDrag = 0;
         ponesRigidBody.useGravity = false;
         //ponesRigidBody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;

     }

 }

 void OnTriggerEnter (Collider ponesSphereCollider) {

     if (ponesSphereCollider.CompareTag("Player")) {

         Application.LoadLevel (1);
         Debug.Log ("Trigger has been hit.");
     }


     }

void Update() { OnTriggerEnter (ponesSphereCollider); }

Comment
Add comment · Show 6 · 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 Key_Less · Jan 03, 2014 at 05:25 AM 1
Share

When the trigger event is sent, the Collider parameter will contain the collider info of the object that has entered the trigger. So, make sure the object that is colliding with the trigger has it's tag set to "Player"

 if(ponesSphereCollider.gameObject.tag == "Player")
 {
    Application.LoadLevel(1);
 }

$$anonymous$$ind you, as soon as an object with a tag "Player" collides with the trigger, your "PlanetOne" level will load immediately. If you want to delay the loading, you could use a coroutine or Invoke method, for example:

 void OnTriggerEnter (Collider ponesSphereCollider) 
 {
    if (ponesSphereCollider.gameObject.tag == "Player") 
    {
       // This will call the LoadLevel method after 3 seconds.
       Invoke("LoadLevel", 3);
    }
 }
 
 void LoadLevel()
 {
    Debug.Log ("Level 1 is loading.");
    Application.LoadLevel (1);
 }
avatar image KitsuShadow · Jan 03, 2014 at 05:42 AM 0
Share

I took your suggestions and edited my script. See the attached video for my current issue. Still wont trigger.

http://www.twitch.tv/kitsushadow/c/3486408

avatar image KitsuShadow · Jan 03, 2014 at 06:22 PM 1
Share

EXCELLENT, WERE IN BUSINESS!

Thank You Very $$anonymous$$uch $$anonymous$$ey_Less

avatar image Key_Less · Jan 03, 2014 at 06:23 PM 0
Share

I'm not sure if I'm completely understanding your issue, but as long as your object is active, the script will always run.

Anyways, you can attach scripts to objects by using AddComponent.

 // This adds the "YourScript" component to THIS game object.
 var scriptHandle = gameObject.AddComponent<YourScript>();
 scriptHandle.CallYourScript$$anonymous$$ethod();

If you want to add the component to another object, make sure you have a valid handle to that object by using a public GameObject variable and assigning that object by dragging and dropping it onto your main script via the Unity Inspector.

 // Attach this script to an object in the scene.
 public class $$anonymous$$ainScript : $$anonymous$$onoBehaviour
 {
    // Assign this variable by dragging and dropping your
    // game object onto it via the Unity Inspector.
    public GameObject yourGO;
 
    void Start ()
    {
       // $$anonymous$$ake sure you have a valid handle to your game obj.
       if(yourGO != null)
       {
          // You can then add your scripts to the game obj.
          yourGO.AddComponent<YourScript>();
       }
    }
 }
avatar image Key_Less · Jan 03, 2014 at 07:04 PM 0
Share

I've converted the comment to an answer, I'd very much appreciate it if you could mark it as answered so that it may be of use to others...and so I can gain more karma of course lol.

Show more comments
avatar image
0

Answer by Key_Less · Jan 03, 2014 at 05:23 PM

Cool, thanks for the video. Looks like everything is setup correctly in the scene except for one thing. I didn't see your custom script attached to the "PlanetOne" game object. In order for the trigger event to occur, you must attach your script containing the OnTriggerEnter() method to the game object that has the collider with the "Is Trigger" flag set to true.

alt text

In the picture, I've created a script called "YourScript" and have attached it to my "PlanetOne" game object. Once you select your game object, you can just drag and drop the script into the inspector window to attach it.

Inside of "YourScript" is the OnTriggerEnter() method. You do not need to call this yourself, so you can remove it from your update call. When the collision occurs, Unity will send a trigger event and your OnTriggerEnter() method will execute. Let me know if you run into any issues.


customscript.png (34.4 kB)
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

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

mesh collider not working 2 Answers

Collision mesh data problem in Unity WebPlayer 2 Answers

Particles going through mesh collider 1 Answer

Correct position of overlapping meshes 0 Answers

Problem with convex Mesh Colliders 0 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