Search Unity

Arc trajectory, with multiple arc side options

Discussion in 'Physics' started by Isheta, Sep 11, 2019.

  1. Isheta


    Aug 9, 2019

    So i want to build an arc trajectory without using rigidbody.
    I have 3 possible sides: Up, Left, Right.

    i have 2 more variables which are called
    float ArcPeakToDistanceRatio and float ArcPeakDistanceFromLineRatio

    So lets assume we're using Up side, an easy way to visualize this is a mortar shot.
    We show a projectile from point A to point B.
    The 'ArcPeakToDistanceRatio' will determine how close the peak will be the starting point.
    The 'ArcPeakDistanceFromLineRatio' will determine how far the peak point will be from a straight line between the points.

    so assume:
    distance = Vector3.Distance(pointA, pointB)
    PP = ArcPeakToDistanceRatio * distance
    H= ArcPeakDistanceFromLineRatio * distance

    Then in 2d, i only need to calculate the degree which is tan(pd/H) and then the hypotenuse by doing Sqrt(H^2 + PP^2).

    The problem is when i need to calculate this in 3d and on other sides.
    Im new to Unity so I think I might be missing tools or examples to how to achieve this.

    So for example if I choose 'Left' side then the projectile should be flying from the left of point A towards point B.

    welp :)

    so i think i found the peak point (haven't tested yet).

    my code:
    Code (CSharp):
    1. private void CalculateAndSetPeakPosition()
    2.         {
    3.             // distance between target and source
    4.             float distance = Vector3.Distance(_startingPosition, _target);
    6.             // rotate the object to face the target
    7.             transform.LookAt(_target);
    10.             // We want to know what will be the peak point
    11.             float peakPartialDistance = distance * ArcPeakToDistanceRatio;
    12.             float peakDistanceFromLine = distance * ArcPeakDistanceFromLineRatio;
    14.             // General direction
    15.             Vector3 direction = _target - _startingPosition;
    16.             direction.Normalize();
    18.             // Distance of the peak relative to line distance
    19.             Vector3 peakAxisPosition = ArcPeakToDistanceRatio * direction;
    21.             Quaternion peakPositionRelativity;
    22.             switch (Side)
    23.             {
    24.                 case Enums.TrajectorySide.Up:
    25.                     peakPositionRelativity = Quaternion.AngleAxis(90f, Vector3.up);
    26.                     break;
    27.                 case Enums.TrajectorySide.Left:
    28.                     peakPositionRelativity = Quaternion.AngleAxis(90f, Vector3.left);
    29.                     break;
    30.                 case Enums.TrajectorySide.Right:
    31.                     peakPositionRelativity = Quaternion.AngleAxis(90f, Vector3.right);
    32.                     break;
    33.                 default:
    34.                     throw new Exception($"Invalid side: {Side}");
    35.             }
    37.             // After combining 2 vectors, one of the direction and one of the distance, we get the peak position
    38.             _peakPosition = peakPositionRelativity * direction + peakAxisPosition;
    39.         }
    Last edited: Sep 11, 2019