Hello!
I’m currently building a basic FPS, and I’m coming across a very peculiar problem.
I have found a way to make my player (built with the CharacterController) jump up and down and it works really well and everything, but I seem to come across a problem when I drop down from an object, such as a building. I can jump up and down with no problem from the building, but when I just drop down without jumping, my CharacterController just immediately falls down really fast.
Here’s my Controller.cs code (used for moving the player):
using UnityEngine;
using System.Collections;
public class Controller : MonoBehaviour {
public float moveSpeed;
public float mouseSpeed;
private float lastMouseSpeed;
public Camera playerCamera;
public CharacterController player;
private float sprintSpeed;
private float originalMoveSpeed;
public static bool sprinting = false;
public float jumpHeight;
public bool jumping = false;
private float verticalVelocity;
/* Locks the cursor in place and gets the CharacterController for manipulation. */
void Start() {
if (player == null) player = GetComponent<CharacterController>();
sprintSpeed = moveSpeed * 1.8f;
originalMoveSpeed = moveSpeed;
lastMouseSpeed = mouseSpeed;
Screen.lockCursor = true;
}
/* Enables player and NPC movement. */
void Update() {
// The key movements, (forward, backwards, side-to-side, etc).
float forward = Input.GetAxis("Forward") * moveSpeed;
float side = Input.GetAxis("Side") * moveSpeed;
// The mouse movements.
float rotationSide = Input.GetAxis("Mouse X") * mouseSpeed;
float rotationForward = Input.GetAxis("Mouse Y") * mouseSpeed;
transform.Rotate(0, rotationSide, 0);
playerCamera.transform.Rotate(-rotationForward, 0, 0);
if (Input.GetButton("Sprint")) {
sprinting = true;
moveSpeed = sprintSpeed;
} else {
moveSpeed = originalMoveSpeed;
sprinting = false;
}
verticalVelocity += Physics.gravity.y * Time.deltaTime;
if (player.isGrounded && Input.GetButtonDown("Jump")) {
verticalVelocity = jumpHeight;
jumping = true;
} else {
jumping = false;
}
Vector3 movement = new Vector3(side, verticalVelocity, forward);
// As long as the game isn't paused, move and whatnot.
if (EscapeMenu.paused != true) {
movement = GetLookout() * movement;
player.Move(movement * Time.deltaTime);
}
}
/* Returns the rotation (the mouse look) for processing. */
Quaternion GetLookout() {
return transform.rotation;
}
/* Returns the lastMouseSpeed. (Used for inventory and escape menu) */
public float getLastMouseSpeed() {
return lastMouseSpeed;
}
/* Sets the mouse speed. (Potential for options) */
public void setMouseSpeed(float passedMouseSpeed) {
this.mouseSpeed = passedMouseSpeed;
}
}