Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by StAtic · Jan 28, 2013 at 02:27 PM · controlsspace shooterspaceflight

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Jan 28, 2013 at 05:26 PM 0
Share

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

avatar image robertbu · Jan 28, 2013 at 06:04 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

player controls question 0 Answers

Fluid and Constant Rotation for Space Sim? 0 Answers

My character moves in seemingly random directions. 1 Answer

How do I have button navigation work for only one of my players? 1 Answer

How do I change controls by pressing one button? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges