- Home /
How to Gently float up?
I am working on a game my senior project where I need to make a game object that the character controls float into the air to a set height at the most when you move it in any direction but falls slowly back to earth. I was attempting to use the move script in character controller:
/// This script moves the character controller forward /// and sideways based on the arrow keys. /// It also jumps when pressing space. /// Make sure to attach a character controller to the same game object. /// It is recommended that you make only one call to Move or SimpleMove per frame. var speed = 6.0; var jumpSpeed = 8.0; var gravity = 20.0;
private var moveDirection = Vector3.zero;
function FixedUpdate() { var controller : CharacterController = GetComponent(CharacterController); if (controller.isGrounded) { // We are grounded, so recalculate // move direction directly from axes moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed;
if (Input.GetButton ("Jump")) { moveDirection.y = jumpSpeed; } }
// Apply gravity moveDirection.y -= gravity * Time.deltaTime;
// Move the controller controller.Move(moveDirection * Time.deltaTime); }
By altering the line of code here:
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
By replacing the 0 with another number, but all that did is make it bounce upon hitting the ground.
Are there any suggestions that can help me?
Answer by StephanK · Jun 17, 2010 at 09:18 AM
This happens because the moving code is inside a check for controller.isGrounded. So your moving code is only executed when your character touches the ground.
Yeah but if I remove the isGrounded portion then it will float up continuously.
That's because you told it to by setting the "up" or y component of the vector to a fixed value. If you do this you have to add a check after moving the object and check if transform.position.y is > your maxHeigth and if it is set y to maxHeight.
Your answer
Follow this Question
Related Questions
SimpleMove not working on Y axis? 1 Answer
What is the best way to move a character for an FPS? 1 Answer
Random float 0 Answers
First Person Character Controls, How? 1 Answer
Footstep sounds system with a Vr touchpad movement system 1 Answer