Hello guys, I am making an FPS. I would like to make a character blink (short distance teleport). I would like my character to teleport in the direction he is going.

I’ve written and commented the script for the most part. I ask for your help to comment the logic in the script and operators I used (whether I did the rigth thing or not). I am particularly interested in how to get the object hit by a ray and check it’s width.

Here’s the script:
(with //commented part). Thanks a lot!!!

using UnityEngine;
using System.Collections;

public class teleport : MonoBehaviour {

	public Vector3 old_position;
	public Vector3 newVector;

	RaycastHit hit;


	void Start()
	{
		old_position = transform.position;
	}
		
	void Update() {

		//if a player is moving
		if(transform.position != old_position)
		{
			newVector = transform.position - old_position;
			Vector3 coordinates = transform.position;
			
			//if a player decides to shoot while moving
			if (Input.GetMouseButtonDown(0))
			{
				//let's check if the space we want to teleport is free
				//let's shoot the ray from the camera height towards the direction we are moving
				Ray forwardRay = new Ray(Camera.main.transform.position, 10* newVector.normalized);
				Debug.DrawRay(Camera.main.transform.position, 110* newVector.normalized, Color.green, 20, true);
				if (Physics.Raycast (forwardRay, out hit)) {
					Debug.Log (hit);
					//get the obejct hit by ray
					//new GameObject our_object = hit 
							
							//if(our_object's position distance - our position => than our teleport range){
							// no obstacles
							// execute teloprt normally
							// transform.position = transform.position + 10* newVector.normalized;
							//	}
									//else if{obejct's distance - our position < than our teleport range){
									//to prevent being stuck in the object
									//let's check the width of an object
										//if(object.mesh.bounds.size =< for example 100){
										//transform.position = transform.position + 10* newVector.normalized;					
										//execute teloprt normally			
										//}

											//else if(object.mesh.bounds.size > 100){
											//calculate distance between the object and the player
											//thedistance = our_object's position distance - our position
											//transform.position = transform.position + thedistance* newVector.normalized;
											//this will teleport us directly in front of an object
			
				}
			}
		
			old_position = transform.position; 
			}
	}
}

Update: any ideas? :smiley:

hit.collider.gameObject will give you the game object hit. Or hit.transform.gameObject.

On finding the width of the hit object, that’s harder. But you don’t need that. The RaycastHit object has a distance variable, which tells you at what distance the raycast hit the collided with object. The way to do a blink is pretty much:

void Blink() {
    Vector3 blinkDirection = transform.forward; //or some other, normalized Vector3
    float blinkLength = maxBlinkLength;
    if(Physics.Raycast(transform.position, blinkDirection, out hit, maxBlinkLength) {
        blinkLength = hit.distance;
    }

    tranform.position = transform.position + (blinkDirection * blinkLength);
}

If that finds you stuck in walls, it’s because the code above puts the center of your player at where the raycast stops, so your character will be halfway through the wall. In that case, you’ll have to subtract half the width of your collider from the raycast distance. Assuming you’re using a capsule collider for your character:

float colliderRadius;
void Start() {
    colliderRadius = GetComponent<CapsuleCollider>().radius;   
}

void Blink() {
    Vector3 blinkDirection = transform.forward; //or some other, normalized Vector3
    float blinkLength = maxBlinkLength;
    if(Physics.Raycast(transform.position, blinkDirection, out hit, maxBlinkLength + colliderRadius) {
        blinkLength = hit.distance - colliderRadius;
    }

    tranform.position = transform.position + (blinkDirection * blinkLength);
}

Hope that helps!