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

Update Raycast disables player movement

Discussion in 'Scripting' started by ArilynArt, Sep 28, 2021.

  1. ArilynArt

    ArilynArt

    Joined:
    Apr 12, 2020
    Posts:
    4
    Hello, I've been developing code for a 3D isometric game and I'm currently having trouble with collisions. I'm adding a roll/dodge/dash move that is supposed to move the player forward while disabling all other movement. The problem I'm running into is trying to get the dash to stop if it hits a wall. Currently I have player movement set up like this:

    Code (CSharp):
    1.     void Move()
    2.     {
    3.         //if we're dodging no movement
    4.         if (dodge) return;
    5.  
    6.  
    7.         //calculate forward rotation based on input and incline and assign to point variable
    8.         CalculateForward();
    9.      
    10.         //if slope ahead compared to the current location is too high, return.
    11.         if (groundAngle >= maxGroundAngle) return;
    12.         //check if we're hitting anything on the ground layer in front of us.
    13.         if (Physics.Raycast(transform.position, point, out hitInfo, height + heightPadding, ground)) return;
    14.    
    15.         //move the player the direction they are facing in order to account for y-axis changes in terrain
    16.         transform.position += point * moveSpeed * Time.deltaTime;
    17.     }
    This all works as intended. When I move the character around it runs into walls and everything works like normal. When I try to do a dash, the character moves up and down inclines as they should, but the character moves through the walls. I tried solving it like this:

    First, the Dodge function is called when the dodge button is pressed

    Code (CSharp):
    1.     void Dodge(Vector2 m)
    2.     {
    3.         //if we're not already dodging and we're on the ground
    4.         if (!dodge && grounded)
    5.         {
    6.  
    7.             dodge = true;
    8.  
    9.             if (m != Vector2.zero)
    10.             {
    11.                //snap player rotation to inputted direction
    12.                 transform.forward = head;
    13.             }
    14.             else
    15.             {
    16.              
    17.             }
    18.          
    19.             //Start movement
    20.             StartCoroutine(DodgeMovement(0.5f));
    21.         }
    22.     }
    Which calls the following coroutine:

    Code (CSharp):
    1.     IEnumerator DodgeMovement(float duration)
    2.     {
    3.         //reset timer
    4.         float time = 0f;
    5.      
    6.  
    7.         while (time < duration)
    8.         {
    9.             //movement
    10.             dodge = true;
    11.              
    12.  
    13.             //Increase the timer
    14.             time += Time.deltaTime;
    15.  
    16.             yield return null;
    17.         }
    18.         //finish movement and remove dodge status.
    19.         dodge = false;
    20.      
    21.     }
    The next function is called on FixedUpdate, but here's the problem.

    Code (CSharp):
    1.     void DodgeMove()
    2.     {
    3.         if (dodge && !Physics.Raycast(transform.position, point, out hitInfo, height + heightPadding, ground))
    4.         {
    5.             //wall collision
    6.             print("dodging");
    7.          
    8.             transform.position += point * dashSpeed * Time.deltaTime;
    9.         }
    10.         else
    11.         {
    12.             moveSpeed = baseMoveSpeed;
    13.         }
    14.     }
    When I include the collision check in the if statement, the dodge has no movement but still prints "dodging" to the console. When I remove the collision check it works as intended but the player moves through walls, so same problem as before. When I try putting the collision check before both functions then neither the normal movement nor the dash movement works.

    Anyone have any idea why this check disables the movement? I can provide more information or files if necessary. Thank you in advance!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Go nuts with this sort of thing ^ ^ ^... print out all kinds of results as far as the name of things you hit with the raycast(s). For all you know you might even be hitting yourself (the player).

    Either way, you must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
    ArilynArt likes this.
  3. ArilynArt

    ArilynArt

    Joined:
    Apr 12, 2020
    Posts:
    4
    Thank you for the advice. I'll try to be more efficient in my debugging and see if I can come up with a solution.