x


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

more ▼

asked Jan 28, 2013 at 02:27 PM

StAtic gravatar image

StAtic
1 6 9 14

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jan 28, 2013 at 04:19 PM

robertbu gravatar image

robertbu
105k 19 37 114

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."

Jan 28, 2013 at 05:26 PM robertbu

Okay, here is combined camera and "fighter" movement, both following the mouse. Put this on the camera:

 public class MouseFollowWithDelay2 : MonoBehaviour {
     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 : MonoBehaviour {
     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.MoveTowards (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.

Jan 28, 2013 at 06:04 PM robertbu
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x224
x37
x13

asked: Jan 28, 2013 at 02:27 PM

Seen: 1060 times

Last Updated: Jan 28, 2013 at 06:04 PM