#absolute noob needs help with groundcheck error

1 messages · Page 1 of 1 (latest)

mortal wigeon
#

@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);

        }
    }
}```
lethal owl
#

Okay, so, what specifically is the problem now

mortal wigeon
#

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

lethal owl
mortal wigeon
#

yea

lethal owl
#

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

mortal wigeon
#

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

lethal owl
#

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

mortal wigeon
lethal owl
mortal wigeon
#

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

lethal owl
#

Before you call .Move, can you log gravSpeed + movementSpeed

#

And see if y is ever 0?

mortal wigeon
lethal owl
#

And this is with <= 0 or with the < 0 on the first if?

mortal wigeon
#

<= 0

#

ok groundcheck isnt flickering anymore i actually figured it out

#

the anim was before i called move

lethal owl
#

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.

mortal wigeon
#

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

lethal owl
mortal wigeon
#

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?

lethal owl
#

When you land, yes. It should be blanking out your downward momentum so you don't "build up" gravity while standing still

lethal owl
#

Well, I have to go so it might be best to take what you have and put it back in the main channel

mortal wigeon
#

unity just crashed and now when i open the project it seems to just fly straight down through the floor

#

everything was working and now i dont understand whats happening