- Home /
how to make a bolt collision teleport player to where the collision happened
I want to make my player have something like a teleport gun, where he fires bolts and whatever they hit, the player teleports to. I have a player that shoots bolts right now and I'm wondering how to code the teleport part. This script teleports the bolt shots back to the player once they hit (though they are disabled), how do I make the player teleport to where the bolt shots hit?
public GameObject hit; public GameObject player; Vector3 spawnPoint;
void Start ()
{
GameObject spawnObject = player;
spawnPoint = spawnObject.transform.position;
}
void OnCollisionEnter (Collision collision) {
if (collision.gameObject.tag == "ground")
{
Instantiate (hit, transform.position, transform.rotation);
transform.position = spawnPoint;
gameObject.active=false;
}
Answer by hav_ngs_ru · Feb 02, 2015 at 09:23 PM
// bolt script
void OnCollisionEnter(Collision.collision) {
...
if (collision.gameObject.tag == "ground") {
SetActive(false);
player.SendMessage("TeleportTo", transform.position);
...
}
...
}
// player script
public void TeleportTo(Vector3 teleportPoint) {
transform.position = teleportPoint;
}
I tried this and I'm getting an error "the name spawnObject does not exist in the current context"
is there an easy way to slightly offset the teleport, because sometimes I get stuck in the material I teleport to. Thanks for your answer though, it was just what I was looking for.
player.Send$$anonymous$$essage("TeleportTo", transform.position + (player.transform.position - transform.position)*offset);
declare public float offset in bolt script and ajust its value in inspector