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!
Have you debugged in the collision code to see if the collision is actually happening?
@highpockets Yes I have, put Debug.log in it earlier and the collision is happening. It gives the bullets location when it collides.
Do have something in your movement code that is immediately teleporting you back to you old location?
This is likely what is happening because if the position is correct when the bullet collides, there is nothing here that suggests that the player object should not be moving to the bullet position
Answer by tormentoarmagedoom · May 24, 2019 at 07:35 AM
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!
Your answer
Follow this Question
Related Questions
Bullet spawning at high Player speed 1 Answer
Pause button not working correctly 1 Answer
Player Movement Not Always Responding 1 Answer
Sumo Physics Bounce Question 0 Answers
Help with drift physics 0 Answers