- Home /
Ball moving much faster forward when there is horizontal input
For some reason, my ball moves a lot faster and increases speed much faster after horizontal input, If you dont move sideways at all, it moves at the rate it should. Here is my snippet of code(in the start function, forwardSpeed = 6)(The code below is in FixedUpdate):
forwardSpeed += Time.deltaTime * 0.02f;
#if !UNITY_ANDROID && !UNITY_IPHONE && !UNITY_BLACKBERRY && !UNITY_WINRT || UNITY_EDITOR
Vector3 editorMovement = new Vector3(Input.GetAxis("Horizontal"), 0.0f, 0.0f);
rigidbody.AddForce(new Vector3(editorMovement.x * Sidewaysspeed * Time.deltaTime, 0.0f, 0.0f));
#else
Vector3 movement = new Vector3 (Input.acceleration.x, 0.0f, 0.0f);
rigidbody.AddForce (new Vector3(movement.x * Sidewaysspeed * Time.deltaTime, 0.0f, 0.0f));
#endif
rigidbody.AddForce (new Vector3 (0.0f, 0.0f, forwardSpeed));
if you add a force every cycle that WILL make it go faster and faster. perhaps you want to add the force only once, rather than every cycle the horizontal input is active? and that last addforce, looks like it will ALWAYS add a force of 0,0,6.
Or, is the problem that is continues to accelerate even after you stop the the horizontal input?
Yup he is right, try to modify rigidbody.velocity ins$$anonymous$$d of forces. Also i believe that good practise is: if you need to add forces in different directions, to calculate final vector (by adding) and add force only once in the direction in between.