- Home /
Add Force: Increase Speed of Ball periodically
Hi, I am making a 4 player pong game, and I want the ball to increase speed every 10 seconds. heres the code i have so far, im just not sure what to add to it to achieve this.
var temp : Vector3;
function Start()
{
gameObject.rigidbody.AddForce(500,0,500);
}
function OnCollisionEnter(col : Collision)
{
temp = gameObject.rigidbody.Velocity;
Debug.Log("X: " + temp.x + " Y: " + temp.y);
if(temp.x > 0)
{
gameObject.rigidbody.AddForce(-1000, 0, 0);
}
else if(temp.x <0)
{
gameObject.rigidbody.AddForce(1000, 0, 0);
}
if(temp.z > 0)
{
gameObject.rigidbody.AddForce(0, 0, -1000);
}
else if(temp.z <0)
{
gameObject.rigidbody.AddForce(0, 0, 1000);
}
}
function Update()
{
var temp : Vector3 = gameObject.rigidbody.Velocity;
if(temp.x > 0)
{
gameObject.rigidbody.AddForce(-1500, 0, 0);
}
else if(temp.x <0)
{
gameObject.rigidbody.AddForce(1500, 0, 0);
}
if(temp.z > 0)
{
gameObject.rigidbody.AddForce(0, 0, -1500);
}
else if(temp.z <0)
{
gameObject.rigidbody.AddForce(0, 0, 1500);
}
}
enter code here
well ins$$anonymous$$d of just using numbers i would create speed variables and have those add up over time.
Answer by syclamoth · Mar 08, 2012 at 04:27 AM
Instead of doing that super-convoluted thing there, why not just use something like this?
rigidbody.AddForce(rigidbody.velocity.normalized * forceAmount, ForceMode.Impulse);
This will add force based on the current velocity of the object. Otherwise, just rely on the physics engine for collisions and reflections.
by "super-convoluted" do u mean the code under function Update?
No, I mean all of it. The code you are using to increase the speed of the ball is overcomplicated and error-prone.
Answer by VIPINSIRWANI · Jul 12, 2013 at 12:04 PM
Set one Variable time and take Time.time after every 10 sec set it like
rigidbody.velocity = new Vector3(rigidbody.velocity.x speed, 0, rigidbody.velocity.z speed)
May Be it will work once try...
Answer by tdaniels · Mar 08, 2012 at 04:30 AM
If you're having issue with the time, set up a time variable and add ten seconds to it. Whenever Time.time is greater than your time variable, increase the velocity by whatever and reset your time variable by 10 seconds. That or do an ienumerator function.
If you're having issue with adding force I'd create a speed variable and use that to drive the movement. Someone else can probably give you more exact instructions for that.
Your answer
Follow this Question
Related Questions
Make an object move forward on its own 2 Answers
Help with player movement and adding force to a ball. 1 Answer
Limiting Speed on Two Axis 1 Answer