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

Bug Subway surfer/ Character's rotation problem with CharacterController

Discussion in 'Physics' started by juanchocoreano, Mar 23, 2023.

  1. juanchocoreano

    juanchocoreano

    Joined:
    Oct 21, 2021
    Posts:
    8
    Hi! I am trying to make a game like subway surfers.

    I attached character controller. I created following script to move left & right.


    if (SwipeRIght)
    {
    if (m_side == SIDE.Mid)
    {
    NewXPos = XValue;
    m_side = SIDE.Right;
    //m_Animator.Play("dodgeRight");
    }
    else if (m_side == SIDE.Left)
    {
    NewXPos = 0;
    m_side = SIDE.Mid;
    //m_Animator.Play("dodgeRight");
    }
    }
    x = Mathf.Lerp(x, NewXPos, Time.deltaTime * speedDodge);
    m_characterController.Move((x - transform.position.x) * Vector3.right);

    The movement between 3 lanes works fine.

    But I have rotation problem with the character.
    When the running animation is activatin Position is moving by amination movement.
    It's not bad. However when moving "dodgeLeft" or "dodgeRight" is activated it's rotation keeps changing.
    So after few lane changes, it's y Y rotation is changed.

    upload_2023-3-24_1-24-3.png
    seems like unlike Rigidbody, the character controller can't fix rotations.

    Anyone can fix this problem? or Do I need to change it to rigidbody?
     
  2. unisuby

    unisuby

    Joined:
    Feb 12, 2024
    Posts:
    1
    Hello!

    It looks like you're facing a rotation issue with your character in your game similar to Subway Surfers. The problem seems to arise when the "dodgeLeft" or "dodgeRight" animations are activated, leading to unintended changes in the Y rotation after a few lane changes.

    To address this, you can try the following modifications in your script:

    Code (CSharp):
    1. if (SwipeRight)
    2. {
    3.     if (m_side == SIDE.Mid)
    4.     {
    5.         NewXPos = XValue;
    6.         m_side = SIDE.Right;
    7.         //m_Animator.Play("dodgeRight");
    8.     }
    9.     else if (m_side == SIDE.Left)
    10.     {
    11.         NewXPos = 0;
    12.         m_side = SIDE.Mid;
    13.         //m_Animator.Play("dodgeRight");
    14.     }
    15. }
    16.  
    17. x = Mathf.Lerp(x, NewXPos, Time.deltaTime * speedDodge);
    18.  
    19. // Store the current rotation before the movement
    20. Quaternion initialRotation = transform.rotation;
    21.  
    22. m_characterController.Move((x - transform.position.x) * Vector3.right);
    23.  
    24. // Reset the rotation to the initial value after movement
    25. transform.rotation = initialRotation;
    26.  

    This modification stores the initial rotation before the movement and resets it to the original value after the character controller has moved. This approach helps in maintaining a consistent rotation during lane changes and avoids unintended rotations accumulating over time.

    Give this a try, and it should help stabilize the rotation of your character. If the issue persists, consider exploring Rigidbody as an alternative solution, but the provided script adjustment might resolve your problem without the need for a significant change in the controller type.