Teleport and change direction of current velocity.


From the title it isn't really clear, so here exactly what I want to do.

Firstly, I'm teleporting an object to another location. (Done through a transform.position)

Secondly, (the part where I fail)

Retain the Force/Velocity of the object, but change it's direction.

As a whole, The object enter the First gate and should jump out of the Second gate relative to where the Second gate is facing.

I've thought of reading my velocity, and give it back to the ball at his exit relative to the space of the exit. But It seems I can only adjust space to Self or World...

Any idea?


Here a Link to a simple Image depicting what it should do.

Your object's velocity has two logical parts -- the direction, and the magnitude (how fast you're moving). We don't want to change the magnitude, but we will change the direction.

You can use the transform of your second gate to get the direction. I don't know exactly how your gate model looks, but I will assume that its exit is facing in the direction of the positive Z axis. (You can verify this by clicking on the object in the Scene with Local coordinates selected)

So, you would need to do at least two things: change the object's direction to be the same as the second gate, and change the object's position to be same as the second gate. Something like this might work:

MyObject.transform.position = SecondGate.transform.position;
MyObject.rigidbody.velocity = SecondGate.transform.forward * MyObject.rigidbody.velocity.magnitude; // SecondGate.transform.forward is a vector pointing down the positive Z, in local space


Saw your answer. The velocity might be getting zeroed because you are not using triggers for your gates or the position change zeroes it in the same tick.

Here's code with trigger gates instead of solid gates, which doesn't require re-instantiating the object and raping the GC:

function OnTriggerEnter(collider : Collider)
    if (collider.attachedRigidbody != null && collider.attachedRigidbody.CompareTag ("Ball")) 
        collider.attachedRigidbody.position = destination.transform.position;
        collider.transform.forward = destination.transform.right;
        collider.attachedRigidbody.AddForce(destination.transform.right * collider.attachedRigidbody.velocity.magnitude * powerYield, ForceMode.VelocityChange);
            // Or collider.attachedRigidbody.velocity = destination.transform.right * collider.attachedRigidbody.velocity.magnitude * powerYield

Found a way to "Do it"

var destination : Transform; 

var powerYield = 2000; //Most realist is 2000.

private var power : float = 0; private var ImpactVelocity : float = 0;

function OnCollisionEnter(collision : Collision) { if (collision.gameObject.CompareTag ("Ball")) { ImpactVelocity = collision.relativeVelocity.magnitude;

    power = ImpactVelocity * powerYield;

        Destroy (collision.gameObject);

            var ball = Instantiate(collision.gameObject, destination.transform.position, destination.transform.rotation);
            ball.rigidbody.AddForce(destination.transform.right * power);


I detect the velocity and then instantiate with power by multiplying velocity value.