x


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!

more ▼

asked Nov 10 '11 at 07:42 AM

MobinS gravatar image

MobinS
56 23 32 36

any one?????

Nov 11 '11 at 04:31 PM MobinS

Mobin,

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.

Nov 11 '11 at 06:57 PM Julien.Lynge
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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

more ▼

answered Nov 11 '11 at 09:02 PM

jahroy gravatar image

jahroy
3.2k 14 18 41

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x953
x379
x181
x38
x34

asked: Nov 10 '11 at 07:42 AM

Seen: 759 times

Last Updated: Nov 11 '11 at 09:05 PM