- Home /
Teleport Player UFPS
Hi, I'm working on a teleporting script with Baste's script. I would like to make similiar to Ascend's teleport. If I push T key then I will teleporting in the middle screen point (crosshair) ,but there is a maxdistance. If I hit a collider(wall,enemy) the player's postion will be front of the wall. Is there idea?
using UnityEngine;
using System.Collections;
public class Blink : MonoBehaviour
{
float colliderRadius;
void Start() {
colliderRadius = GetComponent<CapsuleCollider>().radius;
}
void Blink()
{
Vector3 blinkDirection = transform.forward; //or some other, normalized Vector3
float blinkLength = maxBlinkLength;
if (Input.GetKey(KeyCode.T))
{
if(Physics.Raycast(transform.position, blinkDirection, out hit, maxBlinkLength + colliderRadius)) {
blinkLength = hit.distance - colliderRadius;
}
transform.position = transform.position + (blinkDirection * blinkLength);
}
}
}
I modified the script (you can see below). I tried to figure out more over 3 hours how to stop teleportation whenRaycast hit environment 's collider and the end point will be there was hit. Right now there is a fix distance it's maxBlinkLength. $$anonymous$$aybe should I do somthing with Vector3.Distance?
using UnityEngine; using System.Collections;
public class Blink : $$anonymous$$onoBehaviour {
RaycastHit hit;
float maxBlinkLength = 4.0f;
void Teleport(){
Vector3 teleport = gameObject.transform.position + Camera.main.transform.forward * maxBlinkLength;
transform.position = teleport;
}
void Update() {
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Space)) {
Teleport();
}
}
}
Your answer
Follow this Question
Related Questions
How do I make something happen at collider 1 Answer
Teleporting Player to a point using box colliders 1 Answer
Ignore Collisions with SteamVR Teleport Area? 1 Answer
Internal collisions 1 Answer