title: Tilt Model According to Acceleration Unity
body: <p>I have been working on a <code>CharacterController</code> for some time now (here is the current code):</p>
  
  <pre><code>using UnityEngine;
  
  [RequireComponent(typeof(CharacterController))]
  class MomentumMovement : MonoBehaviour
  {
      public GameObject playerCamera;
      public GameObject playerModel;
  
      CharacterController controller;
      float speed = 400f;
      Vector3 lastVelocity;
  
      void Start()
      {
          controller = GetComponent&lt;CharacterController&gt;();
          lastVelocity = controller.velocity;
      }
  
      Vector3 ScaleDirectionVector(Vector3 direction)
      {
          float multiplier = 1 / (Mathf.Abs(direction.x) + Mathf.Abs(direction.z));
          return new Vector3(
              direction.x * multiplier,
              0,
              direction.z * multiplier
          );
      }
  
      void Move()
      {
          Vector3 moveVector = ScaleDirectionVector(playerCamera.transform.forward) * Input.GetAxis("Vertical");
          moveVector += ScaleDirectionVector(playerCamera.transform.right) * Input.GetAxis("Horizontal");
          moveVector *= speed * Time.deltaTime;
          controller.SimpleMove(moveVector);
          playerModel.transform.position = transform.position;
      }
  
      void RotateToVelocity()
      {
          Vector3 lookAt = transform.position + controller.velocity.normalized;
          Vector3 targetPostition = new Vector3(lookAt.x, transform.position.y, lookAt.z);
          if (targetPostition - transform.position != Vector3.zero)
          {
              Quaternion q = Quaternion.LookRotation(targetPostition - transform.position);
              transform.rotation = Quaternion.RotateTowards(transform.rotation, q, 500 * Time.deltaTime);
          }
  
      }
  
      Vector3 CalculateTilt(Vector3 acceleration)
      {
          return new Vector3(
              acceleration.z,
              0,
              acceleration.x
          );
      }
  
      void TiltToAcceleration()
      {
          Vector3 centerOfMass = controller.center + controller.transform.position;
          Vector3 acceleration = controller.velocity / Time.deltaTime - lastVelocity;
          Vector3 tilt = CalculateTilt(acceleration);
          Quaternion targetRotation = Quaternion.Euler(transform.eulerAngles + tilt);
          playerModel.transform.rotation = Quaternion.Lerp(playerModel.transform.rotation, targetRotation, 10 * Time.deltaTime);
      }
  
      void FixedUpdate()
      {
          Move();
          RotateToVelocity();
          TiltToAcceleration();
          lastVelocity = controller.velocity / Time.deltaTime;
      }
  }
  </code></pre>
  
  <p>And I have been stuck for a few days with adding an "acceleration tilt" effect on the model of the player.<br><br>
  <strong>It is important to note that the script is attached to a <code>GameObject</code> with a <code>CharacterController</code> attached on it, while the playerModel is a separate object in the scene, I am trying to prevent some 
  local-global rotation problems this way (I might be wrong here)</strong>
  <br><br>
  For some reason (this is the best variation of the code) the acceleration tilt looks proper only while accelerating towards positive z.<br>
  Now, I know this has something to do with trigonometry but I have tried many variations of <code>Sin(transform.eulerAngles.y) or Cos(transform.eulerAngles.y) * something</code> in the calculations but still couldn't make it work properly.
  <br><br>
  Can someone please point out my mistake? Thank you all.</p>
  
  <p><strong>Note:</strong> There are weird jitters in the model movement too, if someone could help me with that too that would be great.</p>