# Ball Speed is not increasing as per code

I am working on small project , but now I have a problem . The problem is The Player speed (Ball speed) is not increasing as per the code .

In the beginning ball speed increases , but as the time goes the speed becomes constant .

I made Gravity as -50 in Y - Axis .

So here is the C# Script attached to the ball :-

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Ball_Script : MonoBehaviour
{

// Ball Physics Variables
private Rigidbody Ball_Rigid;
public float Ball_Speed = 10f;
private float Speed_Change_Time = 1.0f ;

// UI Text
public Text My_Ball_Speed_Text ;
public Text My_Key ;

// Use this for initialization
void Start ()
{
Ball_Rigid = GetComponent<Rigidbody>();
My_Ball_Speed_Text.text = Ball_Speed.ToString () + " km/h";
My_Key.text = "No Key Pressed";
}

// Update is called once per frame
void Update ()
{
// Restart game
if(transform.position.y < -7.0f)
{
}

// Ball Automatic Movement script Starts here
Speed_Change_Time_Interval ();
Ball_Rigid.AddForce (new Vector3 (Ball_Speed * -1 * Time.deltaTime, 0, 0));
// Ball Automatic Movement script Ends here

if(Input.GetKeyDown(KeyCode.LeftArrow))
{
Ball_Rigid.MovePosition(new Vector3(transform.position.x , transform.position.y , transform.position.z - 1.0f));
My_Key.text = "Left Arrow Pressed";
}

if(Input.GetKeyDown(KeyCode.RightArrow))
{
Ball_Rigid.MovePosition(new Vector3(transform.position.x , transform.position.y , transform.position.z + 1.0f));
My_Key.text = "Right Arrow Pressed";
}
}

// This Fixed update stops the ball from bouncing
void FixedUpdate()
{

Vector3 currentVelocity = Ball_Rigid.velocity;

if (currentVelocity.y <= 0f)
return;

currentVelocity.y = 0f;

Ball_Rigid.velocity = currentVelocity;
}

// This function increases the ball speed in every 1 second
void Speed_Change_Time_Interval ()
{
Speed_Change_Time = Speed_Change_Time - Time.deltaTime;

if(Speed_Change_Time <= 0)
{
Ball_Speed = Ball_Speed + 10;
Speed_Change_Time = 1.0f;
print(Ball_Speed);
My_Ball_Speed_Text.text = Ball_Speed.ToString ()+ " km/h";
}
}
}


Here FixedUpdate () stops the ball from bouncing and Speed_Change_Time_Interval () function increases the ball speed in every second . But it works some extent . Then there is no change in speed of the ball . Here is the video of the game ( Recorded with Jing , so file is in .swf format . Sorry for that :( ) Link :- http://www.mediafire.com/download/ibk4ffjm3y6i96y/Ball_Vid.swf

File size = 9.28 MB

So what is the problem of this script . If my method is wrong / not good please suggest me a good one :).

Hope you will help me to figure it out .

Thanks , Regards

NB :)

• It's recommended to only call RigidBody methods like AddForce from FixedUpdate – lase Jan 12 '16 at 20:24
• OK , Thanks for the reply , but why the speed is not affecting the ball ? – Nicolus Buck Jan 12 '16 at 20:25