Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Increase speed of an Instantiated Object that uses rb.addForce

Discussion in 'Scripting' started by Berthil, Sep 4, 2017.

  1. Berthil

    Berthil

    Joined:
    Aug 26, 2013
    Posts:
    18
    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:

    Code (CSharp):
    1.  
    2.    
    3.     private Rigidbody rb;
    4.     public bool BallInPlay;
    5.     public float ballSpeed;
    6.  
    7.     private void Start()
    8.     {
    9.         rb = GetComponent<Rigidbody>();
    10.  
    11.        // InvokeRepeating("BallSpeedIncrease", 2f, 2f);
    12.        
    13.     }
    14.  
    15.     void Update ()
    16.     {
    17.         if (Input.GetButtonDown("Fire1") && BallInPlay == false)
    18.         {                    
    19.             transform.parent = null;
    20.             BallInPlay = true;
    21.             rb.isKinematic = false;
    22.             rb.AddForce (ballSpeed, ballSpeed, 0, ForceMode.Force ) ;          
    23.         }
    24.                
    25.     }
    26.      void BallSpeedIncrease()
    27.     {
    28.                
    29.         if (BallInPlay == true)
    30.        {
    31.             rb.AddForce(ballSpeed*1.5f, ballSpeed * 1.5f, 0,ForceMode.Force);
    32.         }
    33.     }
    34. }
     
  2. Sinci1

    Sinci1

    Joined:
    Feb 18, 2016
    Posts:
    53
    You could try replacing this :
    Code (CSharp):
    1. rb.AddForce(ballSpeed*1.5f, ballSpeed * 1.5f, 0,ForceMode.Force);
    with this
    Code (CSharp):
    1. rb.velocity += new Vector3 (ballSpeed * 1.5f, ballSpeed * 1.5f,0);
    Let me know if this works or not
     
  3. Berthil

    Berthil

    Joined:
    Aug 26, 2013
    Posts:
    18
    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:

    Code (CSharp):
    1. rb.velocity += new Vector3(ballSpeed * 0.01f, ballSpeed * 0.01f, 0);
    Thanks
     
  4. Berthil

    Berthil

    Joined:
    Aug 26, 2013
    Posts:
    18
    Is there anothe way i can move this ball after instantiated? Still using physics.
     
  5. game-rules

    game-rules

    Joined:
    Jan 11, 2014
    Posts:
    45
    sdutter likes this.
  6. Berthil

    Berthil

    Joined:
    Aug 26, 2013
    Posts:
    18
    Thank you! Ill have a look.
     
  7. Berthil

    Berthil

    Joined:
    Aug 26, 2013
    Posts:
    18
    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:

    Code (CSharp):
    1.  void BallSpeedIncrease()
    2.     {
    3.        
    4.         if (rb.velocity.x > 0)
    5.         {
    6.             rb.AddForce(newBallSpeedx, 0, 0, ForceMode.VelocityChange);          
    7.         }
    8.  
    9.         if (rb.velocity.x < 0)
    10.         {
    11.             rb.AddForce(newBallSpeedx * -1, 0, 0, ForceMode.VelocityChange);          
    12.         }
    13.         if (rb.velocity.y > 0)
    14.         {
    15.             rb.AddForce(0, newBallSpeedy, 0, ForceMode.VelocityChange);          
    16.         }
    17.  
    18.         if (rb.velocity.y < 0)
    19.         {
    20.             rb.AddForce(0, newBallSpeedy * -1, 0, ForceMode.VelocityChange);          
    21.         }
    22.     }
     
unityunity