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;
	}
	
}

}

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)

Thanks :slight_smile: