- Home /
Teleporting projectiles, and maintaining speed
Hi, I'm trying to teleport a projectile from one teleport to another, by shooting into the first one. I also want the projectiles speed to be maintained when exiting the second teleporter.
This is my code:
#pragma strict
var teleportTo : Transform;
function OnTriggerEnter (col : Collider)
{ col.transform.position = teleportTo.position; }
The projectile does indeed get teleported as wanted, but it does not keep its speed and will immediately drop to the ground. Any help would be appreciated :)
Answer by Blankzz · Aug 10, 2011 at 10:48 PM
Just as Unamine suggested. Something like;
var teleportTo : Transform;
function OnTriggerEnter (col : Collider)
{
var vel : Vector3 = col.rigidbody.velocity;
col.transform.position = teleportTo.position;
col.rigidbody.Addforce(vel, ForceMode.Acceleration);
}
How are you getting the projectile to move in the first place. Are you using forces or setting its position?
I'm adding a force at the birth of the projectile, which is not constant and letting the physics handle the rest. $$anonymous$$y projectiles are large boulders.
Ok so the getting the velocity should work fine. Sounds like something is stopping your object. First debug the rigidbodies velocity before and after teleporting to see when the object is changing velocity.
Just a thought try. Setting the rigidbodies position ins$$anonymous$$d of the transforms.
col.rigidbody.position = teleportTo.position;
or
col.rigidbody.$$anonymous$$ovePosition(teleportTo.position);
Thanks Blankzz, it works now. $$anonymous$$ade the mistake of leaving the collider on one of the exitportals hehe.
Answer by Unamine · Aug 10, 2011 at 10:36 PM
Create a variable that stores the value of the current speed of the projectile before the teleport, then you use this value in the AddForce rigidbody when it is teleported;)
I hope I have helped ;)
Vote +1 :P
Answer by DGArtistsInc · Aug 10, 2011 at 08:50 PM
ok if it has a rigidbody then make a function Update and put the line of script below in it.
transform.rigidbody.AddForce(transform.forward * speed);
Just make a speed variable after that and it should work.
I think the hint is to have a rigidbody in the first place. I just tried firing some virtual rockets into a triggerBox that changes the position, and they come out with the same velocity as going in.
I don't want to add new force to the projectile, just keep the same velocity exiting the second portal as it had entering the first. @Owen, the projectile is already a rigidbody so the problem does not lie there.