Help with clamping rotation, and using animations
So I created a GetMousePos() function to get the location of the mouse on screen and set my player gameObject to rotate towards the click.
I am using a mathf.clamp() to try and keep the x and z axis values at zero.
HOWEVER,
When I use a an attack animation within a certain range (usually within 1) the gameObjects x and z rotations go beyond zero.
Is there a way to keep the z and x rotations 0 no matter what????
(A little background, the GetMousePos() and Attack() [which contains the animations for attacking] both are in the update function and the GetMousePos() comes before attack. So I believe that during update the GetMousePos() sets the rotation a certain way but then the Attack() sets it another)
Thanks
EDIT: This is my function: void GetMousePos() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; Quaternion setRot;
if (Physics.Raycast(ray, out hit))
{
lookTarget = hit.point;
}
Vector3 lookDelta = (hit.point - transform.position);
Quaternion targetRot = Quaternion.LookRotation(lookDelta);
float rotSpeed = turnSpeed * Time.deltaTime;
setRot = Quaternion.Euler(Mathf.Clamp(0, minClamp, maxClamp), lookDelta.y, Mathf.Clamp(0, minClamp, maxClamp));
transform.rotation = Quaternion.RotateTowards(transform.rotation, setRot, rotSpeed);
}
EDIT 2:
So after some QA, I realized the problem is when I click the gameObject "Enemy" it must be hitting a ray against the top of the enemy and setting the rotation to be parallel with the height of the "Enemy"... Any insight into this?
I found my own solution!
All I had to do was insert an If statement above lookTarget:
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.tag == "Enemy")
{
lookTarget = opponent.transform.position;
}
}