scripting questions

Hello again. I was poking around and found a decent script for turning my spaceship. after playing with it i like it. now i just need to add a thrust componet to it. what i want is a script that detects when i use my mouse wheel or hit a button on my keyboard. at this point im not sure i understand how the engine understands how to interpret the key down buttons info with out declareing it but im sure ill figure it out. if some one could help me build one not just build it for me right away that would be awsome. ^^ i would really like to learn how to do this stuff on my own. ^^

Basically, you’d use something like:

if (Input.GetKey ("a")){
    //do something
}

See links below for more info.

well as that is helpfull its not what i ment.
thank you cause i wasnt sure how it registered that but what im talking about is such as example as this.

var speed = 150; 
var turnSpeed = 10;


function Update () { 
   if(Input.GetAxis("Horizontal") > 0.0){ //if the right arrow is pressed 
      transform.Rotate(0.0, turnSpeed * Time.deltaTime, -10.0 * Time.deltaTime, Space.World); //and then turn the plane 
   } 
   if(Input.GetAxis("Horizontal") < 0.0){ //if the left arrow is pressed 
      transform.Rotate(0.0, -turnSpeed * Time.deltaTime, 10.0 * Time.deltaTime, Space.World); //The X-rotation turns the plane - the Z-rotation tilts it 
   } 
   if(Input.GetAxis("Vertical") > 0.0){ //if the up arrow is pressed 
      transform.Rotate(20.0 * Time.deltaTime, 0.0, 0.0); 
   } 
   if(Input.GetAxis("Vertical") < 0.0){ //if the down arrow is pressed 
      transform.Rotate(-20.0 * Time.deltaTime, 0.0, 0.0); 
	  }
	}

it moves the ship around a center axis but doesnt have the GetKey stuff. this is what i meant about how does it register a key hit. but thank you as i can use what you gave me to get closer to what i need.

ok after tinkering around with szymon’s script and cutting out what i dont really need i have come up with a very small code.

var FlyingSpeed = 100; 


function Update () { 

//Forward flying speed 
if (Input.GetKey("e")){
rigidbody.velocity = transform.forward * Time.deltaTime * FlyingSpeed; 
}

}

as you can see its very small and very to the point.

allas however its not what i need exactly.

what i want to do is every time you hit the rotate the mouse wheel it adds to the velocity every time you rotate it the opposite way it subtracts. i dont know how to use the mousewheel in a command yet but maybe somebody does. thank you for your time.

To get mousewheel information, you treat it like an axis (as you do with the arrow keys, etc):

Input.GetAxis("Mouse ScrollWheel")

As a style point, the official line is that you should avoid GetKey() where possible in favor of defining buttons and axes in the Input Manager (Edit->Project Setings->Input)*. For the example you posted, you’d create a new entry and call it, say, “Thrust” and in your code, you’d replace

if (Input.GetKey("e")) {
    rigidbody.velocity = transform.forward * Time.deltaTime * FlyingSpeed;
}

with

if (Input.GetButton("Thrust")) {
    rigidbody.velocity = transform.forward * Time.deltaTime * FlyingSpeed;
}

Doing so has a number of benefits: itkeeps your code from breaking if you decide to remap your thrust key, allows the user to remap keys at runtime, and allows you to bind multiple keys to the same action without having a bunch of OR expressions in your input checks (ie, when using numpad 0 as an alternate thrust when using the arrow keys instead of WASD).

As you should see, “Mouse ScrollWheel” takes advantage of the axis feature of input so that instead of having to define “Thrust (Forward)” and “Thrust (Reverse),” you can just define “Thrust” and set positive and negative inputs for it which make it much easier to do the kind of thing you want.

*You can also rename “Mouse ScrollWheel” there if you so desired.

thank you now i understand my first question. how unity detects what key is hit.

now here is the new code.

function Update () { 
var FlyingSpeed = 0; 
//Forward flying speed 
if (Input.GetAxis("Thrust")) { 
var FlyingSpeed = new FlyingSpeed;

    
}
rigidbody.velocity = transform.forward * Time.deltaTime * FlyingSpeed; 
}

as you can tell its not compleate what want to do is add an int to the var FlyingSpeed. but I dont know the syntax well enough to know how to do it. I took java programming in colage but never really got that far. but what i remember i would add a ++ some where to tell the engine to add 1 to the speed. but this doesnt work. that and in space adding 1 wouldnt do much.

what i would like to do is define a max speed and add a % of that speed every time you scroll the mouse. but I dont know the language well enough to know how to do that yet.

could some one give me an example of how i would define the max speed then useing the scroll wheel increment a %. so say i have a max speed of 1000u/s and i want to incrment 10% of that every time i roll the mouse button either up or down to increase or decrease. i would be greatfull. thank you.

I’m not sure exactly what your intention here is, but I’ve annotated your code with some notes:

function Update () {
    var FlyingSpeed = 0;  //Update is called every frame. Setting FlyingSpeed to 0 every frame means you'll never increment.
    //Forward flying speed
    if (Input.GetAxis("Thrust")) {
    var FlyingSpeed = new FlyingSpeed;   //Is "FlyingSpeed" a class?  Do you mean "newFlyingSpeed?"
}

// The following code isn't in a method so I'm not sure where it's being called.  Did you edit something out before posting?
    rigidbody.velocity = transform.forward * Time.deltaTime * FlyingSpeed;
}

Input.GetAxis() returns a value between -1 and 1 every frame (where 0 is no input). Use this fact to your advantage (untested code):

var flyingSpeed : float = 0;
var minThrust : float = -10;
var maxThrust : float = 10;

function Update () {
    this.flyingSpeed = Mathf.Clamp(this.flyingSpeed + Input.GetAxis("Thrust"), minThrust, maxThrust); // Get the input, add it to flyingSpeed, and then clamp the value between min and max values.  Set flyingSpeed to the value returned.
    rigidbody.velocity = transform.forward * Time.deltaTime * flyingSpeed; //Apply velocity in Update()
}

no i didnt edit anything out i just didnt know what to do with it. that is the code that acctualy moves makes it move. or it was. thank you very much
and thanks for fixing my rather poor attempt at makeing a script. im still learning and this is helping immensly.

what im trying to do is make my ship move in the direction its pointing useing “thrust” which is applyed by scrolling the mouse wheel. this is just the first movement script im trying to accomplish. i have 4 others to work on. fps which is done already but will have to be modifed and a rts point and click style. and a fighter jet style, as well as a ground vehicle style movement. which may be broken between tracked and wheel based vehicles. but then i may also just make them rts style movement.

those are later though.right now im trying to nail down movement in smaller ships which is what this script is for. i only want the max speed public though. that way i can set the speed for differnt space crafts as i finish them and set them up.

right now this game is in concept im trying to figure out how and what works, as make models to populate my scenes what works im keeping what doesnt im tossing.

this is going to be rather large when im done but it will be alot of fun playing. if it works well and does well i may make it multi player.
but im getting ahead of myself. and im babbleing so again thank you for your help and thankyou very much for fixing and anoteing my script.

is this javascript? i ask cause after looking at it and trying it its not doing anything… but then i dont think i have the code to add or subtract from flyingspeed. still trying to figure that one out.

well now i know why it doesnt work. it does just not the way expected. instead of the mouse wheel its the mouse position. well back to the drawing board.

tho its kinda cool to control my speed by moveing the mouse lol.

ok fixed it but how to i increment it by a percentage of the max and min values?

var flyingSpeed : float = 0; 
var minThrust : float = -100; 
var maxThrust : float = 100; 

function Update () { 
    this.flyingSpeed = Mathf.Clamp(this.flyingSpeed + Input.GetAxis("Thrust"), 
	minThrust, maxThrust); // Get the input, add it to flyingSpeed, and then clamp the value between min and max values. 	Set flyingSpeed to the value returned. 
    
	rigidbody.velocity = transform.forward * Time.deltaTime * flyingSpeed; //Apply velocity in Update() 
}

i didnt change the values at all or any of the code for that matter. but how would i add and subtract a %each time the mouse wheel is moved?? thank you for your help.

ok after pokeing around on the scripting refrance and tweaking the script abit i have figured out a few things. but still have questions.

the new script.

var flyingSpeed : float=0;
var minThrust : float= -100; 
var maxThrust : float = 100; 
var Accel : int = 25;

function FixedUpdate () { 

if(Input.GetAxis("Thrust")){   
   this.flyingSpeed = Mathf.InversLerp(this.flyingSpeed + Accel, 
	maxThrust, minThrust);	// Get the input, add it to flyingSpeed, and then clamp the value between min and max values. 	Set flyingSpeed to the value returned. 
}

if("Thrust"){
	rigidbody.velocity = transform.forward * Time.deltaTime * flyingSpeed; //Apply velocity in Update() 
}
}

its doing exactly what i want it to do to a point. i can scroll up and down for speed but what happens is scrolling up sets the value strate to 100 and -100 depending on how i scroll it. what i want it to do is move in increments of 4% of the total i would like this value to be adjustable. could some one help me with this please.