x


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
more ▼

asked Mar 08 '12 at 12:15 AM

Orggrim gravatar image

Orggrim
28 5 7 13

well instead of just using numbers i would create speed variables and have those add up over time.

Mar 08 '12 at 03:30 AM DGArtistsInc
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Mar 08 '12 at 04:30 AM

tdaniels gravatar image

tdaniels
16 1 1 1

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Mar 08 '12 at 04:27 AM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

by "super-convoluted" do u mean the code under function Update?

Mar 08 '12 at 10:04 AM Orggrim

No, I mean all of it. The code you are using to increase the speed of the ball is overcomplicated and error-prone.

Mar 28 '12 at 12:10 AM syclamoth
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x357
x292
x169
x86

asked: Mar 08 '12 at 12:15 AM

Seen: 971 times

Last Updated: Mar 28 '12 at 12:10 AM