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 JoshMBeyer · Jan 24, 2013 at 06:50 AM · errorfloatintcast

What is wrong with my throttle script?

I am creating a Aviation Controller. I had it all working great the other night and I got on this morning and the whole project was deleted somehow so I tried remembering what I did and I can't seem to figure out what I am doing wrong this time. (I've probably been spending too much time working on this game that I am blind to many mistakes so while I take a break I was wondering if anyone can tell me the answer to what I am doing wrong.

I know the answer is simple and I am just going about this the wrong way, but here is the part of the script that I am having trouble with.

 using UnityEngine;
 using System.Collections;
 
 /// <summary>
 /// Just to show the part I am having problems with.
 /// This is only a demo of a part of my Aviation Controller.
 /// 
 /// On this part what I am trying to do is create a throttle to a plane.
 /// When the user is holding down the Up/Down Arrows, smoothly increase the "throttleLevel".
 /// 
 /// At every throttleLevel it will set the Forward force of my aircraft a little higher. 
 /// (I have it all set up to allow editing in the inspector and it all works just fine.)
 /// 
 /// I just can't seem to make the arrows work. I am getting the error,
 /// 
 /// error CS0266: Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?)
 /// 
 /// I tried puting,
 /// throttleLevel += (int)1 * Time.deltaTime;
 /// throttleLevel += (float)1 * Time.deltaTime;
 /// (int)throttleLevel += 1 * Time.deltaTime;
 /// (float)throttleLevel += 1 * Time.deltaTime;
 /// 
 /// adding the "1.f" and "1.0" and "1.0f" I mean I was trying everything even what didnt make sense.
 /// 
 /// and also switching the variable from int to float. not that I thought any of this would work, but like I said I am
 /// very very... tired.. 
 /// 
 /// </summary>
 
 public class NewBehaviourScript : MonoBehaviour {
 
     
     //---------Variables Start--------
     
     public int throttleLevel;
     
     //---------Variables End----------
     
     
     
     // Update is called once per frame
     void Update () 
     {
     
         //----Creating our throttle---
     
         switch (throttleLevel)
         {
             case 1:                    //If throttleLevel = 1, throttle = 1. 
                    throttle = 1;
             break;
             case 2:
                 throttle = 2;        //If throttleLevel = 2, throttle = 2 and so on...
             break;
             case 3:
                 throttle = 3;
             break;
             case 4:
                 throttle = 4;
             break;
             case 5:
                 throttle = 5;
             break;
             case 6:
                 throttle = 6;
             break;
             case 7:
                 throttle = 7;
             break;
             case 8:
                 throttle = 8;
             break;
             case 9:
                 throttle = 9;
             break;
             case 10: 
                 throttle = 10;
             break;
         }
         
         //If the up arrow is being held, add 1 per second.
         if(Input.GetAxis("Vertical") >0) 
         {
             throttleLevel += 1 * Time.deltaTime;
         }
         
         //If the down arrow is being held, take away 1 per second.
         if(Input.GetAxis("Vertical") <0) 
         {
             throttleLevel -= 1 * Time.deltaTime;
         }
         
         //Set the max throttle level to 10.
         if(throttleLevel >= 10)    
         {
             throttleLevel = 10;
         }
         
         //Prevent the throttle level from falling below 0.
         if(throttleLevel <= 0)    
         {
             throttleLevel = 0;
         }
         
         //---------End----------
     }
 }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by fafase · Jan 24, 2013 at 07:56 AM

You could try:

 float throttle;
 float moderation = 0.5f;
 float decrease = 0.3f;

 void Update(){
     throttle +=Input.GetAxis("Vertical")*moderation;
     throttle-= decrease; // Automatic deceleration
     throttle = Mathf.Clamp(throttle,0,10);
     print (throttle);
 }

First off you do not need the all switch thing, as you can directly pass the value to throttle. Then, clamp does the last bit you are doing.

In my example, the throttle also goes down when the button is released (you may want to remove that part but that is just for the example). It just means if you release it decelerates, if you press down it brakes.

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 JoshMBeyer · Jan 24, 2013 at 04:42 PM 0
Share

That works fine, just how do I set the speed by the ammount of throttle? For example; If throttle equals 1, speed is increase to 1. If throttle equals 2 speed is increased to 20. and so on..

avatar image JoshMBeyer · Jan 24, 2013 at 04:43 PM 0
Share

$$anonymous$$eaning what type of statement should I use so I don't have to use 10 if statements in a row.

avatar image
1

Answer by JoshMBeyer · Jan 24, 2013 at 05:10 PM

Nevermind, I figured it out lol.

 throttle += speed += Input.GetAxis("Vertical")*moderation;
 throttle = Mathf.Clamp(throttle,0,20);
 speed = Mathf.Clamp(speed,0,100);
 print (throttle);


Thanks for the help.

Comment
Add comment · 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

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

10 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

Related Questions

Is There A Way To Cast On 1 Returning Value 2 Times? 5 Answers

IComparable error due to float 1 Answer

C# Int to Float conversion performance 3 Answers

Float to int casting in C# script with modulo giving division by zero error 1 Answer

Cannot implicitly convert type `float' to `int 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