Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Throw snowball from players vector position to clicked locatiom

Discussion in 'Scripting' started by forbidden, Nov 19, 2014.

  1. forbidden

    forbidden

    Joined:
    Mar 6, 2013
    Posts:
    36
    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.
     
  2. herman111

    herman111

    Joined:
    May 11, 2014
    Posts:
    119
    Raycasting
     
  3. forbidden

    forbidden

    Joined:
    Mar 6, 2013
    Posts:
    36
    Nanako likes this.
  4. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    i am, thank you for linking it. Have a like
     
  5. forbidden

    forbidden

    Joined:
    Mar 6, 2013
    Posts:
    36
    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..

    Code (CSharp):
    1.     void ThrowSnowBall()
    2.     {
    3.         RaycastHit hit;
    4.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    5.  
    6.         if (Physics.Raycast (ray, out hit, 1000, floorMask))
    7.         {
    8.             Vector3 destinationVector = new Vector3 (hit.point.x, 0f, hit.point.z);
    9.             Vector3 sourceVector = new Vector3 (transform.position.x, 0f, transform.position.z);
    10.  
    11.             //draws a line exactly to where i click.
    12.             Debug.DrawLine(sourceVector,destinationVector);
    13.  
    14.  
    15.             GameObject gameobj;
    16.             gameobj = Instantiate(SnowBall, transform.position, transform.rotation) as GameObject;
    17.          
    18.             UsedSnowBall used;
    19.             used = gameobj.GetComponent<UsedSnowBall> () as UsedSnowBall;
    20.             used.Damage = damagePerShot;
    21.          
    22.             Rigidbody rig;
    23.             rig = gameobj.GetComponent<Rigidbody> ();
    24.  
    25.             //this is where I need to make some sort of calculation to add the correct force to lob to the detination.
    26.             //rig.AddRelativeForce(rVector, ForceMode.Impulse); // add force on new axis
    27.          
    28.             Destroy (gameobj, 5f);
    29.             timer = 0f;
    30.  
    31.         }
     
  6. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    If you are not using gravity you can just apply a force in the direction to the target and use ForceMode.Impulse.

    Code (CSharp):
    1. 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