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.

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
	}
}

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)

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;
   }
}

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.