- Home /
Question by
Bokaii · Jun 09, 2018 at 11:09 AM ·
movementcharactercontrolleraxisfreeze
Freeze Character control on axis
Is there a way to freeze the charactercontroller.Move on one axis at a time? For example if I wanted to ignore gravity when I call a function.
I created a FirstPersonController script (I followed Quill18's simple fps tutorial):
//Floats
//Movement Speed
public float movementSpeed = 5.0f;
public float mouseSensitivity = 2.0f;
//Rotation
float verticalRotation = 0f;
public float UpDownRange = 90f;
float verticalVelocity = 0f;
public float jumpSpeed = 2f;
//Character Controller
CharacterController cc;
// Use this for initialization
void Start () {
//Character Controller
cc = GetComponent<CharacterController>();
if(cc == null)
{
//Freak Out
print("No Character Controller");
}
//Lock cursor
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// Update is called once per frame
void Update () {
//Rotation
//GetAxis
float rotX = Input.GetAxis("Mouse X") * mouseSensitivity;
verticalRotation -= Input.GetAxis("Mouse Y") * mouseSensitivity;
//RotateRangeUpDown
verticalRotation = Mathf.Clamp(verticalRotation, -UpDownRange, UpDownRange);
Camera.main.transform.localRotation = Quaternion.Euler(verticalRotation, 0, 0);
//RotateLeftRight
transform.Rotate(0, rotX, 0);
//Movement
//GetAxis
float h = Input.GetAxis("Horizontal") * movementSpeed;
float v = Input.GetAxis("Vertical") * movementSpeed;
//Vector speed
if (!cc.isGrounded)
{
//Gravity
verticalVelocity += Physics.gravity.y * Time.deltaTime;
}
Vector3 speed = new Vector3(h, verticalVelocity, v);
speed = transform.rotation * speed;
//Jump
if (cc.isGrounded && Input.GetButtonDown("Jump"))
{
verticalVelocity = jumpSpeed;
}
//Move
cc.Move(speed * Time.deltaTime);
I tried having 3 booleans (LockX, LockY and LockZ) and having if statements saying that if one of them is locked, the speed vector has 0 on that axis. For example:
if(LockX)
{
speed.x = 0;
}
But I couldn't get it to work..
Any idea why and what I could do different?
Comment