Help creating restraints for an objects movement according to it's parent

I couldn’t come up with a great title, but my situation is messing with my head. My game is a space game where you control a plane in third person. The camera is using this orbit script:

[AddComponentMenu(“Camera-Control/Mouse Orbit with zoom”)]
public class mouseorbit : MonoBehaviour {

public Transform target;
public float distance = 5.0f;
public float xSpeed = 120.0f;
public float ySpeed = 120.0f;

public float yMinLimit = -20f;
public float yMaxLimit = 80f;

public float distanceMin = .5f;
public float distanceMax = 15f;

float x = 0.0f;
float y = 0.0f;

// Use this for initialization
void Start () {
	Vector3 angles = transform.eulerAngles;
	x = angles.y;
	y = angles.x;
	// Make the rigid body not change rotation
	if (rigidbody)
		rigidbody.freezeRotation = true;

void LateUpdate () {
	if (target) {
		x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
		y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
		y = ClampAngle(y, yMinLimit, yMaxLimit);
		Quaternion rotation = Quaternion.Euler(y, x, 0);
		distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
		RaycastHit hit;
		if (Physics.Linecast (target.position, transform.position, out hit)) {
			distance -=  hit.distance;
		Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
		Vector3 position = rotation * negDistance + target.position;
		transform.rotation = rotation;
		transform.position = position;

public static float ClampAngle(float angle, float min, float max)
	if (angle < -360F)
		angle += 360F;
	if (angle > 360F)
		angle -= 360F;
	return Mathf.Clamp(angle, min, max);


I have a unique aiming mechanic. There is a cross hair across from the camera on the other side of the ship as a parent of the camera. The gun points use look at to aim at the crosshair

This way, Where ever I orbit the camera, the cross hair is in the middle farther away.

The issue with this is that guns can’t shoot backwards. I need to constrain how far the cross hair can point away from straight forward. I want the camera to still be able to look backwards, while the cross hair gets as close as it can to being in your view; in turn, preventing the player from shooting at too much of an angle. (ran out of attachments)

Unfortunately, I can’t even begin to think of how this could be accomplished in unity.
So any Advice, ideas, alternatives or solutions would superb.


You could keep two separate angles, one for the camera, and one for the crosshairs. So the camera can move freely 360, but the crosshairs will be calculated according to the camera.

If the camera is between -30 and 30 (say 30 is the maximum angle of the guns) from the ship’s forward vector, then the crosshairs angle remains exactly like the camera angle. If the camera is between 30 and 180 degrees from the ship’s forward, then the crosshairs remains 30. If on the other hand it is between -30 and -180 (or 180 and 290, which is the same) then your crosshairs remain -30.

You can calculate the crosshairs angle every frame based on the camera angle.

Some sample script:

public Vector3 GetCrosshairsPosition(Transform ship, Transform camera, float maxAngle, float distanceFromShip) {
    Vector3 directionFromShip = Vector3.RotateTowards(ship.forward, camera.forward, Mathf.Deg2Rad * maxAngle, 0);
    return ship.position + directionFromShip * distanceFromShip;