Increase speed of an Instantiated Object that uses rb.addForce

Can anyone help me, by explaining how can i change the speed of an object that is instantiated and already moving?

The variable “ballSpeed” is used at the initial movement and change it does not affect the speed of the instantiated ball.

How can i find what is making the instantiated object move and change its value?

I tryed to use this “InvokeRepeating” but the “rb.AddForce(ballSpeed*1.5f, ballSpeed * 1.5f, 0,ForceMode.Force);” makes the ball act strange and changing its direction.

I read a lot of posts in the forum and tryed some solutions but i couldnt make it work.

Thanks in advance!

code:

    private Rigidbody rb;
    public bool BallInPlay;
    public float ballSpeed;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();

       // InvokeRepeating("BallSpeedIncrease", 2f, 2f);
       
    }

    void Update ()
    {
        if (Input.GetButtonDown("Fire1") && BallInPlay == false)
        {                    
            transform.parent = null;
            BallInPlay = true;
            rb.isKinematic = false;
            rb.AddForce (ballSpeed, ballSpeed, 0, ForceMode.Force ) ;           
        }
               
    }
     void BallSpeedIncrease()
    {
               
        if (BallInPlay == true)
       {
            rb.AddForce(ballSpeed*1.5f, ballSpeed * 1.5f, 0,ForceMode.Force);
        }
    }
}

You could try replacing this :

rb.AddForce(ballSpeed*1.5f, ballSpeed * 1.5f, 0,ForceMode.Force);

with this

rb.velocity += new Vector3 (ballSpeed * 1.5f, ballSpeed * 1.5f,0);

Let me know if this works or not

Thanks for the repply!

The ball still changes its course every time the InvokeRepeating acts.

I had to reduce the values to 0.01f because the ball was desappearing with the high speed:

rb.velocity += new Vector3(ballSpeed * 0.01f, ballSpeed * 0.01f, 0);

Thanks

Is there anothe way i can move this ball after instantiated? Still using physics.

AddForce should be called in FixedUpdate, not Update, but Input.GetButtonDown shall be called in Update. So you need somehow to setup a state machine to code it properly.

Look at this video based on Tanks! tutorial setting up a state machine with the shell firing:

http://game-rules.net/manual/tutorials.html#firing-shells


Game Rules
Unified Visual Scripting Asset with rules engine

1 Like

Thank you! Ill have a look.

Thanks everybody for the help!

I solve this problem using a if statement to add force depending of the direction.

The ball changes its direction ( using physics) every time it hits a rigidbody…

So if the ball if going to the right it has a positive speed (+X).

on the other hand, if it is going to the left it has a negative speed (-X).

Same Logic with Up and Down (Y)

I will place the code here in case someone had the same question:

 void BallSpeedIncrease()
    {
       
        if (rb.velocity.x > 0)
        {
            rb.AddForce(newBallSpeedx, 0, 0, ForceMode.VelocityChange);           
        }

        if (rb.velocity.x < 0)
        {
            rb.AddForce(newBallSpeedx * -1, 0, 0, ForceMode.VelocityChange);           
        }
        if (rb.velocity.y > 0)
        {
            rb.AddForce(0, newBallSpeedy, 0, ForceMode.VelocityChange);           
        }

        if (rb.velocity.y < 0)
        {
            rb.AddForce(0, newBallSpeedy * -1, 0, ForceMode.VelocityChange);           
        }
    }