- Home /
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.
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.
Answer by Tomer-Barkan · Nov 26, 2013 at 06:28 AM
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;
}
I'm not smart enough to understand this. Once I'm better at program$$anonymous$$g and trigonometry I'll come back to this. I'll mark you as the rite answer :)
lol sorry about that. You definitely need a basic understand of 3d geometry to use unity well.
I'll try to explain a bit more:
The ship is pointing forward. The camera is rotated so that it is looking from right to left. So the ship's forward vector is pointing straight forward, and the camera's forward vector is pointing straight left. Hence the angle between them is 90 degrees.
Now if the guns can only fire 30 degrees, the vector pointing from the ship to the crosshairs cannot be the same as the one of the camera, because that would be 90 degrees. So ins$$anonymous$$d, you take the ship's forward vector (the one pointing straight ahead), and you rotate it towards the camera's vector (the one pointing left), but you limit the rotation to a maximum of 30 degrees. (thats the 3rd parameter of Vector3.RotateTowards
).
What you get is a vector that is rotated towards the direction the camera is looking, but no more than 30 degrees, which means it is within the gun's range.
Then it is a simple matter of starting from the ship's position, then moving X units towards the calculated vector, and you get the location of the crosshairs. (X being distanceFromShip
, the third parameter to the function)