Somewhat inconsistent raycast hits

Hi.

I am currently developping a top down dungeon crawler and settled on a kinematic rigidbody for the main player since i dont plan on adding any physics.
I am using raycast based detection to check if i’ve hit a wall but it seems that the collisions are not always occuring at the same time.

Here’s a gif to illustrate what i mean.
consideratemaleaxolotl

Here you can see that sometimes the cube stops far away from the wall, and sometimes closer or even touching it.

Here’s the code i use to cast the ray;

float vertical;
public float verticalSpeed;

RaycastHit2D hitDown

void Start()
{
    rigidbody2d = GetComponent<Rigidbody2D>();
}

void Update()
{
    vertical = Input.GetAxis("Vertical");
}

void FixedUpdate()
{

    //Testing down hit
    if(vertical < 0)
    {
        hitDown = Physics2D.Raycast(rigidbody2d.position, Vector2.down, detectionLength,          LayerMask.GetMask("World"));
        if(hitDown.collider != null)
            {
                vertical = 0;
            }
        }
    Vector2 position = rigidbody2d.position;
    position.y = position.y + verticalSpeed * vertical * Time.deltaTime;
    rigidbody2d.MovePosition(position);
}

Im a bit confused as to how to improve the ray’s detection. Should i increase physics iteration? Should i cast a longer ray? Any help would be greatly appreciatted.

1 Like

The message was held for moderation review for a while for some reason. Ill try to bump this once.

your rigidbody2d has gravity off right?
and what push your object down? by input?

1 Like

You’re getting inconsistencies because you’re only zeroing out your velocity when you encounter a wall, rather than accounting for the distance to the wall. Instead of raycasting a fixed amount, you should be raycasting the distance that you are expecting to travel in the current frame. If you hit something, truncate the travel distance to be the distance to the wall. This will ensure that you always end up perfectly flush.

For more details, Sebastian Lague has an older tutorial series that illustrates this technique nicely.

2 Likes

If none of those work, try putting the Raycast in normal update. For me Update usually works better.

@Putcho ; My rigidbody is kinematic, it doesnt have gravity and yes it is being moved by input using “MovePosition” on the rigidbody.

@Madgvox ; Setting the raycast length to be equal to the distance im expecting to travel seems like it would fix the issue and remove the potential misses coming from the frame updates. I’ll try to change it up so that the ray’s length depends on my speed and will keep you updated.

Thanks to your input i was able to get a consistent collision point and got the player to stop precisely on the same point on every collision no matter the speed at which the collision happens.

cheers for telling us how you did it…

1 Like

Please don’t necro posts simply to criticise.

If you have a problem which is just as likely to be caused by something else, please create your own thread and when solved, posts your solution.

Thanks.

1 Like