Bug on Character movement
when I press forward/back/left/right the player character does move but stops abruptly but after when I press space to jump it will start moving again I have some bug on my code don't know which is which
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FPSController : MonoBehaviour {
public float speed = 2f;
public float sensitivity = 2f;
public GameObject eyes;
public GameObject weapon;
CharacterController player;
private float verticalVelocity = 0;
private float gravity = 14f;
private float jumpForce = 10f;
private float rotX = 0;
private float rotY = 0;
// Use this for initialization
void Start () {
player = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
// //check if player is grounded
if (player.isGrounded){
verticalVelocity = -gravity * Time.deltaTime;
//check if player has press space
if(Input.GetKeyDown(KeyCode.Space))
{
verticalVelocity = jumpForce;
}
}
else
{
verticalVelocity -= gravity * Time.deltaTime;
}
Vector3 movement = Vector3.zero;
movement.x = Input.GetAxis("Horizontal") * speed;
movement.y = verticalVelocity;
movement.z = Input.GetAxis("Vertical") * speed;
rotX = Input.GetAxis("Mouse X") * sensitivity;
rotY = Input.GetAxis("Mouse Y") * sensitivity;
transform.Rotate(0,rotX,0);
eyes.transform.Rotate(-rotY,0,0);
weapon.transform.Rotate(rotY,0,0);
movement = transform.rotation * movement;
player.Move(movement*Time.deltaTime);
}
}
Comment
Your answer
Follow this Question
Related Questions
Character only falls when button is pushed down 0 Answers
Problem with the Character Controller on the Y-Axis,Character Controller drifting in the Y Axis 0 Answers
Character not jumping smoothly using CharacterController 0 Answers
Moving slows decent with Rigidbody 2D 0 Answers
Adding force to direction of a Character Controller? 0 Answers