Problem was solved
Air Control not working on Character Controller
Hi all,
I've been trying to find a solution to this problem for some time now and haven't had too much luck. I'm trying to implement limited air control for my first-person character controller, but I keep facing an issue in which if I try to move while mid-air, my player starts bugging out or flying in circles.
I've tried checking if x and z input is positive/negative and adding/subtracting respectively but can't seem to get it to change at all.
Here's what I have currently
void Start()
{
controller = GetComponent<CharacterController>();
controllerCollider = GetComponent<CapsuleCollider>();
}
private void Update()
{
GetPlayerInput();
if (controller.isGrounded)
{
velocity.x = xInput;
velocity.z = zInput;
velocity.y = 0f;
controller.slopeLimit = 45.0f;
if (Input.GetButton("Jump"))
{
velocity.y = jumpHeight;
controller.slopeLimit = 90.0f;
}
}
else
{
velocity.x = (velocity.x + xInput);
velocity.z = (velocity.z + zInput);
}
velocity.y -= gravity * Time.deltaTime;
velocity = transform.TransformDirection(velocity);
controller.Move(velocity * walkingSpeed * Time.deltaTime);
if ((zInput != 0 || xInput != 0) && AntibumpCheck())
{
controller.Move(Vector3.down * controller.height / 2 * antiBumpMultiplier * Time.deltaTime);
}
}
private void GetPlayerInput()
{
xInput = Input.GetAxisRaw("Horizontal");
zInput = Input.GetAxisRaw("Vertical");
if (xInput != 0f && zInput != 0f)
{
xInput *= 0.7071f;
zInput *= 0.7071f;
}
}
Any help is appreciated, thanks!
Answer by lgarczyn · Nov 27, 2019 at 02:15 AM
I had to try to fix your code after your last ask. Had to get rid of the bump check since I don't have the source for that, but if you do keep it, I would suggest not calling Move twice in the same frame, as advised by the documentation.
private void Update()
{
Vector3 input = GetPlayerInput();
if (controller.isGrounded)
{
velocity = input * walkingSpeed;
controller.slopeLimit = 45.0f;
if (Input.GetButton("Jump"))
{
velocity.y = jumpHeight;
controller.slopeLimit = 90.0f;
}
}
else
{
velocity += input * airSpeed;
}
velocity.y -= gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
private Vector3 GetPlayerInput()
{
Vector3 output = new Vector3();
output.x = Input.GetAxisRaw("Horizontal");
output.z = Input.GetAxisRaw("Vertical");
output = Vector3.ClampMagnitude(output, 1f);
output = transform.rotation * output;
output.y = 0;
return (output);
}
The problem was keeping the velocity local, when it should have been in world coordinates.
This code will work as long as you do not rotate the CharacterController on any axis but the Y axis. If you rotate it downward to look down, your character will move slower when you do so.
To add a 'look up and down' functionality, it's usually best to rotate the camera inside the player, instead of rotating the player.
Hello again, I'm glad my last question struck a chord with you!
Can you explain a little more on what you mean by not rotating on any axis but the Y axis? I had written a $$anonymous$$ouse Look script to handle the rotation of the player. This just matched the character controller's rotation with the camera's. Is this the kind of rotation you're suggesting to avoid?
For the AntiBumpCheck, is there a way for me to optimize this? As in without calling .$$anonymous$$ove twice in the same frame. Would a coroutine work better for this?
Thanks
The Y axis is the vertical axis. It's the one axis character controllers usually rotate around. It's not really made to rotate sideways.
If your game is an fps, the camera would likely be a child of the character controller. In that case, only the camera will look up or down (along with the head of the character if it is animated), while the whole character controller rotates to look left or right.
So the character controller rotates around the Y axis, and the camera rotates around the X axis.
I don't know what antibumpcheck does, but you can replace two $$anonymous$$oves by a single $$anonymous$$ove using the sum of the arguments of the two $$anonymous$$oves.
Follow this Question
Related Questions
Move like 2D sonic in 3D space around in a spiral 1 Answer
How can i look for collisions of the bulidngs in may array ? how can i use OnTRiggerEnter/exit ? 0 Answers
Rotate sphere to the direction of movement 0 Answers
Character Controller - Limit controller speed 0 Answers
How to make a ladder script 0 Answers