- Home /
The question is answered, right answer was accepted
Flying Platform jump
In my game i want to make a platform, and when my FPC steps on it, he flies up in the air, i've tried some codes but I cant figure it out, if anyone knows how to do this that'd be great. Thanks!
Answer by aldonaletto · Jun 19, 2012 at 05:41 PM
If you're using the First Person Controller prefab, create a trigger, child it to the platform and add this script to it:
var speed: float = 50;
function OnTriggerEnter(other: Collider){ var cMotor: CharacterMotor = other.GetComponent(CharacterMotor); if (cMotor){ cMotor.SetVelocity(Vector3.up * speed); } } This does not move the platform, but will shoot the FPC up at speed meters per second.
thank you so much and by creating a trigger do you mean make a box collider on a empty GameObject and tick is trigger?
Yes, this is the best method (you could also create a cube, adjust its dimensions, set Is Trigger and uncheck $$anonymous$$esh Renderer to make it invisible)
One more thing: the function SetVelocity in the Character$$anonymous$$otor has an error: it sends a message OnExternalVelocity with the default option RequireReceiver, what generates errors "Send$$anonymous$$essage has no receiver". You can comment out this line in the CharacterController.js script (line 582 in the current version) to eli$$anonymous$$ate the error messages, or add the option DontRequireReceiver to it, like below:
Send$$anonymous$$essage("OnExternalVelocity", Send$$anonymous$$essageOptions.DontRequireReceiver);
@Aldo it's giving me an error "Assets/Scripts/test/flip.js(2,13): BCE0018: The name 'Character$$anonymous$$otor' does not denote a valid type ('not found'). Did you mean 'UnityEngine.CharacterJoint'?"
Answer by Berenger · Jun 18, 2012 at 02:15 AM
I suppose you want the platform to go up for a hosrt distance and extremely fast, so it can throw the player. Physic is probably not the best way to go as it would probably leave the player behind. I suggest you use a collider on the platform only to detect when the player step on it, then parent the player to the platform, make it go up, unparent and finally add a force upward to the player. A big one. You might have to save the player's velocity when he steps one the platform.
it sounds like it'l work but i dont know how to do what you said--can you explain a little please
Add a collider on the platform. Set isTrigger to true.
Add a script on the platform, implement the function OnTriggerEnter
Inside, check if it is the player, if it is, the player's transform.parent becomes the platform's transform.
Either with an animation or a coroutine, make the platform rise very fast as to throw the player, like if there was a spring below.
At the highest point of that animation, unparent the player (player.transform.parent = null) and add a force on his rigidbody.
Is it better ?
step 3 is :
function OnTriggerEnter( col : Collider ){
if( col.CompareTag( "Player" ) // don't forget to set that on your player
{
col.transform.parent = transform;
}
}
step 4 and 5, how do you animate the platform currently ?