Search Unity

Throwing Trajectory Problem

Discussion in 'Scripting' started by giraffe1, Jul 2, 2021.

  1. giraffe1

    giraffe1

    Joined:
    Nov 1, 2014
    Posts:
    302
    Hi,

    I am having problems modifying this code to get the underhand throw to be less curvey. I want it to arch less and be thrown more like a straight line with a slight downward curve. The current code works great for the overhand throw but not realistic for the underhand throw. I made a video showing the problem.



    I am using the code from: https://answers.unity.com/questions/886580/throw-grenade-with-definite-speed-and-on-definite.html

    Code (CSharp):
    1. public void Launch(ThrowType type, Vector3 dir, float dist)
    2.     {
    3.         float hSpeed = 10f;
    4.  
    5.         float gravity = Physics.gravity.magnitude;
    6.         float totalTime = dist / hSpeed;
    7.         float vSpeed = (totalTime * gravity) / 2;
    8.         Vector3 force = new Vector3(dir.x * hSpeed, vSpeed, dir.z * hSpeed);
    9.  
    10.         rigid.velocity = force;
    11.  
    12.         Debug.DrawRay(transform.position, dir, Color.red, 3f);
    13.         Debug.DrawRay(transform.position, force, Color.green, 3f);
    14.     }
    I tried to modify the gravity/vertical speed/horizontal values manually but I did not work and was way off from the target. Maybe I need to use a different formula? I just want to get rid of the arch in the trajectory to make it match the animation more.

    Any help appreciated, thanks.
     
    Last edited: Jul 2, 2021
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    Just increase hSpeed. Simple physics. Faster you throw it the less high it needs to go to reach destination.
     
  3. giraffe1

    giraffe1

    Joined:
    Nov 1, 2014
    Posts:
    302
    When I do that; the grenade does fly straighter but it also flies super far away from the target. I added some debug lines to show the direction vector and force vector.
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    Well, what you want isn't really how physics works in real life, so I don't know what you want to hear, you're going to have to give up something.