- Home /
space flight controls help
I am making a flight sim like freelancer and black prophicy but I have a problem. I want it so the camera follows the mouse or crosshairs but is not linked to my ship, at the same time I want the ship to follow the mouse but not in a parrel line,so it creates the feeling that the ship is following the mouse with a bit of dely and the forward point of the ship moves towards the mouse instead of the hole ship, I tryed puting a empty game obj in front of the ship anf making it the parent but it hadnt worked,
Does anyone know like a fourm post or know what im doing wrong or even can tell me what to do, Ive seen two other posts like this and used the scripts but they didn't work
Answer by robertbu · Jan 28, 2013 at 04:19 PM
I'm not sure what you want from your description. If you want the camera to change its look direction when the mouse is on the edge of the frame like I saw in a Freelancer video on YouTube you can do something like:
public class MouseFollowWithDelay : MonoBehaviour {
public float maxDegreesPerSecond = 30;
private Camera cam;
private Quaternion qTo;
private bool bRot = false;
void Start () {
cam = GetComponent<Camera>();
qTo = transform.rotation;
}
void Update () {
Vector2 v2ViewportPos = cam.ScreenToViewportPoint(Input.mousePosition);
if ((v2ViewportPos.x < 0.1 || v2ViewportPos.x > 0.9 || v2ViewportPos.y < 0.1 || v2ViewportPos.y > 0.9)) {
if (!bRot) {
bRot = true;
Vector3 v3WorldPos = Input.mousePosition;
v3WorldPos.z = cam.nearClipPlane;
v3WorldPos = cam.ScreenToWorldPoint(v3WorldPos);
qTo = Quaternion.LookRotation(v3WorldPos-transform.position);
}
}
else
bRot = false;
transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, maxDegreesPerSecond * Time.deltaTime);
}
}
Note you'll need to do a bit more work to get the camera to ease into place rather than an abrupt stop I have here, but this should get you started.
In the video @alucardj suggested, the camera does not appear to follow the mouse. That is there are segments where the "fighter" is at the top of the screen engaging the enemy, but the camera never rotates in that direction. It appears that the camera is script driven and not controlled by the user. The question says "I want it so the camera follows the mouse."
Okay, here is combined camera and "fighter" movement, both following the mouse. Put this on the camera:
public class $$anonymous$$ouseFollowWithDelay2 : $$anonymous$$onoBehaviour {
public float maxDegreesPerSecond = 3;
private Camera cam;
private Quaternion qTo;
private bool bRot = false;
void Start () {
cam = GetComponent<Camera>();
qTo = transform.rotation;
}
void Update () {
Vector3 v3WorldPos = Input.mousePosition;
v3WorldPos.z = cam.nearClipPlane;
v3WorldPos = cam.ScreenToWorldPoint(v3WorldPos);
qTo = Quaternion.LookRotation(v3WorldPos-transform.position);
transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, maxDegreesPerSecond * Time.deltaTime);
}
}
Put this on the "fighter:"
public class Fighter : $$anonymous$$onoBehaviour {
public float fSpeed = 0.1f;
private Camera cam;
private float fDist;
void Start () {
cam = Camera.main;
fDist = (transform.position - cam.transform.position).magnitude;
}
void Update () {
Vector3 v3WorldPos = Input.mousePosition;
v3WorldPos.z = fDist;
v3WorldPos = cam.ScreenToWorldPoint(v3WorldPos);
transform.position = Vector3.$$anonymous$$oveTowards (transform.position, v3WorldPos, fSpeed);
}
}
Note there is a lot of things going on in the video, like bank turns to the new position. This may not be the right approach, but it is a starting place.