Teleportation with Collision detection.


I have created this simple teleportation script. As you can see in the Update function I am trying to check for collisions using Physics. CheckCapsule. But it is not working.

Anyone has any hints on why? Or should I use a different approach?

using UnityEngine;
using System.Collections;

public class telePortationScript : MonoBehaviour {
    Camera myCamera;

    GameObject player;
    CharacterController playerCC;

    // Use this for initialization
    void Start () {

        player = GameObject.FindGameObjectWithTag("Player");
        playerCC = player.GetComponent<CharacterController>();

	// Update is called once per frame
	void Update () {

        RaycastHit tempHit;

        if (Physics.Raycast(myCamera.transform.position, myCamera.transform.forward, out tempHit, 20))

            if (Input.GetMouseButtonDown(2))
                Vector3 goal = new Vector3(tempHit.point.x, player.transform.position.y, tempHit.point.z);
                if (CorridorIsWideEnough(player.transform.position, goal, 2)) {
                    player.transform.position = goal;


    bool CorridorIsWideEnough(Vector3 startPt, Vector3 endPt, float width)
        return Physics.CheckCapsule(startPt, endPt, width);


Use OnTriggerEnter function! This is a simple way for collision (as trigger) detection. Just mark “Is Trigger” in teleporter object collider and write some code for teleport and attach to them.

void OnTriggerEnter (Collider otherObject)
   if (otherObject.gameObject.CompareTag ("Player"))
      Vector3 newPosition = otherObject.gameObject.transform.position;
      newPosition = new Vector3 (10, 10, 10);
      otherObject.gameObject.transform.position = newPosition;

Something like that. Of course you can assign position as Vector3 in variable or using Transform of the point of empty gameobject (this is the best idea, because you are adjusting point once and you can manipulate them as you like).