Teleporting to a projectile - Moving the player error

44 views Asked by At

I've been working on a project involving instantiating an orb which then teleports the player to its location when hitting any object I tag with "floor". I am relatively new to programming and so any help is appreciated.

  void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.tag == "Floor")
        {
            script.canShoot = true;
            isTriggered = true;
            player.transform.position = teleportLocation;
            Destroy(gameObject);
            Debug.Log("hit floor");
        }

My "teleportLocation" is just the orbs constant position as of currently.

Currently the issue I am facing is with moving the player, I have substituted the player for a cube which did work when setting the position. This is my script for instantiating the projectile:

  Vector3 dir = mouse.transform.position - transform.position;
        if (!disabled)
        {

             GameObject instance = Instantiate(orbPrefab, orbShooter.transform.position, orbPrefab.transform.rotation);
             instance.GetComponent<Rigidbody>().AddForce(dir * power);
             instance.transform.position = new Vector3(0, 0, 0);

        }

The only way I have been able to teleport the player to the orb successfully was through this script:

public class teleport : MonoBehaviour
{
    public Vector3 tpPos;
    public GameObject testOrb;
    // Start is called before the first frame update
    void Start()
    {
     
    }

    void Update()
    {
        tpPos = testOrb.transform.position;
        if (Input.GetKeyDown(KeyCode.E))
        {
            transform.position = tpPos;
        }
    }
}

I could move the player to an orb (which was not an instantiated object but was instead a normal game object moved by clicking) when I press "E" and this seemed to work. The problem doesn't seem to lie within the collision detection or transform positions but instead the instantiation. It would be helpful if anyone knows a way which could teleport my player to the instantiated orb.

the above involves the different things i had tried....

2

There are 2 answers

0
Luis Moseley On BEST ANSWER

Sorry for the late answer, I have been completing projects with limited time frames (usually 5 weeks or less each) and moved on too fast once I had solved it. The issue as I recall was due to the order in which actions were happening within script. I am not entirely sure, however all I can say is that:

I wanted my player to shoot an orb (through instantiation) and then have them teleport to the orb (which I thought could be done by simply setting player.transform.position = orb.transform.position

This had no effect however. I then tried to set the players position in different ways and troubleshooted whether the issue was a result of collision or something else. Collision was perfectly fine.

After this, my last effort was to create a subroutine / function inside of the player script (attached to the player object) and then call the function from the orb script (when it impacts the floor).

I didn't expect this to work but it did and so I can only suspect that the issue was some sort of error within the order of things being called? I am not entirely sure but if you require further elaboration I am happy to do so! Thanks for your patience :)

1
Luis Moseley On

Found the solution which just involved creating a method inside of the script attached to the player which changes the players transform position to the orbs. The method is then called inside of the orb script when it collides with the floor. The problem seemed to be with where the teleport was called inside of the script for some reason, hopefully others will understand if they have this issue also..