This is my code for turning. But for some reason my player only turns to the right.
void Turning()
{
Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(camRay, out hit, 100f, floorMask) || Physics.Raycast(camRay, out hit, 100f, wallMask))
{
Vector3 playerToMouse = hit.point - transform.position;
playerToMouse.z = 0f;
playerToMouse.x = 0f;
transform.Rotate(playerToMouse);
}
}
Not at the moment, but i might try it the other day^^. In the past default parameters weren't supported at all in Unity. So i always implemented an overload instead. Of course default parameters are a nice feature, but i prefer to keep my code backwardscompatible as much as possible.
Am I right in thinking you’re attempting to rotate by playerToMouse.y degrees about the y axis?
What you need to remember is that hit.point and transform.position are both points in 3d space so you’d be rotating by the height difference between hit.point and transform.position.
If transform.position is always on the ground then playerToMouse.y would always be greater than zero so you would always turn to the right.
You can use ArcTan (Unity - Scripting API: Mathf.Atan2) to find the actual angle between the mouse and player. On line 10 try this and see if it’s useful.
Not at the moment, but i might try it the other day^^. In the past default parameters weren't supported at all in Unity. So i always implemented an overload instead. Of course default parameters are a nice feature, but i prefer to keep my code backwardscompatible as much as possible.
– Bunny83