Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 AppleseedSmoothie · Jan 27, 2018 at 11:27 PM · scripting problemmovementairplane

Spaceship movement script?

I currently have basic movement system for my space which 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 spaceship/airplane movement mechanic? Preferably third person but first person ship controls are very similar to third 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 ship 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 'ship_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 (ship_control.js), just add it to your 'Player' object and then attach the camera as a child of the 'Player'. You can also add a 3D model of a spaceship as a child of the 'Player' object too.

 public var MousePosition = Input.mousePosition;
 public var position;
 public var curShipSpeed : 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_ship = 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);
 
     curShipSpeed = Mathf.Lerp(curShipSpeed, current_speed, Time.deltaTime * step);
     transform.position += transform.TransformDirection(Vector3.forward) * curShipSpeed * 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 ins$$anonymous$$d. Example:

 float acceleration = 1.5f; // change to what suits you best
 
 if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.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.Get$$anonymous$$ey($$anonymous$$eyCode.A)) {
     rotationDirection.y += rotationSpeed * Time.deltaTIme
 }
 if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.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.

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

How do I keep my character facing the direction of travel after movement stops? 1 Answer

Need help with my movement script.. :( 1 Answer

Question about moving an object technique 3 Answers

How to move the main camera in Google Cardboard? 0 Answers

Client Sprite cant be controlled when spawned by NetworkManager 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