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.
http://i.imgur.com/S5GopU9.jpg (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.
Thanks.