Camera (view locking and movement) on a rigidboy

I am trying to make a space game in which a player can be sent in space, this player has a rigid body, so it can rotate on all axis. But with this came a problem about the view.

I must have a view relative to the player orientation, and this in a restricted view cone.

Lets say a view cone restriction of -60 and 60, you can be thrown in space, you would be rotating on the z axis, so would be your view. But what i want is the view to be realist.
I made a planet that we can land on, but, if you land under the planet, you see upside down, and if you go on the sides, you see the planet surface on the side. → This is what i want to fix.

(i already have the gravity code for the player orientation for the planet, but not the view)

using UnityEngine;
using System.Collections;

public class GravitationMovement : MonoBehaviour
{
    public Transform gravitySource;
    public float gravScalar = 4.0f;
    public Vector3 rotOffset = Vector3.zero;

    void FixedUpdate ()
	{
		var direction = gravitySource.position - transform.position;
		rigidbody.AddForce(direction.normalized * gravScalar);
		transform.rotation = Quaternion.LookRotation(direction.normalized) * Quaternion.Euler(rotOffset);
    }
}

I now need a script to control how the view would turn relative to the parent.

Here is what i tried, and it failed hard, i think i need quaternions, but i couldn’t find helpful references anywhere, even on the wiki.

using UnityEngine;
using System.Collections;

public class CameraMovement : MonoBehaviour
{
	bool lockView = false;
	
	float sensitivityPitch = 4F;
	float sensitivityYaw = 4F;
	float rotationPitch;
	float rotationYaw;
	float rotationRoll;
	
	float rotMinimumPitch = 0;
	float rotMaximumPitch = 0;
	
	float rotMinimumYaw = 0;
	float rotMaximumYaw = 0;
	
	void Update ()
	{
		rotationYaw += Input.GetAxis("Mouse X") * sensitivityYaw;
		rotationPitch += Input.GetAxis("Mouse Y") * sensitivityPitch;
		
		Mathf.Clamp(rotationYaw, rotMinimumYaw, rotMaximumYaw);
		
		rotMinimumPitch = transform.parent.eulerAngles.x - 10;
		rotMaximumPitch = transform.parent.eulerAngles.x + 10;
		
		rotMinimumYaw = transform.parent.eulerAngles.y - 10;
		rotMaximumYaw = transform.parent.eulerAngles.y + 10;
		
		print("minpitch = " + rotMinimumPitch);
		print("maxpitch = " + rotMaximumPitch);
		
		print("minyaw = " + rotMinimumYaw);
		print("maxyaw = " + rotMaximumYaw);
		
		if (rotationPitch > rotMinimumPitch)
		{
			if (rotationPitch < rotMaximumPitch)
			{
				Vector3 rot = new Vector3(-rotationPitch, rotationYaw, 0);
				transform.localEulerAngles = rot;
			}
			else
			{
				transform.eulerAngles = new Vector3(rotMaximumPitch, transform.eulerAngles.y, transform.eulerAngles.z);
			}
		}
		else
		{
			transform.eulerAngles = new Vector3(rotMinimumPitch, transform.eulerAngles.y, transform.eulerAngles.z);
		}
		
		if (rotationYaw > rotMinimumYaw)
		{
			if (rotationYaw < rotMaximumYaw)
			{
				Vector3 rot = new Vector3(-rotationPitch, rotationYaw, 0);
				transform.localEulerAngles = rot;
			}
			else
			{
				transform.eulerAngles = new Vector3(transform.eulerAngles.x, rotMaximumYaw, transform.eulerAngles.z);
			}
		}
		else
		{
			transform.eulerAngles = new Vector3(transform.eulerAngles.x, rotMinimumYaw, transform.eulerAngles.z);
		}
	}
}

so, unfortunately 3D math and quaternion madness is my weak point…
however, this should at least handle the orientation so that the camera’s “forward” is always perpendicular to the surface of the planet. My answer does not take into account user rotations or distance from the surface.

I don’t think this will completely solve your problem, but perhaps give you a starting point on how to ensure that the “planet” is always “down” to your camera.

anyhoo, one line of code for rotations (again, this doesn’t handle user look rotation or distance from surface)

transform.rotation = Quaternion.LookRotation(Vector3.Cross(transform.position - center.position, center.position), transform.position - center.position);

Nevermind, i found it out!!
I just had to use the localEulerAngles more, i tried it at first but it wasn’t working right so i deleted the code for it and went with the hard way xD

So, here’s the code if anybody tried searching similar things.

using UnityEngine;
using System.Collections;

public class CameraMovement : MonoBehaviour
{
	bool lockView = false;
	
	float sensitivityPitch = 4F;
	float sensitivityYaw = 4F;
	public float rotationPitch;
	public float rotationYaw;
	
	float rotMinimumPitch = 0;
	float rotMaximumPitch = 0;
	
	float rotMinimumYaw = 0;
	float rotMaximumYaw = 0;
	
	void Update ()
	{
		rotationYaw += Input.GetAxis("Mouse X") * sensitivityYaw;
		rotationPitch += Input.GetAxis("Mouse Y") * sensitivityPitch;
		
		rotMinimumPitch = -10;
		rotMaximumPitch = 10;
		
		rotMinimumYaw = -10;
		rotMaximumYaw = 10;
		
		if (rotationPitch < rotMinimumPitch)
		{
			rotationPitch = rotMinimumPitch;
			transform.localEulerAngles = new Vector3(rotMinimumPitch, transform.localEulerAngles.y, transform.localEulerAngles.z);
		}
		
		if (rotationPitch > rotMaximumPitch)
		{
			rotationPitch = rotMaximumPitch;
			transform.eulerAngles = new Vector3(rotMaximumPitch, transform.localEulerAngles.y, transform.localEulerAngles.z);
		}
		
		if (rotationYaw < rotMinimumYaw)
		{
			rotationYaw = rotMinimumYaw;
			transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, rotMinimumYaw, transform.localEulerAngles.z);
		}
		
		if (rotationYaw > rotMaximumYaw)
		{
			rotationYaw = rotMaximumYaw;
			transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, rotMaximumYaw, transform.localEulerAngles.z);
		}
		
		Vector3 rot = new Vector3(-rotationPitch, rotationYaw, 0);
		transform.localEulerAngles = rot;
	}
}

Also, a note to the people like me who duplicate the same codes in 2 different if statements; put it one time after both ifs, that’s win.