Search code examples
unity-game-enginegravityrigid-bodies

How can I add Force to my object with gravity so that It can travel to Long Distance?


I am new In Unity and I am Developing one Simple game where Plates are Initiate from One place and It will Destroy on click. I want to show my plate flying from one place to another place with Gravity.

My code for Plate Initiate is Below..

void Update () {

    InitTimer -= Time.deltaTime;
    print("InitTimer" +InitTimer);
    if(InitTimer <= 0){
        InitTimer = Random.Range(1f, 2f);
        Instantiate(plate,new Vector3 (range,transform.position.y,transform.position.z), Quaternion.identity);
    }

}

Can Anyone give me any reference code or any guidance?

I want to fly my every plate In one Direction.

Thank You In Advance..


Solution

  • Instantiate the plate to a GameObject then AddForce to it:

    GameObject plate = Instantiate(plate,new Vector3 (range,transform.position.y,
                       transform.position.z), Quaternion.identity) as GameObject;
    
    // example of adding force in x axis, change the vector3 however you want
    plate.rigidbody.AddForce(new Vector3(1f, 0f, 0f), ForceMode.Impulse);
    

    Don't forget to add a rigidbody to your prefab.