How do you make a player controller that works with a seamless teleport?
I want to build a better seamless teleport that works with player controller.
I had to enable auto sync transforms in the physics settings because I use character controller with move.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SeamlessTeleport2 : MonoBehaviour
{
public Transform Teleport;
public Transform TeleDest;
public Vector3 Position;
public Quaternion Rotation;
public GameObject Player;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter(Collider other)
{
Player.transform.parent = Teleport.transform;
Position = Player.transform.localPosition;
Rotation = Player.transform.localRotation;
Player.transform.parent = TeleDest.transform;
Player.transform.localPosition = Position;
Player.transform.localRotation = Rotation;
Player.transform.parent = null;
}
}
You only need to set the Player.transform.position to your destination’s transform.position.
With the CharacterController component, you need to disable the component before moving the player. You also don’t need to reference the player via the inspector, when you will gain access to it via the OnTriggerEnter callback. As your code is, if anything enters the trigger, the player will be suddenly teleported.
I could not find a simpler way to get the local position to the other parent game object and then teleport.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SeamlessTeleport2 : MonoBehaviour
{
public Transform Teleport;
public Transform TeleDest;
public Vector3 Position;
public Quaternion Rotation;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter(Collider other)
{
if (other.TryGetComponent(out CharacterController controller))
{
controller.transform.parent = Teleport.transform;
Position = controller.transform.localPosition;
Rotation = controller.transform.localRotation;
controller.transform.parent = TeleDest.transform;
controller.enabled = false;
controller.transform.localPosition = Position;
controller.transform.localRotation = Rotation;
controller.enabled = true;
controller.transform.parent = null;
}
}
}