- Home /
Movment Script
I have just made a Movement script For a ball game but i can only go forward in one movement so when I move the camera The ball goes sideways. Also I want my ball to be able to jump but it flys when you hold space? how can i fix that in the script
#pragma strict
function Start () {
}
function Update () {
rigidbody.AddForce(Input.GetAxis("Horizontal")*10,
Input.GetAxis("Jump")*25,
Input.GetAxis("Vertical")*10);
}
You have no variable declarations or if statements so your script has no logic that needs to be carried out.First try declaring variables and using them in your code.It is the convention and allows for easier script altering for yourself and anyone else if needed. Second you need to put your Input statements into an if statement.Try something like this:
If(Input.GetButtonDown("Jump"))
{
rigidbody.AddForce(0,jumpVel,0);
}
What ever the logic you want to be carried out will be done inside the if statement.
Also try watching EteeskiTutorials FPS1.5 if you can't get this yourself as he goes over jumping and how it works very well.
Yes gamerant is correct, you need to have if statements, function Update () happens at least once per frame. SO every frame that code is run, this means the force is added to the ball every frame of every second. Thats why you need logic like if statements to make that one line of code only happen once PER spacebar press.
I assume you mean how to add logic to a script. Well if you are new to scripting I suggest and would strongly urge you to look at tutorial videos here on unity it self http://unity3d.com/learn/tutorials/modules or simply go on youtube.There are ton of great tutorials there.
But to answer your question,take a look at the example I gave you above.It says that if you press the jump button then a force is added to your rigid body along the y axis.This simply means that only when the button is pressed does the object in the game jump(or move up).
Inside the if brackets if() is where you put what you want your game to do.So in this case you are saying that you want the object to jump if the jump button is pressed down.Then inside {} these brackets is where your game logic goes.It's what you tell the program to do when the button is pressed.In this case you are telling it to add force to the rigid body.The brackets after AddForce tells your game which direction to add the force in.`(0,jumpVel,0)` simply says that there is no force added to the object along the x and the z axis since they are 0.But a force is added to the object along the y axis. JumpVel is the declare variable with which you can control how much force is added along the y. If it's small then a small amount of force is added. To declare this you would simply put var jumpVel : float; outside all of your functions.What this does is add the declared variable to the in game inspector which allows you to tweak the value of the force without having to go back to your script all the time. The float means that your variable can have decimal points and gives a much greater accuracy and control for when you try to find a nicely working value for jumping.
Oh and quickly: GetButtonDown means that when the button is pressed you will jump once.If you have GetButton your object would move up along the y axis as long as you have the jump button pressed.
Your answer