- Home /
How can I change camera when colliding with "X" object?
Hi, I'm new to Unity, I'm trying to make a Sonic Fan-game but I don't know programation (I know I should have that knowledge if I want to create a game, I'm sorry). The thing is that I want to change to "B" camera (Or the angle of the main one) by entering to "X" object and go back to "A" camera by exiting "X" object
As you can see, I have a "Loop de loop" in here and I want to change the camera angle so the player doesn't have any problem by seeing where they going while they're running throught it.
(Also, English is not my mother language, if there are any orthographical errors is because of that)
Answer by ModLunar · Jul 12, 2018 at 03:41 AM
One possibility is having a trigger collider to cover the entire ramp's volume -- so you can detect the player getting on/off the ramp. So you can create an empty GameObject somewhere around the center of the ramp. You can add a Collider to that new object (say a BoxCollider or SphereCollider for example), and mark "Is Trigger" to true (checked) in their inspector. Marking a Collider as a trigger will make it not a physical collider that pushes you, but instead, it becomes one that can detect other Colliders entering/leaving its volume.
So if you have that trigger Collider, you can create a new MonoBehaviour C# script and attach it to the same GameObject. In that C# class, you can create methods to catch Unity's special "messages". Just like Awake(), Start(), and Update() are magically called from Unity for you, you can define methods called "OnTriggerEnter", "OnTriggerStay", and "OnTriggerExit". These give you information about other Colliders entering and leaving its volume. More specifically, here's the signature of those methods:
//Gets called when a Collider enters the trigger's volume
public void OnTriggerEnter(Collider other) {
}
//Gets called every physics update where another Collider is still inside the trigger's volume
public void OnTriggerStay(Collider other) {
}
//Gets called when a Collider leaves the trigger's volume
public void OnTriggerExit(Collider other) {
}
The argument given to each of the method, "Collider other", is the other Collider that has come into contact or lost contact with the current trigger Collider. so maybe you can have an if statement to ask if the other Collider belongs to the player's GameObject. If that is so, then you can make the currently active Camera disabled and enable the Camera for the ramp -- something like this:
public class RampTriggerVolume : MonoBehaviour {
public Camera normalCamera;
public Camera rampCamera;
public void OnTriggerEnter(Collider other) {
//When the player gets close enough (into this trigger's volume)
//then we turn on the ramp Camera and temporarily turn off the normal one
if (other.gameObject.tag == "Player") {
SwitchToRampCamera();
}
}
public void OnTriggerExit(Collider other) {
if (other.gameObject.tag == "Player") {
SwitchToNormalCamera();
}
}
private void SwitchToRampCamera() {
rampCamera.enabled = true;
normalCamera.enabled = false;
}
private void SwitchToNormalCamera() {
rampCamera.enabled = false;
normalCamera.enabled = true;
}
}
This script/class (which I called "RampTriggerVolume") will switch the cameras immediately if the trigger detects that a Collider, attached to a GameObject that has the tag "Player" in the inspector, enters/exits its volume. Hopefully this is enough to get you started!
And by the way, to use this RampTriggerVolume script, drag the script onto the same GameObject as the trigger Collider. You should also add a Rigidbody component marked as "Kinematic" (isKinematic should be true). Two Colliders will ignore each other if neither of them have a Rigidbody -- at least 1 of them need a Rigidbody for the "OnTriggerXXX" events to get called. Being Kinematic will make the ramp's trigger Collider not actually receive forces, so it will stay fixed in place (unless you have your own scripts moving it or something)
P.S. - Your English is pretty good! Probably better than I would be able to do in your language xD bravo for trying!