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

Cannonball physics

Discussion in 'Getting Started' started by OhnoNono, Jul 12, 2017.

  1. OhnoNono

    OhnoNono

    Joined:
    Jul 12, 2017
    Posts:
    1
    Hey Guys,

    I'm working on projectile physics for a cannonball/ Tank projectile.

    I want the projetile to somehow travel a great distance without having to travel at a very high speed.

    I have tried altering the projectile gravity, but when my projectile hits another object, it bounces off and keep traveling at high speed.

    Code (CSharp):
    1.  
    2. public class BulletPhysics : MonoBehaviour
    3. {
    4.     public float bulletGravity = 1f;
    5.     Rigidbody rb;
    6.  
    7.     public void Awake()
    8.     {
    9.         rb = GetComponent<Rigidbody>();
    10.     }
    11.  
    12.     private void Update()
    13.     {
    14.         rb.AddForce(new Vector3(0, bulletGravity, 0), ForceMode.Acceleration);
    15.     }  
    16. }
    17.  
    18.  
    The projectile speed is altered in another script, that i am applying to a shotspawner.

    Code (CSharp):
    1.  
    2. public class Shot : MonoBehaviour {
    3.  
    4.     public GameObject projectile;
    5.     public float projectileSpeed = 10;
    6.  
    7.  
    8.     // Use this for initialization
    9.     void Start ()
    10.     {
    11.          
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update ()
    16.     {
    17.         if (Input.GetButtonDown("Fire1"))
    18.         {
    19.             Fire();
    20.         }
    21.     }
    22.  
    23.     void Fire()
    24.     {
    25.         GameObject bullet = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
    26.         bullet.GetComponent<Rigidbody>().velocity = projectileSpeed * transform.forward;
    27.  
    28.  
    29.     }
    30. }
    31.  
    I hope you can help me find a way, to get the physics just right.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Ummm

    Distane = Speed * Time. By definition. So if you want the object to travel a large distance, you must have a large speed or a large time. There are no other ways to approach this.

    Perhaps a better description of the problem will help?
     
  3. mechaniqe

    mechaniqe

    Joined:
    Jan 25, 2017
    Posts:
    18
    What do you normally expect your cannon do when it hits another object? Explode? Or perhaps just stop?