- Home /
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
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.
Where should I put the OnTriggerEnter function? in Update? and if so what should the return value be. Also I fixed Application.LoadLevel (1);
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.
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().
Answer by haim96 · Jan 02, 2014 at 04:43 PM
did you check that the colider marked as trigger on your gameobject?
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
Yes, its there. I think im missing something with the trigger function and it not being used in Start or Update.
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); }
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);
}
I took your suggestions and edited my script. See the attached video for my current issue. Still wont trigger.
EXCELLENT, WERE IN BUSINESS!
Thank You Very $$anonymous$$uch $$anonymous$$ey_Less
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>();
}
}
}
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.
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.
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.
Your answer
Follow this Question
Related Questions
mesh collider not working 2 Answers
MeshCollider has different shape than MeshFilter 1 Answer
Colliders on this spider 1 Answer
How to change meshcollider thickness 2 Answers
How to correct add collider to every child of gameobject? 1 Answer