Search Unity

How to make transform.up and transform.rotation work together?

Discussion in 'Scripting' started by Korthking, Dec 26, 2021.

  1. Korthking

    Korthking

    Joined:
    Jan 12, 2021
    Posts:
    5
    I have a script for making my character turn, and I have a script which rotates it based on the leg positions. Do any of you know how I can make these work together?

    Currently #1 makes #2 not able to turn anymore.

    #1:
    Code (CSharp):
    1. private void Update()
    2.     {
    3.         lastBodyUp = transform.up;
    4.  
    5.         nbLegs = legTarget.Length;
    6.         if (nbLegs > 3 && bodyOrientation)
    7.         {
    8.             Vector3 v1 = legTarget[0].position - legTarget[3].position;
    9.             Vector3 v2 = legTarget[2].position - legTarget[1].position;
    10.             Vector3 normal = Vector3.Cross(v1, v2).normalized;
    11.             Vector3 up = Vector3.Lerp(lastBodyUp, normal, 1f / (float)(smoothness + 1));
    12.             // This works, but interfers with the mouseLook rotation of the Character
    13.             transform.up = up; // <==
    14.             lastBodyUp = up;
    15.         }
    16.     }
    In this #1 code I do the mean position of my 4 legs, and rotates it based on their positions. It looks great for mechs and spiders on its own.

    #2:
    Code (CSharp):
    1.  
    2.             var step = RotationSpeed * Time.deltaTime;
    3.             Quaternion lookRotation = Quaternion.Euler(0f, Camera.main.transform.eulerAngles.y, 0f);
    4.             transform.rotation = Quaternion.RotateTowards(transform.rotation, lookRotation, step);
    5.  
    In this code I've used Cinemachine, and use the main camera to determine the direction of the camera, then make the character turn slowly towards it (steering with your camera)

    I'll also add that these are two different files on the same gameobject

    Any help is greatly appreciated.