- Home /
Box collider that triggers camera change
I'm trying to make a simple 3D game where you shoot an instantiated object (sphere) towards an end object and when it hits the end object, the camera changes to another one in the same scene, simulating a level change. I'm not sure if there's an easier way to change to another level, but i'm trying to keep it as simple as possible. Do I need to add a box collider to the end goal that makes it so when the instantiated ball hits it the camera is switched?
Answer by bennett_apps · Apr 11, 2018 at 11:56 PM
Lots of ways to move the camera. Yes, you could use a trigger that moves the Camera when you hit the object in front. Is the next goal directly in front every time? If so, you could just add a certain number of units on one axis to the camera every time OnTriggerEnter () is called. Like, say the first goal is at (10, 0, 0) and the second is at (20, 0, 0) and so on, the code would look like:
void OnTriggerEnter () {
transform.position += new Vector3 (10, 0, 0);
}
You could also just switch scenes...
Answer by DuckGreatness · Apr 12, 2018 at 12:35 AM
You can have multiple cameras in your scene set where you want and switch between them. Yes I would have a collider on both objects and one of them should be a rigidbody. You can use the void OnCollisionEnter (Collision col) function attached to one of the game objects to trigger the camera switch. If you have 2 cameras, you can put a script on one (in this example the one that is not the main camera) like the following to switch on impact.
private Camera _cMainCamera;
void Start () {
_cMainCamera = GameObject.Find("Main Camera").GetComponentInChildren<Camera>();
GetComponent<Camera>().enabled = false;
_cMainCamera.enabled = true;
}
OnCollisionEnter (Collision col){
if (col.gameObject.tag == "Target"){
_cMainCamera.enabled = true;
GetComponent<Camera>().enabled = false;
}
}
Loading another scene can be very easy. Add using UnityEngine.SceneManagement; to your script and then use SceneManager.LoadScene ("NameOfYourScene"); Here is an example.
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class LoadGame : MonoBehaviour {
public void BeginAdventure (){
SceneManager.LoadScene ("WorldMapIntro");
}
}
Your answer
Follow this Question
Related Questions
Change Material depending on Collider position? 2 Answers
Why when i move the player object through the door the ontriggerenter/exit event are not fire ? 2 Answers
How to change between scripts on trigger? 2 Answers
FPS all of a sudden gets stuck in floor??? 0 Answers
Connect 2 gameobjects to interact with them simultaneously? 2 Answers