Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Why are my balls sagging upwards?

Discussion in 'Scripting' started by warrenbrandt, Apr 11, 2023.

  1. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    Hi guys,

    I am trying to shoot balls in random directions at a constant speed.
    But when they instantiate they move along but then float upwards a bit in an arc.
    I have no gravity enabled on the Rigidbody2D and i also turned off gravity in project settings.
    Cant work it out. Here is my script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GreenBall : MonoBehaviour
    6. {
    7.     public Rigidbody2D rb;
    8.  
    9.     private void FixedUpdate()
    10.     {
    11.         rb.AddForce(transform.up * 5);
    12.         rb.AddForce(transform.right * 5);
    13.  
    14.     }
    15.  
    16.    
    17.  
    18.     private void OnCollisionEnter2D(Collision2D collision)
    19.     {
    20.         if (collision.gameObject.tag == "GreenBall")
    21.         {
    22.             Destroy(collision.gameObject);
    23.             Destroy(gameObject);
    24.         }
    25.     }
    26. }
    27.  
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,398
    Great title lmao

    If you just want your balls to go in 1 direction add force once and disable drag. Otherwise I suggest changing the velocity manually
     
    mopthrow, Elhimp, Kreshi and 4 others like this.
  3. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    Oh, my word....honestly, it didnt even occur to me! :) the title i mean. Thanks for the help!

    Drag is off, and i want it to go straight, but start off going in different directions.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,758
    Your FixedUpdate() code is constantly pushing this ball up and to the right... but that is up and to the right RELATIVE to the Transform itself because you used Transform.right (which is Local), not Vector3.right (which is World)

    Works the same as long as your ball isn't spinning... but if it spins, you may lose control of your balls. Nobody wants you to do that. You should control your balls as best you can... it's only... professional.
     
    mopthrow, warrenbrandt and DevDunk like this.
  5. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    637
    lol - great thread.
     
    Kurt-Dekker, koirat and DevDunk like this.
  6. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    Got it sorted guys thanks! My balls are perfectly under control....