Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Turning to camera direction OnStateUpdate

Discussion in 'Scripting' started by Djabenv, Apr 11, 2021.

  1. Djabenv

    Djabenv

    Joined:
    May 16, 2018
    Posts:
    19
    Hi,

    I'm pretty new to Unity, I watched a tutorial using StateMachineBehaviour scripts and liked it so testing it out. When I change from a MonoBehaviour script on the player to StateMachineBehaviour my player no longer turns! Can anyone help me out?

    I am using Cinemachine for a 3rd person camera, rootmotion for forward / backward strafe movement and the mouse / camera rotation for turning.

    The code works when attached to the character as MonoBehaviour, but doesn't work as StateMachineBehaviour. The Debug.Log is showing that there is a rotation but it isn't doing it.
    • control.forward and control.strafe are keybindings
    • turnspeed is set at 5.0f, same as previous
    • The Debug.Log is showing the correct position, rotation as zero and the target rotation as y0 to 1, depending on the rotation of camera to player)
    • Debug.Log example: (0.0, 0.1, 11.0) (0.0, 0.0, 0.0, 1.0)(0.0, 0.2, 0.0, 1.0)
    • I also tried: animator.transform.rotation = Quaternion.Euler(0, yawCamera, 0) but didn't work either
    • I think the only other script affecting movement is controling forward/backward and strafe, I don't think there is anything in it that could affect rotation.
    Code (csharp):
    1.  
    2. override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    3.     {
    4.         if (control.forward != 0 || control.strafe != 0) {
    5.             float yawCamera = Camera.main.transform.rotation.eulerAngles.y;
    6.             animator.transform.rotation = Quaternion.Lerp(animator.transform.rotation, Quaternion.Euler(0, yawCamera, 0), turnSpeed * Time.deltaTime);
    7.             Debug.Log(animator.transform.position + "  " + animator.transform.rotation + Quaternion.Euler(0, yawCamera, 0));
    8.         }
    9.     }
    10.  
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    Hm, I've never done that sort of transform update from within a state callback. You might need to delay the change until the next Update().

    You can do that by setting a flag and doing the work later.

    You can also create a System.Action variable in your class, then set it based on that code block, then check for it and execute it in the Update() loop.

    It might also be some weird animation timing issue.

    Either way, here is some timing diagram help:

    https://docs.unity3d.com/Manual/ExecutionOrder.html
     
    Djabenv likes this.
  3. Djabenv

    Djabenv

    Joined:
    May 16, 2018
    Posts:
    19
    Thanks for the quick reply Kurt! Do you think a state callback is the wrong place to put this kind of thing? I don't know how to use a System.Action variable but I decided to just change this bit of code back and put it in an Update().