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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Box Cast Lagging Behind When Moving Player

Discussion in 'Scripting' started by energy980, Feb 23, 2020.

  1. energy980

    energy980

    Joined:
    Feb 22, 2020
    Posts:
    24
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class playerMovement : MonoBehaviour
    6. {
    7.     [SerializeField] private LayerMask platformLayerMask;
    8.     public float speed = 10.4f;
    9.     void start()
    10.     {
    11.     }
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         Vector2 pos = transform.position;
    16.         // Walking right
    17.         if (Input.GetKey("d"))
    18.         {
    19.             pos.x += speed * Time.deltaTime;
    20.         }
    21.         // Walking left
    22.         if (Input.GetKey("a"))
    23.         {
    24.             pos.x -= speed * Time.deltaTime;
    25.         }
    26.         // Jumping
    27.         if (IsGrounded() && Input.GetKeyDown(KeyCode.Space))
    28.         {
    29.             Rigidbody2D rigidbody2D = GetComponent<Rigidbody2D>();
    30.             rigidbody2D.velocity = Vector2.up * 7;
    31.         }
    32.         transform.position = pos;
    33.         Rigidbody2D rigidbodyGravity = GetComponent<Rigidbody2D>();
    34.         rigidbodyGravity.gravityScale = 1.5f;
    35.     }
    36.     private bool IsGrounded()
    37.     {
    38.         BoxCollider2D boxCollider2D = GetComponent<BoxCollider2D>();
    39.         float extraHeightText = 0.05f;
    40.         RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider2D.bounds.center, boxCollider2D.bounds.size, 0f, Vector2.down, extraHeightText, platformLayerMask);
    41.         Color rayColor;
    42.         if (raycastHit.collider != null)
    43.         {
    44.             rayColor = Color.green;
    45.         }
    46.         else
    47.         {
    48.             rayColor = Color.red;
    49.         }
    50.         // Right Ray Cast
    51.         Debug.DrawRay(boxCollider2D.bounds.center + new Vector3(boxCollider2D.bounds.extents.x, 0), Vector2.down * (boxCollider2D.bounds.extents.y + extraHeightText), rayColor);
    52.         // Left Ray Cast
    53.         Debug.DrawRay(boxCollider2D.bounds.center - new Vector3(boxCollider2D.bounds.extents.x, 0), Vector2.down * (boxCollider2D.bounds.extents.y + extraHeightText), rayColor);
    54.         // Bottom Ray Cast
    55.         Debug.DrawRay(boxCollider2D.bounds.center - new Vector3(boxCollider2D.bounds.extents.x, boxCollider2D.bounds.extents.y + extraHeightText), Vector2.right * (boxCollider2D.bounds.extents.x * 2), rayColor);
    56.         Debug.Log(raycastHit.collider);
    57.         return raycastHit.collider != null;
    58.     }
    59. }
    60.  


    There is my code and a video of what is happening. I didn't know what else to call it besides lagging behind. I want the box cast to fix perfectly as the player moves around. Thanks for all the help.
     
    Last edited: Feb 23, 2020
  2. Technokid2000

    Technokid2000

    Joined:
    Dec 18, 2016
    Posts:
    36
    Looks like Debug.Draw statements are not completed in the exact same frame, but rather setup ready to be drawn in the next frame instead. MEANING, anything using that function will be a frame or two lagging behind always
     
  3. energy980

    energy980

    Joined:
    Feb 22, 2020
    Posts:
    24
    Do you see anything in my code that could fix this? I kind of get what you mean, but I'm not sure where to start to fix the issue.
     
  4. Technokid2000

    Technokid2000

    Joined:
    Dec 18, 2016
    Posts:
    36
    Nothing really stands out as obvious. I'm not sure if there is a "Same frame" version of the function, but I think it's just an inbuilt Unity3D thing. By the looks of it, it's an issue with how Unity dedicated graphics ray tracing. It will always draw the rays 1 frame afterwards. MEANING, without finding another function similiar to "Debug.DrawRay()" but operating within the same frame, I don't think there is anything that can be done code-wise. The only thing I can think of is to get a Unity3D Dev in to help out, and even then I don't think there's much that can be done
     
  5. energy980

    energy980

    Joined:
    Feb 22, 2020
    Posts:
    24
    Thanks for the help either way.