- Home /
Teleportation with stored speed and direction
I have a working two way teleportation system, where, if a 2D spaceship collides with one teleport, it appears at the other, and vice versa. The ship is kinematic and is being moved by Rigidbody2D.MovePosition().
Right now, the ship simply appears at the exit teleport. The behaviour I am looking for is for the ship to come out of the exit teleport with the same direction and velocity with which it entered the first teleport.
I figured I could simply give back the velocity of the ship during teleport exit, but this is not working for some reason.
This is the script attached to each teleport -
public GameObject exitTeleport;
static GameObject thisTeleport;
void OnTriggerEnter2D (Collider2D col){
if (col.CompareTag("ship")){
if (thisTeleport == exitTeleport)
return;
thisTeleport = this.gameObject;
col.GetComponent<Rigidbody2D>().position = exitTeleport.GetComponent<Rigidbody2D>().position;
}
}
void OnTriggerExit2D (Collider2D col){
if (col.CompareTag("ship")){
Vector2 shipSpeed= col.GetComponent<shipScript>().shipSpeed;
Debug.Log (shipSpeed);
if (exitTeleport == thisTeleport)
thisTeleport = null;
col.GetComponent<Rigidbody2D>().velocity += shipSpeed;
}
}
The Vector2 shipSpeed is in the ship script's Update(). It returns -
(0.1, 0) for right movement, (-0.1, 0) for left movement
(0, 0.1) for up movement, & (0, -0.1) for down movement.
Is there any way I can use these vectors to keep the ship moving after it exits the teleport?