Ok, so this is a follow up to my previous question and I think I found a solution. I’ve created a new gameObject that follows the same rotation as my playerObject and I’ve attached two scripts to that object, one to stop the rotation within a certain range above the player and one for below the player.
public GameObject player;
public float turnSpeed = 6f;
public bool lookAtMouseEnabled;
float minUpRotation = 350f;
float maxUpRotation = 10f;
// Start is called before the first frame update
void Start()
{
if (lookAtMouseEnabled)
{
player.GetComponent<PlayerMovement>().Lookatmouse();
}
}
// Update is called once per frame
void Update()
{
Rotate();
float yRotation = transform.rotation.eulerAngles.y;
if (yRotation >= minUpRotation || yRotation <= maxUpRotation)
{
lookAtMouseEnabled = false;
}
else
{
lookAtMouseEnabled = true;
}
}
public void Rotate()
{
Plane playerplane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (playerplane.Raycast(ray, out float hitdist))
{
Vector3 targetpoint = ray.GetPoint(hitdist);
Quaternion targetrotation = Quaternion.LookRotation(targetpoint - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetrotation, turnSpeed * Time.deltaTime);
}
}
The other script is the same but between 170f and 190f.
The problem is I can see the lookAtMouseEnabled bool being checked in the right areas, but when the gameObject rotates out of the min and max ranges the playerObject rotation is still stuck in one area?
Honestly, I’m so tired now. It’s possibly something simple that I’ve just skimmed over.