#absolute noob needs help with groundcheck error
1 messages · Page 1 of 1 (latest)
@lethal owl
public class ThirdPersonMovement : MonoBehaviour
{
public CharacterController controller;
public Transform cam;
public Animator anim;
public float speed = 6;
public float gravity = -9.81f;
public float jumpHeight = 3;
Vector3 velocity;
float turnSmoothVelocity;
public float turnSmoothTime = 0.1f;
void Update()
{
if (controller.isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
if (Input.GetButton("Jump") && controller.isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
}
if (controller.isGrounded)
{
anim.SetBool("isGrounded", true);
}
else
{
anim.SetBool("isGrounded", false);
}
//gravity
velocity.y += gravity * Time.deltaTime;
//walk
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
float targetAngle = 0f;
float angle = 0f;
Vector3 moveDir = Vector3.zero;
Vector3 movementSpeed = Vector3.zero;
if (direction.magnitude >= 0.1f)
{
targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
movementSpeed = moveDir.normalized * speed * Time.deltaTime;
}
Vector3 gravSpeed = velocity * Time.deltaTime;
controller.Move(gravSpeed + movementSpeed);
}
}
}```
Okay, so, what specifically is the problem now
groundcheck is still flickering
and also the jump is buggy and doesnt happen 100% of the time
prob because theres still an issue w the groundcheck
That would be because you need to hit it on a frame it's grounded
yea
Change this:
if (controller.isGrounded && velocity.y < 0)
To this:
if (controller.isGrounded && velocity.y <= 0)
You're not applying a downward force ifyour y velocity is 0
which means you don't touch the ground
So it was actually a combination of both of the common Controller issues
groundcheck still flickering
this is all probably because i tried to do my own manual groundcheck before someone told me to use the character controller groundcheck
Actually, I think a better way to do this would be this:
if (Input.GetButton("Jump") && controller.isGrounded) {
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
} else {
velocity.y = -2f;
}
Instead of the first two ifs
now jump is completely broken
Oh, right, that's going to mess up when you're not on the ground, my bad, it was better before
with the first two ifs ^
you see i struggle to jump onto the cube because i have to spam spacebar for it to jump
and also isGrounded flickering
this is a video using the older code before i saw the groundcheck flicker
and i can jump much easier
And this is with <= 0 or with the < 0 on the first if?
<= 0
ok groundcheck isnt flickering anymore i actually figured it out
the anim was before i called move
So the Y value of velocity shouldn't be able to be zero, ever. Let's do something to make the logs a bit cleaner. Remove the * Time.deltaTime from movementSpeed and gravSpeed, and instead change the move to controller.Move((gravSpeed + movementSpeed) * Time.deltaTime);. Don't change the log.
i cut and pasted it down to the bottom of the script and now it is working as normal
but
the jump is now all weird
i did it
jump is still weird
If you comment out the animation lines, is the jump still weird
the y seems to be going back and forth
like from -1 to -6 then goes back to -1
and so on
is that normal?
When you land, yes. It should be blanking out your downward momentum so you don't "build up" gravity while standing still
yes
Well, I have to go so it might be best to take what you have and put it back in the main channel