- Home /
changing a float number by moving arrows (horizontal and vertical input)?
Hi I made a bullet and put this on prefab out of game. and I made a constant force for it and I put "10" for X force. now when I shoot it go forward in X line. and when I put X "9" and then put Y "1" the ball shot a little farther near Y line.. and when I continued this ( like this : X=10 Y=0 , X=9 Y=1 , X=8 Y=2 , X=7 Y=3 ... X=0 Y=10 ) it will change the direction of it.. and for other side I put -9 and -1 ... i work perfect and I put the numbers in float. when I change them like this Manually the work great. but I want that for example when I hold 'W' automatically set the X to 10 and Y to 0... when I hold 'W' and 'D' it automatically set X to 5 and Y to 5 too. I mean how should I change a float number using horizontal and vertical input the we use for moving around? thnx!
$$anonymous$$obin,
I really can't understand what you're saying. Please try the following:
Use complete sentences, and don't write as if you would speak (it comes across as a stream of consciousness and is very hard to parse)
Use proper capitalization, punctuation, etc.
Look at your question from the perspective of someone who isn't you. You seem to make a lot of assumptions that I know what's going on with your game. Perhaps some screenshots, or more explanation, would help.
Also, remember that politeness goes a long way here. Acting indignant when your question isn't answered immediately won't get you any sympathy.
Answer by jahroy · Nov 11, 2011 at 09:02 PM
To literally do what you describe, you could use code like this:
function Update () { if ( Input.GetKey(KeyCode.W) ) { X = 10; Y = 0; }
if ( Input.GetKey(KeyCode.D) ) {
X = 5;
Y = 5;
}
}
However, as you suggest by saying using horizontal and vertical input, there are better ways to do it...
You could adjust X and Y based on Horizontal and Vertical input like this:
/* a variable used to control the speed */
var speedFactor : float = 0.002;
function Update () { if ( Input.GetAxis("Horizontal") != 0 ) { X += Input.GetAxis("Horizontal") * speedFactor; }
if ( Input.GetAxis("Vertical") != 0 ) {
Y += Input.GetAxis("Vertical") * speedFactor;
}
}
The Horizontal and Vertical axes are mapped to the WASD keys, the arroy keys, and some other stuff. You can play with the input settings in the Inspector by choosing Edit -> Project Settings -> Input.
I recommend checking out the Input documentation and the camera control scripts that that come in the Standard Assets/Scripts folder.
There are also tons of questions and answers on this site about how to do stuff with character movement input (like clamp the values, smooth the motion, lerp the camera, etc...).
Hope this helps...
Your answer
Follow this Question
Related Questions
Adapting Horizontal and Vertical movement to New Input System? 0 Answers
How can I make GUI button send an input message when I press it? 0 Answers
How to have vertical and horizontal axis work at the same time 1 Answer
Change a float smooth 2 Answers
Player Moving forward and backward, instead of side-to-side 2 Answers