- Home /
transform position player teleport
I managed to get the player to teleport to a collider surface or variable distance away from the camera into space. The tricky part is where the teleport to space is close enough to a collider that it will teleport me to the other side!
I have a ray cast buoy in place to show that if one of these ray casts hits a collider, that it should make an adjustment to where the player is teleporting. BUT I seem to come across a wall in getting it to work! How should I go about fixing this?
using UnityEngine; using System.Collections;
public class Blink : MonoBehaviour {
public GameObject blinkPin;
public float maxDistance = 10f;
public float blinkArea = .5f;
public static Transform cam;
Vector3 camPos;
//Vector3 freeBlinkPos;
GameObject pin;
GameObject buoy;
bool teleC = false;
bool teleN = false;
// Use this for initialization
void Start () {
cam = Camera.main.transform;
pin = GameObject.Find ("Pin");
buoy = GameObject.Find ("Marker");
pin.SetActive (false);
}
// Update is called once per frame
void Update () {
bool foundHit = false;
//add camera local transform and adds maxdistance
camPos = GameObject.FindWithTag ("Player").transform.Find ("Main Camera").transform.TransformPoint (0,0, maxDistance);
BlinkHere (foundHit);
}
void BlinkHere (bool hitCol)
{
RaycastHit hitRay = new RaycastHit ();
//PORTSIDE DETECTOR PORT
bool port = false;
RaycastHit limitP = new RaycastHit ();
Vector3 portside = buoy.transform.TransformDirection (Vector3.left);
port = Physics.Raycast (buoy.transform.position, portside, out limitP, 1);
Debug.DrawRay (buoy.transform.position, portside * 1, Color.red);
//STARBOARD DETECTOR STAR
bool star = false;
RaycastHit limitS = new RaycastHit ();
Vector3 starside = buoy.transform.TransformDirection (Vector3.right);
star = Physics.Raycast (buoy.transform.position, starside, out limitS, 1);
Debug.DrawRay (buoy.transform.position, starside * 1, Color.green);
//STERN DETECTOR STERN YELLOW
bool stern = false;
RaycastHit limitSt = new RaycastHit ();
Vector3 sternside = buoy.transform.TransformDirection (Vector3.back);
stern = Physics.Raycast (buoy.transform.position, sternside, out limitSt, 1);
Debug.DrawRay (buoy.transform.position, sternside * 1, Color.yellow);
//TOP DETECTOR TOP
bool top = false;
RaycastHit limitTop = new RaycastHit ();
Vector3 topside = buoy.transform.TransformDirection (Vector3.up);
top = Physics.Raycast (buoy.transform.position, topside, out limitTop, 1);
Debug.DrawRay (buoy.transform.position, topside * 1, Color.cyan);
//BOW DETECTOR BOW MAGENTA
bool bow = false;
RaycastHit limitBow = new RaycastHit ();
Vector3 bowside = buoy.transform.TransformDirection (Vector3.forward);
bow = Physics.Raycast (buoy.transform.position, bowside, out limitBow, 2);
Debug.DrawRay (buoy.transform.position, bowside * 2, Color.magenta);
Debug.Log (bow);
//BELOW DETECTOR BELOW
bool below = false;
RaycastHit limitBelow = new RaycastHit ();
Vector3 belowside = buoy.transform.TransformDirection (Vector3.down);
below = Physics.Raycast (buoy.transform.position, belowside, out limitBelow, 1);
Debug.DrawRay (buoy.transform.position, belowside * 1, Color.white);
if (Input.GetButton ("Fire1"))
{
hitCol = Physics.Raycast ((transform.position + new Vector3 (0,1,0)), cam.forward, out hitRay, maxDistance);
pin.SetActive (true);
//test ceiling
//Debug.DrawRay (buoy.transform.position, buoy.transform.forward * 3, Color.red);
if (hitCol)
{
Debug.Log ("switched to teleC porter");
blinkPin.transform.position = hitRay.point;
blinkPin.transform.rotation = Quaternion.LookRotation (hitRay.normal);
blinkPin.transform.Find ("Pin").localPosition = new Vector3 (0, 0, .5f);
teleN = false;
teleC = true;
}
if (!hitCol)
{
Debug.Log ("switched to teleN porter");
//make sure to change child cube to 0 0 0 here, and back to original in first if
blinkPin.transform.position = camPos;
blinkPin.transform.rotation = transform.rotation * (Quaternion.Euler (-90, 0, 0));
//dont bother changing the y axis because its simply the plane collision
blinkPin.transform.Find ("Pin").localPosition = new Vector3 (0, 0, 0);
teleC = false;
teleN = true;
}
}
//Blink to Collider TELEC
if ( Input.GetButtonUp ("Fire1") && teleC)
{
Debug.Log ("teleported with teleC");
//for some reason it adds maxDistance from somewhere, not just teleports to blinkpin
Vector3 newPos1 = blinkPin.transform.TransformPoint (0, 0, blinkArea);
transform.position = newPos1;
pin.SetActive (false);
}
//Blink to Null Space TELEN
if (Input.GetButtonUp ("Fire1") && teleN)
{
Debug.Log ("teleported with teleN");
//port || star || stern || top || bow || below
//MAGENTA
if (bow)
{
Debug.Log ("teleported with bow");
Vector3 newPosBow = blinkPin.transform.TransformPoint (0, 0, -2f);
transform.position = newPosBow;
pin.SetActive (false);
}
//YELLOW
/*
if (stern)
{
Debug.Log ("teleported with stern");
Vector3 newPosStern = blinkPin.transform.TransformPoint (0, 0, .2f);
transform.position = newPosStern;
pin.SetActive (false);
}
*/
//port || star || stern || top || bow || below
if (!port || !star || !stern || !top || !bow || !below)
{
Debug.Log ("teleported without any help");
Vector3 newPosNull = blinkPin.transform.TransformPoint (0, 0, 2f);
transform.position = newPosNull;
pin.SetActive (false);
}
}
}
}
Your answer
Follow this Question
Related Questions
JS Dynamic Height Camera vibrating =( 1 Answer
Negative Positions Breaks Raycasts 1 Answer
Getting transform position of last parent 2 Answers
NullReferenceException when using raycast 1 Answer
Trouble with raycasting 1 Answer