Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Object Not Moving After Turning

Discussion in 'Scripting' started by CallOfIshmael, May 7, 2024.

  1. CallOfIshmael

    CallOfIshmael

    Joined:
    Oct 22, 2023
    Posts:
    5
    I am making a dungeon-crawling game for class. I am currently working on a "Bouncing Moving Behavior". Essentially, whenever the player moves, the enemy will also move into an open space. Whenever the enemy is trying to move and there is a wall in front of them, the enemy should rotate 180 degrees and continue moving until they encounter another wall, repeating continuously.
    The problem I am having is that when the enemy runs into the wall, they only turn 180 degrees once and move one space. After that, they do not move anymore. It wouldn't even run the code anymore.

    Any and all help is welcome!

    Below is the Update part of the code.

    Code (CSharp):
    1.  private void Update()
    2. {
    3.      CheckWall();
    4.      Debug.DrawRay(transform.position, raycastDir * raycastDistance, Color.green);
    5.      raycastDir = transform.TransformDirection(0, 0, raycastDistance);
    6.  
    7.      //Move Enemy
    8.      if (PlayerController.Instance.playerTurn && AtRest)
    9.      {
    10.          switch (moveBehave)
    11.          {
    12.              //Move in opposite direction when facing a wall
    13.              case Movement.Bounce:
    14.  
    15.                
    16.                  if (facingWall == false)
    17.                  {
    18.                      targetGridPos += (transform.forward * moveMulti);
    19.                  }
    20.                  if (facingWall == true)
    21.                  {
    22.                      //Turn 180 degrees
    23.                      transform.RotateAround(transform.position, transform.up, 180f);
    24.                      targetGridPos += (transform.forward * moveMulti);
    25.                  }
    26.                
    27.  
    28.                  break;
    29.  
    30.              //Turn right and move when facing wall
    31.              case Movement.Turn:
    32.                  if (facingWall == true)
    33.                  {
    34.                      //Turn 90 degrees
    35.                      transform.RotateAround(transform.position, transform.up, 90f);
    36.                  }
    37.                  targetGridPos += (transform.forward * moveMulti);
    38.                  break;
    39.  
    40.  
    41.          }
    42.      }
    43. }
    Below is the function that detects whether it is facing a wall.
    Code (CSharp):
    1. private void CheckWall()
    2. {
    3.     RaycastHit hitFront;
    4.     if (Physics.Raycast(transform.position, raycastDir, out hitFront, raycastDistance))
    5.     {
    6.         switch (hitFront.collider.gameObject.tag)
    7.         {
    8.             case "Wall":
    9.                 facingWall = true;
    10.  
    11.                 break;
    12.  
    13.             default:
    14.  
    15.                 break;
    16.         }
    17.     }
    18.     else
    19.     {
    20.         facingWall = false;
    21.     }
    22.  
    23. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,115
    That's a... slightly... unusual way to put it, but okay.

    It sounds to me like it is time for you to start debugging.

    By debugging you can find out exactly what your program is doing so you can fix it.

    https://docs.unity3d.com/Manual/ManagedCodeDebugging.html

    Use the above techniques to get the information you need in order to reason about what the problem is.

    You can also use
    Debug.Log(name);
    to find out if any of your code is even running. Don't assume it is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.
     
  3. CallOfIshmael

    CallOfIshmael

    Joined:
    Oct 22, 2023
    Posts:
    5

    To elaborate: once the enemy detects that they are facing a wall, they turn around, move once, and won't move anymore.
    I also tried debugging the script. When I place a breakpoint, the software would pause the game at that moment and show the process of running that code, right? When the enemy does turns away from the wall, the program doesn't run through the script anymore. I have also tried using the "debug.Log" function, and it works until the the enemy turns away from the wall
     
  4. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    You're using raycastDir before you re-calculate it.
    You probably want to re-calculate it at the very start of Update()
     
  5. CallOfIshmael

    CallOfIshmael

    Joined:
    Oct 22, 2023
    Posts:
    5
    Ugh, found the issue! It was part of the turning function (which I did not paste here). thank you!