Search Unity

  1. How can our website serve you better? Give us your feedback. Take our survey and let us know.
    Dismiss Notice

(Solved) Parabolic Trajectory: Front to Back

Discussion in 'Scripting' started by cdalexander, Mar 15, 2010.

  1. cdalexander

    cdalexander

    Joined:
    Nov 9, 2009
    Posts:
    73
    [ post topic changed from: I have been Parabolically beaten and need assistance]


    I've posted a similar situation some time ago, but I can not use the same answer that solved it.

    Much like the original post linked below, I wish to draw a parabola trajectory line from any height to any height.
    Reference Link:
    http://forum.unity3d.com/viewtopic.php?t=2541

    I actually got all the properties that you would think is the hard part.

    My problem has been translating this into a vector3 relevant to originating position. Example...

    If I have a trajectory where in the middle of its flight, it will be 50m in the forward direction and 20m height, and I want to predict the world position of this spot prior to flight, how do I translate this to a Vector3?

    I've tried:
    Code (csharp):
    1.  
    2. // setting the initial offset
    3. var offSet = Vector3(0,20, 50);
    4.  
    5. // I've Tried:
    6. midPoint = transform.position + offset;
    7. midPoint = transform.TransformDirection (offSet)
    8. midPoint = transform.TransformDirection (transform.position + offSet)midPoint = transform.TransformDirection (transform.position * offSet)
    9. midPoint = transform.TransformPoint([as in TransformDirection])
    10.  
    I've also attached a UnityScript file that is basically what I've got with slight modifications to make sense to the reader.

    Now I can't figure out why TransformDirection or Point wouldn't work.. am I overlooking something?

    (PS - I know I could use a ray and find the position by referencing the distance in the ray... but I need to do this purely mathematically without using rays)

    Thanks for any input here...
     

    Attached Files:

  2. Tinus

    Tinus

    Joined:
    Apr 6, 2009
    Posts:
    436
    transform.position is in world space, while your offset vector is in local space. To add them together, you need to transform one to the same space as the other. This means you leave one of them be while transforming the other, not both of them.

    My guess is that you need something like this:

    Code (csharp):
    1. // Since offset is a point, not a direction, you don't have to add to transform.position:
    2. midPoint = transform.TransformPoint(offset);
    3.  
    4. // Using a direction, you do have to use transform.position as a starting point, then add the translation:
    5. midpoint = transform.position + transform.TransformDirection(offset);
    6.  
    Hope that helps. (And I hope it works! Otherwise: Oops)
     
  3. cdalexander

    cdalexander

    Joined:
    Nov 9, 2009
    Posts:
    73
    As soon as I saw your code I knew that that was it.

    I am aware of the world/local difference in what I had. I just couldn't get TransformPoint or TransformDirection to work with me so I was trying everything under the sun. I didn't even think to add position to the TransformDirection, which should have been a little obvious.

    Oh well... nice work, Tinus. This is the 3rd (?) time you've helped me immensely. Thanks!
     
  4. cdalexander

    cdalexander

    Joined:
    Nov 9, 2009
    Posts:
    73
    Just for convenience, I posted below attached the working code to this post in the event anyone wants to use it.

    Thanks again for all your help!.


    Code (csharp):
    1.  
    2.  
    3. ////////////////////////////////////////////////////////
    4. // First, we need to get our X-angle and
    5. // the velocity along the Y and Z axis
    6.    
    7. // PI
    8. var pi: float = Mathf.PI;
    9.    
    10. // Gravity - 9.81 meters per second
    11. var g: float = Physics.gravity.y; //-9.8
    12.    
    13. // Velocity
    14. var v: float = 100;
    15.    
    16. // Angle
    17. var theta: float = 45;
    18.    
    19. // Velocity along Y-axis
    20. var velocityY: float = v * Mathf.Sin(theta/180*pi);
    21.    
    22. // Velocity along Z-axis
    23. var velocityZ: float = v * Mathf.Cos(theta/180*pi);
    24.    
    25. // Done getting all the variables we need
    26. ////////////////////////////////////////////////////////
    27. // Now let's find out where everything is going to be at
    28. //
    29.    
    30. // How long will it take for the projectile to reach the highest/halfway point?
    31. // general kinematic equation: vf = vo + at | our forumla: t = (0 - vy) / g
    32. var halftime: float = (0.0 - velocityY) / g;
    33.    
    34. // How long will the projectile be in the air?
    35. var fulltime: float = halftime*2;
    36.    
    37. // How high will the projectile get?
    38. // the 'up' velocity will be at 0 at this point - Halfway (0.5) between begining and end.
    39. // displacement equation: d=(v*t)+(0.5*g*t^2) | our formula: d=(v*y*t)+(0.5*g*y*t^2)
    40. var totalHeight: float = (velocityY*halftime)+0.5*g*Mathf.Pow(halftime,2);
    41.    
    42. // How far away will the projectile land?
    43. // assuming no air resistance: (forward velocity) * (time in flight)
    44. var totalDistance: float = velocityZ * fulltime;
    45.  
    46.  
    47. // Where is the middle of the entire trajectory?
    48. var midOffset: Vector3 = Vector3 (0, totalHeight, (totalDistance/2) );
    49. var midPoint = transform.position + transform.TransformDirection(midOffset);
    50.    
    51. // What is the position (or location) the projectile will hit?
    52. var endOffset: Vector3 = Vector3(0, 0, Mathf.Abs(totalDistance));
    53. var endPoint = transform.position + transform.TransformDirection(endOffset);
    54.    
    55.    
    56. // print a simple gizmo trajectory line
    57. Debug.DrawLine (transform.position, midPoint, Color.blue);
    58. Debug.DrawLine (midPoint, endPoint, Color.red);
    59.  
    60.  
     

    Attached Files:

  5. rd42

    rd42

    Joined:
    Apr 8, 2010
    Posts:
    77
    This is great! Thanks for sharing it.

    I'm a little confused on how to use it. I applied it to a sphere gameobject and it just slowly goes across the screen regardless of the velocity I have it set too. Any ideas on what I'm doing wrong?

    Will this work on Unity iPhone?

    Thank you again for sharing your work.
     
  6. JRulkiewicz

    JRulkiewicz

    Joined:
    Sep 5, 2011
    Posts:
    17
    I imported this script and attached it to a sphere and I get the following error:


    ArgumentException: You are not allowed to call get_transform when declaring a variable.
    Move it to the line after without a variable declaration.
    Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
    trajectory..ctor () (at Assets/Standard Assets/Scripts/trajectory.js:47)
     
unityunity