- Home /
character controller
Hi, I'm trying to figure out how can i move my character to some fixed height,for example if my character position is at 10 and i want to lift him in gradually to let say 15. this is what i'm doing:
void Update()
{
float nextHeight = Mathf.Lerp( transform.position.y, transform.position.y + 5, 3f );
MoveVector.y = nextHeight;
myCharacter.Move( MoveVector );
}
but my character is infinity flying up.
help please.
can you please edit and then highlight your code, and then click the little button with all the ones and zeros so we can read your code better :D
-Grady
Answer by AngryOldMan · Jul 02, 2011 at 12:20 PM
There are a few ways to do this. You could turn it into a coroutine in order to give you a large amount of control over how fast it moves, by what amount and when it stops. A simpler or faster way though would be to put an if statement in to stop the code for the character rising. Something like:
void update()
{
if (characterHeight <= maxHeight)
{
MoveVector.y = nextHeight;
}
}
Answer by aldonaletto · Jul 02, 2011 at 02:16 PM
If you're using the standard FPS Controller, you can make a "jet pack" effect by setting a vertical velocity. In this script, when you press F the character starts to get height like moved by a jet pack; you can't control directions fully while it's going up, but as soon as you release the F key the control keys work as usual:
var cMotor: CharacterMotor;
function Start(){
cMotor = GetComponent(CharacterMotor);
}
function FixedUpdate(){
if (Input.GetKey("f")){
cMotor.SetVelocity(Vector3.up*1.5); // vert velocity = 1.5
}
}
Answer by easy · Jul 02, 2011 at 10:42 AM
Hi again,
this is the code now:
float maxHeight;
void Start()
{
maxHeight = transform.position.y + 5f;
}
void Update()
{
float nextHeight = Mathf.Lerp( transform.position.y, maxHeight , 3f );
MoveVector.y = nextHeight;
myCharacter.Move( MoveVector );
}
still the same problem. help please.
Answer by save · Jul 02, 2011 at 10:19 AM
It's because you position towards the current transforms position +5 every frame. You have to store the Y-position first and transform against that instead.
var nextHeight : float = 10.0;
function setNextHeight (object : Transform) {
nextHeight = object.transform.position.y + 5;
}
Call the function on a given event (could be a trigger) with:
setNextHeight(theObject);
Then transform towards nextHeight:
Mathf.Lerp( transform.position.y, nextHeight, 3f );
(Pseudo-mix of js)
Your answer
Follow this Question
Related Questions
Stopping the Character Controller 0 Answers
Detecting collisions with OnControllerColliderHit when not moving 0 Answers
Conflict between CharacterController and NavMeshAgent 1 Answer
Lerp can someone explain please 2 Answers
OnCollisionEnter does not work 4 Answers