- Home /
Question by
$$anonymous$$ · Jul 11, 2017 at 03:42 AM ·
c#movementsimplesimplemovewont
SpeedUp (Beginner project RollBall)
Hi, am doing the project RollBall and I want make my player who increase speed by 10.0f every time I press 'o' but it wont script:
public class PlayerMovements : MonoBehaviour {
public float speed;
public Rigidbody rb;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movements = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movements * speed * Time.deltaTime);
shift ();
}
public void shift (){
if (Input.GetKeyDown (KeyCode.O)) {
speed = speed + 10.0f;
} else {
speed = 400.0f;
}
}
}
Comment
Best Answer
Answer by shrey150 · Jul 11, 2017 at 03:58 AM
The problem is you're resetting your speed every time the key is not pressed back to 400.0f. Take out the else statement and just have:
if (Input.GetKeyDown(KeyCode.O)) {
speed += 10.0f;
}
and you should be fine. In addition, you can use +=
to do incremementation on a variable (`speed = speed + 10.0f` = speed += 10.0f
)
Your answer
![](https://koobas.hobune.stream/wayback/20220612125954im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to create 2D random movement? 1 Answer