Reputation: 115
I have a ball which need to accelerate during time, but give a very little force. I tried to accelerate with Time.deltatime divided with specific values but it doesn't work.
This is my sample of code (to start game, you need a one tap and ball is moving with constant speed) and my question is how can automatically accelerating a ball during time.
void Start ()
{
Application.targetFrameRate = 60;
gameStart = false;
}
void Update ()
{
if (!gameStart)
{
if (Input.touchCount >= 1)
{
gameStart = true;
GetComponent<Rigidbody2D> ().AddForce (new Vector2 (1f, 0.5f) * force);
}
}
}
Upvotes: 0
Views: 140
Reputation: 415
You can try this, I have not changed any variable names to maintain familiarity with your original code
Rigidbody2D myRigidbody
void Awake(){ myRigidbody = GetComponent<Rigidbody2D>(); }
void Start ()
{
Application.targetFrameRate = 60;
gameStart = false;
// Invokes the method setGameStartToFalse in 2 seconds, then repeatedly every 1 seconds.
InvokeRepeating("setGameStartToFalse", 2f, 1.0F);
}
void FixedUpdate ()
{
if (!gameStart)
{
if (Input.touchCount >= 1)
{
gameStart = true;
myRigidbody.AddForce(new Vector2 (1f, 0.5f) * force);
}
}
}
void setGameStartToFalse()
{
gameStart = false;
}
Upvotes: 1
Reputation: 7973
In Update()
method you set gamestart to true
so the application will not execute the code inside the first if
until you will set again gamestart to false
. You also must check that MonoBehaviour is enabled or Update will not execute
Upvotes: 0