Home > Mobile >  OnGround Raycast not working after scaling fps character
OnGround Raycast not working after scaling fps character

Time:10-03

I am trying to make a FPS Game. In the Movement.cs. The Raycast starts from the center of the Capsule (player's transform.position) and is sent towards Vector3.down, with the limit of playerHeight / 2 0.1f, where playerHeight is the height of the CapsuleCollider of the player (Capsule).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class checkJump : MonoBehaviour
{
    private Rigidbody rb;
    private float playerHeight = 2f; 
    private bool isOnGround;
    private float jumpSpeed = 400f;

    void Start() {
        rb = GetComponent<Rigidbody>();
    }
    void Update()
    {
        isOnGround = Physics.Raycast(transform.position, Vector3.down, playerHeight / 2   0.1f);
        Jump();
        Debug.Log(isOnGround);
    }

    void Jump() {
        if (Input.GetKeyDown(KeyCode.Space) && isOnGround)
        {
            rb.AddForce(Vector3.up * jumpSpeed * Time.deltaTime, ForceMode.Impulse);
        
        }
    }
}

I disabled all other scripts except "checkJump.cs" one. And when I set the scale value to (4,4,3) or something else, it shows me false in the console and nor does the player Jump when I press Enter. But when I set the scale value to (1,1,1) it shows true. In both cases, the player is on the surface of the ground.

Also tried on another Capsule GameObject, doesn't work.

Here are the images :

https://forum.unity.com/attachments/onescale-png.931606/

https://forum.unity.com/attachments/otherscale-png.931609/

CodePudding user response:

Your isOnGround variable is set to true only when the RayCast hits the ground. Since you've set it to transform.position, it's going to fire from the center of the character. You've also limited the distance it fires to playerHeight / 2 0.1, which is 1.1.

So basically, whenever your localScale.y is larger than 1.1, your RayCast no longer reaches the ground, and thus won't ever registering you being on the ground.

To solve this, multiply your RayCast distance by your localScale.y as well.

  • Related