Throw snowball from players vector position to clicked locatiom

Hi

I been trying to figure out how to do this in unity.

I tried simple rigid body and add force and it lobs but that just gets me a general direction projectile.

What I want to do is be able to click on the screen and throw the snowball from my moving players vector to that location clicked and have it go up and come back down at the location.

I can use camera screen point too and plot a 2nd vector to throw at but I can’t get the math correct to lob the snowball/rigid body.

Think I am missing something.

Raycasting

Figured it out, found a script in the unity answers that pointed me in the right direction.

If any one is interested

1 Like

i am, thank you for linking it. Have a like

Ok well, I tried the script out and for me it dont seem to do what I need but I am sure it would be helpful for someone trying to do some type of turret type system of trajectory.

However I am still having some issues so I am going back to the drawing board…

Here is what I am trying to do, incase anyone thinks of anything to give some input on this topic.
Throw a snowball from a moving player transform to the clicked spot.

Some notes:
I don’t care about y, in either player or destination position because I don’t have jumping characters.

So far, I can easily do the following…

    void ThrowSnowBall()
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);

        if (Physics.Raycast (ray, out hit, 1000, floorMask))
        {
            Vector3 destinationVector = new Vector3 (hit.point.x, 0f, hit.point.z);
            Vector3 sourceVector = new Vector3 (transform.position.x, 0f, transform.position.z);

            //draws a line exactly to where i click.
            Debug.DrawLine(sourceVector,destinationVector);


            GameObject gameobj;
            gameobj = Instantiate(SnowBall, transform.position, transform.rotation) as GameObject;
          
            UsedSnowBall used;
            used = gameobj.GetComponent<UsedSnowBall> () as UsedSnowBall;
            used.Damage = damagePerShot;
          
            Rigidbody rig;
            rig = gameobj.GetComponent<Rigidbody> ();

            //this is where I need to make some sort of calculation to add the correct force to lob to the detination.
            //rig.AddRelativeForce(rVector, ForceMode.Impulse); // add force on new axis
          
            Destroy (gameobj, 5f);
            timer = 0f;

        }

If you are not using gravity you can just apply a force in the direction to the target and use ForceMode.Impulse.

rigidbody.AddForce(destinationVector - sourceVector * power, ForceMode.Impulse);

If you want to use gravity on the snowball, so it falls in a curve like the real world, you will need to know how to calculate the needed speed based on gravity, angle and distancee. Newton will help you