- Home /
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----------
}
}
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.
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..
$$anonymous$$eaning what type of statement should I use so I don't have to use 10 if statements in a row.
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.