Answer the question
In order to leave comments, you need to log in
How to teleport the camera when approaching an object?
Hello! Teleportation does not work in Unity 3D. I wanted to make the camera teleport to another area when approaching a certain object, for example, a cube. I tried a bunch of options for this script, nothing comes out.
public class TELEPORT : MonoBehaviour
{
// Start is called before the first frame update
public Transform pos;
public GameObject obj;
public Camera obj1;
private void OnTriggerEnter(Collider other)
{
Camera.main.transform.position = new Vector3(0f, 0f, -550f);
Camera.main.transform.rotation = new Quaternion(0, 0, 0, 0);
other.transform.position = pos.transform.position;
Camera.main.transform.position = pos.transform.position;
}
}
Answer the question
In order to leave comments, you need to log in
What exactly doesn't work? Is pos declared in the inspector? Does the object with this script have a trigger and a rigidbody hanging on it? Moreover, both the collider and the rigidbody (if the latter is not on the teleport) must also be present on the camera. Does the name of the TELEPORT script match the name of the script? Have you hung up this script on the object?
50% of success is the correct analysis. No one will tell you what is wrong here until you tell what exactly the problem is. You can use a debugger, in extreme cases, output to the console, but do not blindly poke, hoping that it will work.
PS: some more advice about this code. I don't recommend using Camera.main because it's pretty
demanding feature. You can cache the camera in Start and then use it. And, of course, you shouldn't call scripts caps - it's not very readable.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question