transform rotations overwriting eachother
I have a script for making my character turn, and I have a script which rotates it based on the leg positions (for making my spider wobble and align to the terrain). Do any of you know how I can make these work together?
Currently #1 makes #2 not able to turn anymore.
1:
private void Update()
{
lastBodyUp = transform.up;
nbLegs = legTarget.Length;
if (nbLegs > 3 && bodyOrientation)
{
Vector3 v1 = legTarget[0].position - legTarget[3].position;
Vector3 v2 = legTarget[2].position - legTarget[1].position;
Vector3 normal = Vector3.Cross(v1, v2).normalized;
Vector3 up = Vector3.Lerp(lastBodyUp, normal, 1f / (float)(smoothness + 1));
// This works, but interfers with the mouseLook rotation of the Character
transform.up = up; // <==
lastBodyUp = up;
}
}
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:
var step = RotationSpeed * Time.deltaTime;
Quaternion lookRotation = Quaternion.Euler(0f, Camera.main.transform.eulerAngles.y, 0f);
transform.rotation = Quaternion.RotateTowards(transform.rotation, lookRotation, step);
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 gameobjects. I've also tried to play around with adding physics rotations instead, but with no luck yet.
The ideal solution is some way of adding these transforms together, to make both of them work somehow
Answer by BetaFruit · Jan 06 at 12:33 PM
This really is not something I understand at all but you could be having a problem where the scripts are setting the rotations they dont change to 0 and messing up the other ones. In that case make them use the localrotation value instead of 0. And if thats not your problem try making both rotation scripts work together in one script like adding both rotations together then applying them.
What do you mean by the scripts don't change to 0? I'm relatively new to Unity and still have some kinks and quirks to understand.
Regarding adding both rotations together, that's the solution I kind of want, but I'm having problems understanding how I can do that. In #1 I rotate the body using transform.up and Lerp, which works great. I am not 100% sure how transform.up works, and googling didn't make me understand it too much either (lol).
In #2 I use Quaternion.RotateTowards on transform.rotation. I believe the issue is that both methods use the transform to overwrite the other.
I might be asking the wrong question, but I believe it boils down to "Can I somehow merge Lerp on transform.up with Quaternion.LookTowards on transform.rotation?"