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 jesser1981 · Mar 30, 2014 at 08:15 PM · addforcespeed

Flight Sim Control

Hi everyone, I'm trying to script basic flight controls for a flight sim I would like to make. I can't seem to get the object to move forward. I want the player to be able to control the speed using the keypad + and - keys. Making the speed increase or decrease and stop moving. This is the script I have, I'm not getting any errors but it just doesn't seem to work at all.

Any help would be greatly appreciate or even suggestions to make it better.

 using UnityEngine;
 using System.Collections;
 
 public class PlayController : MonoBehaviour 
 {
     //Variables for speed control
     public float curSpeed;
     public float maxSpeed = 1200;
     public float minSpeed = 0;
 
 
 
     void Start () 
     {
         CurSpeed ();
     }
     
 
     void FixedUpdate () 
     {
         if (Input.GetKeyDown (KeyCode.KeypadPlus)) 
         {
             rigidbody.AddForce (Vector3.forward * curSpeed);
         }
         if (Input.GetKeyDown (KeyCode.KeypadMinus)) 
         {
             rigidbody.AddForce (Vector3.forward * curSpeed);
         }
 
         transform.Rotate (Input.GetAxis("Vertical"), 0.0f,

-Input.GetAxis("Horizontal")); }

     void CurSpeed()
     {
          float incSpeed = 5;
          float decSpeed = 5;
 
         if (curSpeed > 0 && curSpeed < maxSpeed) 
         {
             curSpeed += incSpeed;
         } 
         else if (curSpeed == maxSpeed) 
         {
             curSpeed = maxSpeed;
         }
 
         if (curSpeed <= maxSpeed && curSpeed > minSpeed) 
         {
             curSpeed -= decSpeed;        
         }
         else if (curSpeed == minSpeed)
         {
             curSpeed = minSpeed;
         }
     }
 }
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
0

Answer by getyour411 · Mar 30, 2014 at 08:21 PM

CurSpeed method is only called once in the lifetime of this class (at init); you probably meant to include a call to it in each of the if(keypress) bits.

Using GetKeyDown means the player has to taptaptap, if that's what you want then no changes needed, if you wanted a 'while holding button down' just cahnge that to GetKey

Comment
Add comment · Show 4 · 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 jesser1981 · Mar 30, 2014 at 08:41 PM 0
Share

Nothing is happening, it's still staying in one spot and not moving forward at all.

avatar image jesser1981 · Mar 30, 2014 at 08:42 PM 0
Share

I changed it to just input.getkey

avatar image getyour411 · Mar 31, 2014 at 10:35 PM 0
Share

You didn't address the CurSpeed() issue

avatar image william9518 · Mar 31, 2014 at 10:42 PM 0
Share

Like what getyour411 said, you have to put the CurSpeed() function inside an update function, such as void Update().

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

21 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

Related Questions

AddForce() and limiting speed due to friction. 1 Answer

How to limit the force from player input without limiting speed 2 Answers

i am not able to increase the speed of ball perodically in pong, in this code the speed of ball decrease while playing so help with this, how to increase speed of ball perodically?, 0 Answers

Rigidbody.AddForce not working 1 Answer

How to set the velocity in Z direction to 0? 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