- Home /
LoadLevel when colliding an object and clicking on it ?
Hi all, I'm very new to c# and Unity.
I'm working on a little problem in 2D. I would like to change the scene when two thing occuring at the same time :
-my character is colliding with a game object
-the player is clicking on this game object
I have already managed to do this : when my character collides the gameobject the scene loads.
using UnityEngine;
using System.Collections;
public class transc : MonoBehaviour {
void OnCollisionEnter2D ()
{
Application.LoadLevel ("scene2");
}
}
Now, I think that I need to use the if(Input.GetMouseButton(0))
somewhere but I can't manage to make it work properly...
Maybe you guys know a good tutorial about that or some elements to make me progress ?
Thanks a lot !
Answer by Landern · Mar 09, 2015 at 12:24 PM
You might want to use:
OnCollisionStay2D(Collision2D coll)
{
if (Input.GetMouseButton(0))
Application.LoadLevel("scene2");
}
Stay is invoked each frame while the collider is inside another collider.
Unity Docs(OnCollisionStay2D):
Sent each frame where a collider on another object is touching this object's collider (2D physics only).
While on the other hand:
OnCollisionEnter2D(Collision2D coll)
{
// Some code
}
Is called on the frame when the collider enters the other collider and until it exits and re-enters, no more invocation is done.
From Unity Docs(OnCollisionEnter2D):
Sent when an incoming collider makes contact with this object's collider (2D physics only).
On other thing you may want to consider and that is putting the script on the object and making it a trigger is physics isn't a concern.