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!
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...