• 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 AppleseedSmoothie · Jan 27, 2018 at 11:27 PM · scripting problemmovementairplane

Spaceship movement script?

I currently have basic movement system for my space w$$anonymous$$ch is W to go forward and rotates using the mouse but doesn't feel realistic at all and is too clunky feeling. So does anyone know any good scripts I cam implement or tips that would make a good spaces$$anonymous$$p/airplane movement mechanic? Preferably t$$anonymous$$rd person but first person s$$anonymous$$p controls are very similar to t$$anonymous$$rd person anyway. Really appreciate it. If you want any more specifics then I'll add them

Comment
Add comment · Show 1
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 Honorsoft · Feb 13, 2018 at 08:28 AM 0
Share

By the way, it's a 3rd-person ship script, it's simple and basic but it's not bad and easy to add to it. One thing that would be good is Unity's Rigidbody AddTorque, it can give your ship a realistic 'twisting' momentum on the X -axis, (Think of the old-school Asteroids arcade game).

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Honorsoft · Jan 28, 2018 at 04:36 AM

I was practicing my java with a s$$anonymous$$p script, feel free to use it. I am more familiar with C# so I wasn't sure about how to use the Input.mousePosition in javascript, so you'll get an error saying:

 INTERNAL_get_mousePosition is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 's$$anonymous$$p_control' on game object 'Player'.
 See "Script Serialization" page in the Unity Manual for further details.
 

But anyways, a basic, uncluttered system, and it works, I made one for C# but can't find it. Here's the javascript (s$$anonymous$$p_control.js), just add it to your 'Player' object and then attach the camera as a c$$anonymous$$ld of the 'Player'. You can also add a 3D model of a spaces$$anonymous$$p as a c$$anonymous$$ld of the 'Player' object too.

 public var MousePosition = Input.mousePosition;
 public var position;
 public var curS$$anonymous$$pSpeed : float = 0.0;
 public var step = 2;
 public var current_speed = 0;
 public var maxSpeed : int = 100;
 public var mouse_rotate = 0.5;
 public var agility = 30;
 public var dist_fr_s$$anonymous$$p = 2.85;
 public var rotate_speed = 2.0;
 
 function Update () {
 
         if (Input.GetKey ("d")) 
         {
             transform.Rotate(0,0,-rotate_speed);
         }
 
         if (Input.GetKey ("a")) 
         {
             transform.Rotate(0,0,rotate_speed);
         }
         if ((Input.GetKey("up") || Input.GetKey("w")) && (current_speed < maxSpeed)) {
         current_speed += step;
     } else if ((Input.GetKey("down") || Input.GetKey("s"))  && (current_speed > 0)) {
         current_speed -= step;
     }
 
         MousePosition = Input.mousePosition;
         MousePosition.x = (Screen.height/2) - Input.mousePosition.y;
         MousePosition.y = -(Screen.width/2) + Input.mousePosition.x;
         transform.Rotate(MousePosition * Time.deltaTime * mouse_rotate, Space.Self);
 
     curS$$anonymous$$pSpeed = Mathf.Lerp(curS$$anonymous$$pSpeed, current_speed, Time.deltaTime * step);
     transform.position += transform.TransformDirection(Vector3.forward) * curS$$anonymous$$pSpeed * Time.deltaTime;
     
 }
 
Comment
Add comment · Show 1 · 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 Zodiarc · Feb 13, 2018 at 10:08 AM 0
Share

I think it would be better if you used velocity instead. Example:

 float acceleration = 1.5f; // change to what suits you best
 
 if(Input.GetKey(KeyCode.W)) {
     rigidbody.velocity += Vector3.forward * acceleration;
 }
  

This way if you let "W" go, the ship will maintain its movement direction (exactly how space works). You will have to turn the ship and make decelerate again by pressing "W".

Rotation could work this way:

 // class variables
 private Vector3 rotationDirection = new Vector3();
 private float roatationSpeed = 1.5f // can also change
  // ----------------------- //
 if(Input.GetKey(KeyCode.A)) {
     rotationDirection.y += rotationSpeed * Time.deltaTIme
 }
 if(Input.GetKey(KeyCode.D)) {
     rotationDirection.y -= rotationSpeed * Time.deltaTIme
 }
 
 transform.Rotate(rotationDirection);


This should also maintain the rotation direction until you "apply a force" in the opposite direction to stop.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

161 People are following this question.

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

Related Questions

Player Sliding without any Input 2 Answers

My player's movement is slippery 1 Answer

2D Movement and getting the mouse position 0 Answers

Please Check the code and tell me why this isn't working?! 1 Answer

Disabling script while in certain position 1 Answer


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