I made a thread with same type of problem, but it has sunk into the abyss. I’m attempting to modify movement script from Unity reference to meet my needs. My project is a side scroller. I need my character to be able to move, jump and move while on air. Modifying script so that I can jump and move is easy, but problems arise with air movement.
Would anyone know an solution for this?
This is a slightly changed code from reference, but it doesn’t enable movement while on air.
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(0, 0, Input.GetAxis("Horizontal"));
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);
}
This is code after I tried to tweak into allowing movement while on air, but it doesn’t work as expected and my jumps become “teleports” into the air instead of gradual movement.
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(0, 0, Input.GetAxis("Horizontal"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump") controller.isGrounded) {
moveDirection.y = jumpSpeed;
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}