Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Somewhat inconsistent raycast hits

Discussion in 'Scripting' started by CardboardKnight, Mar 24, 2021.

Thread Status:
Not open for further replies.
  1. CardboardKnight

    CardboardKnight

    Joined:
    Mar 8, 2021
    Posts:
    10
    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.


    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;

    Code (CSharp):
    1.  
    2. float vertical;
    3. public float verticalSpeed;
    4.  
    5. RaycastHit2D hitDown
    6.  
    7. void Start()
    8. {
    9.     rigidbody2d = GetComponent<Rigidbody2D>();
    10. }
    11.  
    12. void Update()
    13. {
    14.     vertical = Input.GetAxis("Vertical");
    15. }
    16.  
    17. void FixedUpdate()
    18. {
    19.  
    20.     //Testing down hit
    21.     if(vertical < 0)
    22.     {
    23.         hitDown = Physics2D.Raycast(rigidbody2d.position, Vector2.down, detectionLength,          LayerMask.GetMask("World"));
    24.         if(hitDown.collider != null)
    25.             {
    26.                 vertical = 0;
    27.             }
    28.         }
    29.     Vector2 position = rigidbody2d.position;
    30.     position.y = position.y + verticalSpeed * vertical * Time.deltaTime;
    31.     rigidbody2d.MovePosition(position);
    32. }



    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.
     
    jasonasaad2 likes this.
  2. CardboardKnight

    CardboardKnight

    Joined:
    Mar 8, 2021
    Posts:
    10
    The message was held for moderation review for a while for some reason. Ill try to bump this once.
     
  3. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    your rigidbody2d has gravity off right?
    and what push your object down? by input?
     
    jasonasaad2 likes this.
  4. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    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.
     
    jasonasaad2 and Putcho like this.
  5. jasonasaad2

    jasonasaad2

    Joined:
    Nov 30, 2020
    Posts:
    51
    If none of those work, try putting the Raycast in normal update. For me Update usually works better.
     
  6. CardboardKnight

    CardboardKnight

    Joined:
    Mar 8, 2021
    Posts:
    10
    @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.
     
  7. CardboardKnight

    CardboardKnight

    Joined:
    Mar 8, 2021
    Posts:
    10
    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.
     
  8. sillyninja65

    sillyninja65

    Joined:
    Dec 1, 2022
    Posts:
    9
    cheers for telling us how you did it...
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    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.
     
    Kurt-Dekker likes this.
Thread Status:
Not open for further replies.