- Home /
humanoid not aiming properly
I am rotating the chest of my humanoid model and it is acting strange. At normal rotation, the model looks fine. But when aiming up or down, it sort of aims sideways if you can tell in the 2nd picture.
I think it has something to do with the chest being rotated slightly during the aiming animation but I'm not sure. Here is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class playerscr : MonoBehaviour { Animator anim; Transform chest; float rotx = 0f; float roty = 0f; // Start is called before the first frame update void Start() { anim = GetComponent(); chest = anim.GetBoneTransform(HumanBodyBones.Chest); }
// Update is called once per frame
void Update()
{
}
void LateUpdate()
{
rotx += Input.GetAxis("Mouse X");
roty -= Input.GetAxis("Mouse Y");
chest.rotation *= Quaternion.Euler(roty, rotx, 0);
}
}
Answer by highpockets · Apr 14, 2019 at 02:53 PM
In your aiming animation, is everything fine?? (if you don't apply this script, I mean). You are not saving the last rotation every frame, the next time the bones update, your code in LateUpdate will be overwritten and thus you should store a lastChestRotation Quaternion variable every frame and apply it so as to have this script act as a masking animation of the chest. You should also multiply the other way around:
chest.rotation = Quaternion.Euler(roty, rotx, 0) * chest.rotation;
I just tried the code you changed actually and it works at first, but when the actual character is turning it gets messed up and starts to look sideways again.
What do you mean, when the actual character is turning?? You mean when the root node starts turning?? Are you saving the last rotation every frame??
In my regular update function I added this
lastrot = chest.rotation;
Except I don't know if this is right and how I would use the lastrot variable.
And in my lateupdate function I made it so that my entire character rotates when I move the mouse along with my chest code.
chest.rotation = Quaternion.Euler(roty, rotx, 0) * chest.rotation; transform.rotation = Quaternion.Euler(0, rotx, 0);
When I rotate the transform of my player, the rotation of my chest gets messed up again
Hey I figured it out. Your code works, it was just the animation was really weird.
Oh that’s good. I was going to code you something different when I had time, but that’s good it works. I don’t personally like using Euler angles much. Anyhow, glad to help. Please mark the answer as correct to have it resolved
Your answer
Follow this Question
Related Questions
How to change the axis of bone of humanoid model? 1 Answer
Finger bone rotation axis is wrong 1 Answer
High Quality rig with Mecanim 0 Answers