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 Dalek.skaro · Jun 22, 2015 at 05:53 AM · script.mousespaceturning

Mouse Position turning 3rd person space sim.

I am triyng to make a space exploration game, i want to use the mouse controls to turn the ship, the same sistem used in games as BattleStarGalactica Online, in which the position of the mouse in relation to the center of the screen is used to make the ship turn. I tried making that, but commands like "Input.mouseposition" are really tricky for me to completely understand, and once i use them there is the problem of triyng to redirect the origin of the mouseposition vector2 to the center of the screen. I already have done the trust forward and backwards :

 public class NavigationScript : MonoBehaviour {
 
 public float translation = 0F;
 
 void Update () {
 
         if (Input.GetAxis ("Mouse ScrollWheel") > 0) 
         {
             if (translation <= 0.5F) {translation+=0.1F;}
         } 
         else if (Input.GetAxis ("Mouse ScrollWheel") < 0) 
         {
             if (translation >= -0.4F) {translation-=0.1F;}
         }
         transform.Translate(0, translation, 0);
                     }
 }

I am still a beginner, can anyone help me, or at least give me some tips(or even one good tutorial), in making this turning algorithm (in C# plz) ?

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
Best Answer

Answer by AlwaysSunny · Jun 22, 2015 at 05:51 AM

There are several ways to approach this requirement. Assuming you want the ship to turn faster the further the mouse is from the screen center, this should get you started:

 public Camera mainCamera;  // assign your main camera
 public float rateOfRotation = 10f;

 void Update() {
   Vector3 lookVector = mainCamera.ScreenPointToRay(Input.mousePosition);
   Quaternion desiredRotation = Quaternion.LookRotation( lookVector, transform.up );
   transform.rotation = Quaternion.Lerp( transform.rotation, desiredRotation, rateOfRotation * Time.deltaTime );
 }

There are several ways to improve upon this, like creating a deadzone or maximum radius considered, but it should give you a good place to start.

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 Dalek.skaro · Jun 23, 2015 at 12:30 AM 0
Share

I made some alterations on your code because it wasn't translating: Vector3 lookVector = mainCamera.ScreenPointToRay(Input.mousePosition); from a Ray to a Vector3, so I did :

 public class NavigationScript : $$anonymous$$onoBehaviour {
 
     public Camera $$anonymous$$ainCamera;
     public float translation = 0F;
     public float LookSensitivity = 1F;
     public float LookSmothDamp = 0.1F;
 
     void Start () {
     
     }
 
     void Update () {
 
         Ray lookRay = $$anonymous$$ainCamera.ScreenPointToRay(Input.mousePosition);
         Quaternion desiredRotation = Quaternion.LookRotation (lookRay.direction, transform.up);
         transform.rotation = Quaternion.Lerp( transform.rotation, desiredRotation, LookSensitivity * Time.deltaTime * LookSmothDamp );
 
         if (Input.GetAxis ("$$anonymous$$ouse ScrollWheel") > 0) 
         {
             if (translation <= 0.5F) {translation+=0.1F;}
         } 
         else if (Input.GetAxis ("$$anonymous$$ouse ScrollWheel") < 0) 
         {
             if (translation >= -0.4F) {translation-=0.1F;}
         }
         transform.Translate(0, translation, 0);
                     }
         }

But the controls are very chaotic and I don´t know what to do to adjust them in a meaningful way, could you please take a look and help me do some "final" adjustements to this basic controls ?

avatar image AlwaysSunny · Jun 23, 2015 at 05:02 AM 1
Share

Not sure why I thought that approach was superior to this simplified version. Guess I'm so used to thinking in 3D, I go for that even when 2D is easier.

 void Update () { CamLook(); }
     
 public float lookRate = 5f;    
 public float maxEnvelope = 0.2f;    
 public float deadZone = 0.0001f;
 
 void CamLook() {            
     float xD = Input.mousePosition.x - (Screen.width*0.5f);
     float yD = Input.mousePosition.y - (Screen.height*0.5f);        
     Vector3 v = new Vector3( -yD, xD, 0 ) * 0.001f;        
     if (v.sqr$$anonymous$$agnitude < deadZone) return;            
     v = v.normalized * $$anonymous$$athf.$$anonymous$$in( v.magnitude, maxEnvelope );        
     transform.Rotate( v * lookRate * Time.deltaTime );
 }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Can't shoot towards mouse click point 1 Answer

How to set up a space game with Evochron: Mercenary like controls? 0 Answers

Unity Ui. Why constant pixel size option in Canvas Scaler don't work with mouse? 1 Answer

Move object towards mouse in full 3D space 2 Answers

Turning animation using mouse value 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