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 /
  • Help Room /
avatar image
0
Question by TBC_Gyde · Dec 11, 2016 at 08:25 AM · gameflyingsimulator

Flight Sim help

I was wondering if there is a tutorial on creating something like this: https://www.reddit.com/r/Unity3D/comments/5gamip/tried_my_hand_at_making_a_space_plane_controller/

I don't really care about the model, I just wanna know how to code the speed, smooth turning and using your mouse to control the plane.

If anyone could send me a tutorial or teach me that, it would be awsome. Thanks for reading!

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 Konomira · Dec 12, 2016 at 12:50 AM

 using UnityEngine;
 using System.Collections;
 
 
 public class FlightControls : MonoBehaviour {
 
     public KeyCode
         accelerate = KeyCode.LeftShift,     //  Key used to accelerate
         decelerate = KeyCode.LeftControl;   //  Key used to decelerate
 
     public float
         maxSpeed = 1000000, //  Maximum speed
         minSpeed = 0;       //  Minimum speed
 
     public Camera 
         playerView;     //  The main camera
 
     GameObject cameraTarget;    //  The point that the camera looks at (just above the plane)
 
     float
         currentSpeed = 0;   //  The current speed
     
     void Start()
     {
         cameraTarget = new GameObject();    //  Creates a new pivot point
         playerView = Camera.main;   //  Sets the current camera
         Cursor.visible = false; //  Hides the cursor
 
         if(GetComponent<Rigidbody>() == null)   //  If there is no rigidbody
         {
             gameObject.AddComponent<Rigidbody>();   //  Create a rigidbody
             GetComponent<Rigidbody>().useGravity = false;   //  Don't use gravity
         }
 
         cameraTarget.transform.parent = transform;  //  Sets the pivot point as a child to the plane
     }
 
     void Update()
     {
         Controls();
         CameraControls();
     }
 
     void Controls()
     {
         if(Input.GetKey(accelerate) && currentSpeed < maxSpeed) //  If the accelerate key is pressed and the plane can accelerate
         {
             currentSpeed++; //  Accelerate
         }
 
         if (Input.GetKey(decelerate) && currentSpeed > minSpeed)    //  If the decelerate key is pressed and the plane can decelerate
         {
             currentSpeed--; //  Decelerate
         }
 
         transform.Rotate(new Vector3(Input.GetAxis("Mouse Y"), Input.GetAxis("Horizontal"), -Input.GetAxis("Mouse X")));
         /*
          * Rotation breakdown:
          *  - X rotation is the pitch   (up/down)
          *  - Y rotation is the yaw     (left/right)
          *  - Z rotation is the roll    (tilt left/right)
          */
 
         GetComponent<Rigidbody>().velocity = transform.forward * currentSpeed; //   Moves the plane forward at the current speed
     }
 
     void CameraControls()
     {
         playerView.transform.LookAt(cameraTarget.transform);    //  Looks at the pivot point
         cameraTarget.transform.position = transform.position + (transform.up * 2);  //  Sets the pivot point 2 units above the plane
         playerView.transform.position = transform.position - (transform.forward * 10) + (transform.up * 2); //  Sets the camera 10 units behind and 2 units above the plane
     }
 }

Attach this script to a cube of size (3, 0.3, 3) and you have yourself a primitive flying object

Comment
Add comment · Show 3 · 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 TBC_Gyde · Dec 12, 2016 at 06:45 AM 0
Share

Thank you for the help, but I'm getting 2 errors

  1. The referenced script on this Behavior is missing!

  2. The referenced script on this Behavior (GameCube) is missing!

avatar image Konomira TBC_Gyde · Dec 13, 2016 at 10:48 AM 0
Share

$$anonymous$$ake sure the script is called FlightControls.cs

avatar image TBC_Gyde Konomira · Dec 14, 2016 at 10:08 AM 0
Share

Gee thanks! It's working now. Time to make textures and models :D

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

109 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

Related Questions

Why does my platform move when I press my directional keys when it does not have the PlayerController script? 1 Answer

My project's UI keeps resizing itself and I have no idea why. 0 Answers

How to rotate a Pivot with a front facing Player with a mouse?,Does anyone know the code for front pivot movement using mouse to aim? 1 Answer

Jacks game Simulator 0 Answers

Polymorphic array inside ScriptableObject,Polymorphic array in ScriptableObject 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