Home > Software design >  my character is falling through the floor
my character is falling through the floor

Time:08-06

I'm not sure why my character is falling through the floor. My Rigidbody: has use gravity, no kinematic and Continuous Detection is set to Continuous. My character is in the sky but the isgrounded bool ticks to true when it touches the floor but falls through anyways My cube which is my floor has a box collider

 void Update()
    {
        Move();
        isGrounded = Physics.CheckSphere(GroundCheck.position, groundRadius, (int)whatisGround);

        if (isGrounded == true)
        {

            velocity.y = -1f;

            if (Input.GetKeyDown(KeyCode.Space))
            {
                velocity.y = JumpForce;
            }
        }
        else
        {
            velocity.y -= Gravity * -2f * Time.deltaTime;
        }


    }

    void Move()
    {
        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");

        Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;

        if (direction.magnitude >= 0.1f)
        {

            float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg   playerCamera.eulerAngles.y;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
            transform.rotation = Quaternion.Euler(0f, angle, 0f);

            Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
            controller.Move(moveDir.normalized * speed * Time.deltaTime);
        }
    }
    }

CodePudding user response:

Looks like your code tests for the isgrounded value, and if it is true, changes the vertical velocity to -1, which is down. Since you don't tell it to zero it's vertical speed, and I assume it has no collider, it keeps falling down.

You need to either use a collider or, if isgrounded is true, set the y velocity to 0.

  • Related