Raycast length problem

Hello guys
Im trying to make a simple sidescroller and im already struggeling on the player controller. so the left and right walking works fine, and actually the jumping works too but i ran into the problem, that i can keep jumping while in air. so i can pretty much spam the space button and well…go to space with it. so i tought implementing a “isGrounded” bool makes sense. for that i use a raycast that is slightly biger than my player. and i want that if a collider touches the raycast, the bool gets turned to “isGrounded” wich makes jumping possible. first i just had the problem that as soon as i start the game the bool turned to true. no matter how high in the air i started with the player. and first i really didnt figure out the problem. but then i tryed to start the player NOT ontop of a collider. so that he pretty much falls into nothing. and there i have seen the is grounded bool stayed on false. wich makes me think that it somewhat works. tough it seems like my ray gets casted infinite. so if there is anywhere, no matter how far under my player a collider, it goes to true. wich is strange since my DrawRay gets shown properly. id be really happy if someone can tell me what im doing wrong.
thank you in advance and sorry for my english
greetings Ilias

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

public class movement : MonoBehaviour
{
    public int speed = 3;
    public int jumpForce = 200;
    public float playerHeight;

    public bool grounded;

    private Transform player;
    private Rigidbody rigidbody;

    void Start ()
    {
        player = GetComponent<Transform>();
        rigidbody = GetComponent<Rigidbody>();
    }
   
    void Update ()
    {
        transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * speed * Time.deltaTime);

        if (Input.GetButtonDown("Jump") && grounded)
        {
            rigidbody.AddForce(transform.up * jumpForce);
        }
    }

    void FixedUpdate()
    {
        Vector3 dwn = transform.TransformDirection(Vector3.down);
        Debug.DrawRay(transform.position, dwn * playerHeight, Color.red);
        RaycastHit hit;

        if (Physics.Raycast(transform.position, dwn, out hit))
        {
            if (hit.collider)
            {
                grounded = true;
                Debug.Log("grounded");
            }
        }
    }
}

There are several overloads to the method: Unity - Scripting API: Physics.Raycast

Check out the options. Yes, one of them includes a max distance to check (and yes, it’s infinite by default) :slight_smile:
I think this is the one you’re using atm?

public static bool Raycast(Vector3 origin, Vector3 direction, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

(and yes, it’s infinite by default)
really thanks
I always forget to put max_distance , because of the Drawline way , it cheats me ,
and I feel sometimes like there is A Logical Error lol hh

Then maybe you’re better off using a line-segment with Physics.LineCast which has an explicit start and end point.

Note that you’re replying to an old post.

1 Like