Teleporting To Projectile Location

Hey, I’m relatively new to c# and I’m trying to code a gun that teleports the player to the bullets location when it hits a surface in my world and then deletes the bullet. I’ve set up the gun to shoot but I’m having difficulty with the actual teleporting of the player. I’ve found other topics on this and tried to implement it into my code but have had no luck so far. Here’s the code I’ve got so far.

public class BulletTP : MonoBehaviour
{

public GameObject Player;
    public GameObject Bullet;

    void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.tag == "World")
        {
            Player.transform.position = new Vector3 (Bullet.transform.position.x, Bullet.transform.position.y, Bullet.transform.position.z);
            Destroy(Bullet);
        }
    }

    void Start()
    {
        
    }

    void Update()
    {
     
    }
};

Any help is much appreciated!

Hello.

I supose this script is attached to the bullet (because OnCollisionEnter works for the colliders in the same gameobject as the script)

Then, as yu teleport at position of the object containing the script, you can do this:

Player.transform.position = gameObject.transform.position;

(gameObject its the obkect where the script is attached to)

Then if is not working, first you need to know if the function OnCollisionEnter is beeing called.

To know this, add a Debug.Log to see if is executed.

 void OnCollisionEnter(Collision col)
 {
Debug.Log ("Collision");
     if (col.gameObject.tag == "World")
     {
    ...............

Be sure all objects in the world are tagged as “World”

Be also sure bullet have a Collider and a rigidbody (if not OnCollisionEnter will never be executed)

Good luck!