0
\$\begingroup\$

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)
    {
        Application.LoadLevel(0);
    }

    // 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 :)

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

1 Answer 1

1
\$\begingroup\$

Your ball behaves similar to how a real ball would. You are applying an increasing force (which looks unusual to me) you call "speed". The drag of the Rigidbody dampens the effect of the force in a way that grows with speed, which means that there is a max speed called "terminal velocity": http://forum.unity3d.com/threads/terminal-velocity.34667/

Set your drag to zero and the speed will not be clamped to a maximum but this is unrealistic. In any case I don't get why your force (stop calling it "speed") cannot just be a constant.

What you are doing is made difficult by the fact that you are trying to be physics-based along the Y axis, kinematic along the X axis and... teleported along the Z axis. :-)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .