so I’m a noob and I’m trying to write a charactercontroller of my own and I had it working before but now gravity isn’t applying properly.
The problem is that gravity does seem to be applying, the player is spawns in the air, but he falls to the ground in super choppy way and when i use my jump function I fly up into the air and get stuck.
could anyone take a look for me?
if you happen to see anything i could be doing better (which may be most of it lol!)
public enum states { Idle, Walking, Strafing, Running, Jumping, Attacking, Talking }
public enum directions { Front, Back, Left, Right }
public var playerState = states.Idle;
public var playerDirection = directions.Front;
var forwardUI : GameObject;
var speed : float = 7;
var runSpeed : float = 13;
var jumpSpeed : float = 15.0;
var turnSpeed : float = 60;
var gravity : float = 20.0;
var turnLock = true;
var theBullet : Transform;
var bulletSpawner : Transform;
var lookAtTarg : Transform;
private var moveDirection : Vector3 = Vector3.zero;
private var controller : CharacterController;
var playerBody : GameObject;
function Start(){
controller = GetComponent(CharacterController);
var horizontalVelocity : Vector3 = controller.velocity;
var horizontalSpeed : float = horizontalVelocity.magnitude;
}
function Update() {
horizontalVelocity = controller.velocity;
horizontalSpeed = horizontalVelocity.magnitude;
// Handles the input for the run button
if(Input.GetButtonDown("run")){
turnLock = false;
}
if(Input.GetButtonUp("run")){
turnLock = true;
}
if(turnLock == true playerDirection != directions.Back)
{
playerDirection = directions.Back;
}
//Snap Camera Code goes here
//if(playerDirection == directions.Back){
// transform.Rotate(0, 180, 0);
// playerDirection = directions.Front;
//}else if(playerDirection == directions.Right){
// transform.Rotate(0, 90, 0);
// playerDirection = directions.Front;
//}else if(playerDirection == directions.Left){
// transform.Rotate(0, -90, 0);
// playerDirection = directions.Front;
//}else if(playerDirection == directions.Front){
//}
if(Input.GetButtonDown("fire")){
fireWeapon();
}
if(Input.GetAxis("Mouse X") != 0){
var turn: float = Input.GetAxis("Mouse X");
transform.Rotate(0, turn * 16 * Time.deltaTime, 0);
}
if(Input.GetAxis("Vertical") != 0 controller.isGrounded Input.GetAxis("Horizontal") == 0){
//Forward/Backward Movement
moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
if(Input.GetAxis("Vertical") > 0){
//Forward movement, no need to check on turnlock
if(playerDirection != directions.Back){
playerDirection = directions.Back;
}
if(horizontalSpeed > 0 playerState != states.Walking){
playerState = states.Walking;
}
}
if(Input.GetAxis("Vertical") < 0){
//Backward movement, we need to see if we're locked forwards
if(playerDirection != directions.Front turnLock == false){
playerDirection = directions.Front;
}
if(horizontalSpeed > 0 playerState != states.Walking){
playerState = states.Walking;
}
}
//print("Forward/Backward movement detected");
}
else if(Input.GetAxis("Horizontal") != 0 controller.isGrounded Input.GetAxis("Vertical") == 0){
//Left/Right
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 0);
if(Input.GetAxis("Horizontal") > 0){
//Right movement, we need to see if we're locked forwards
if(playerDirection != directions.Right turnLock == false){
playerDirection = directions.Right;
}
if(horizontalSpeed > 0 playerState != states.Walking){
playerState = states.Walking;
}
}
if(Input.GetAxis("Horizontal") < 0){
//Left movement, we need to see if we're locked forwards
if(playerDirection != directions.Left turnLock == false){
playerDirection = directions.Left;
}
if(horizontalSpeed > 0 playerState != states.Walking){
playerState = states.Walking;
}
}
//print("Left/Right movement detected");
}
else if(Input.GetAxis("Horizontal") != 0 controller.isGrounded Input.GetAxis("Vertical") != 0){
moveDirection = Vector3(Input.GetAxis("Horizontal") / 1.5, 0, Input.GetAxis("Vertical") / 1.5);
//print("Diag movement detected");
//Placed this code here to grant forward and backward directions priorit in a diagonal situation
if(Input.GetAxis("Vertical") > 0){
//Forward movement, no need to check on turnlock
if(playerDirection != directions.Back){
playerDirection = directions.Back;
}
if(horizontalSpeed > 0 playerState != states.Walking){
playerState = states.Walking;
}
}
if(Input.GetAxis("Vertical") < 0){
//Backward movement, we need to see if we're locked forwards
if(playerDirection != directions.Front turnLock == false){
playerDirection = directions.Front;
}
if(horizontalSpeed > 0 playerState != states.Walking){
playerState = states.Walking;
}
}
}
else if(Input.GetAxis("Horizontal") == 0 Input.GetAxis("Vertical") == 0 controller.isGrounded){
moveDirection = Vector3(0, 0, 0);
if(playerState != states.Idle){
playerState = states.Idle;
}
//print("no movement detected");
}
if(!controller.isGrounded)
{
//print("character in air");
playerState = states.Jumping;
}
if (Input.GetButton ("Jump") controller.isGrounded) {
moveDirection.y = jumpSpeed;
}
moveDirection = transform.TransformDirection(moveDirection);
if(turnLock == true){
moveDirection *= speed;
}else if(turnLock == false){
moveDirection *= runSpeed;
}
// Apply gravity
moveDirection.y -= 15 * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
//print(horizontalSpeed);
//print(playerDirection);
}
function fireWeapon(){
var newBullet = Instantiate(theBullet, bulletSpawner.position, bulletSpawner.rotation);
Destroy(newBullet.gameObject, 2);
}