Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

[Released] Trajectory - Easy and Accurate Trajectory Arc Creation

Discussion in 'Assets and Asset Store' started by superroxfan, Nov 8, 2013.

  1. superroxfan

    superroxfan

    Joined:
    Aug 23, 2013
    Posts:
    83
    $Screenshot5-Small.png
    $Screenshot11-small.png

    You've just seen a couple of screenshots from my Unity asset, Trajectory! So what does Trajectory do? Well, Trajectory creates an arc that shows the path that a projectile will travel through the air. This arc can be made using Line Renderer (as seen in the lower image) or a custom texture made by you (as seen in the top image)! This can be used for an aiming arc in launcher games, golf games, games with grenades, games with mortars, and several other types of games that need physics-accurate aiming/trajectory arcs.

    Features
    -Real physics equations used for highly accurate predictions
    -Two different scripts! One specializes in 2D and the other in 3D
    -Use of line renderer for easy arc customization
    -Custom line length
    -Custom textures if you don't want to use line renderer
    -Custom gravity value for low, high, or no gravity situations
    -Use of layer mask so you can have the arc pass through some objects but stop when it hits others

    To Do List
    -Drag! Once I figure out what Unity uses for drag, I'll be able to add it to Trajectory. If anyone knows Unity's drag formula please post here or PM me
    -Increased Performance!

    I'd love to hear what you feel needs added, so feel free to post here or PM me your ideas.

    As you can see, Trajectory has plenty of features and more coming! And the best part is you don't have to write any code to get it working! Here's a couple of screenshots of all of the parameters you'll have to play with:
    2D Trajectory script inspector
    $InspectorScreenshot.png

    3D Trajectory script inspector
    $3DInspectorScreenshot.png

    2D Demo

    3D Demo

    More demos can be found on the asset store page located here:

    Asset Store Page. Only $15!
     
    Last edited: Nov 22, 2013
  2. superroxfan

    superroxfan

    Joined:
    Aug 23, 2013
    Posts:
    83
    I'll be updating Trajectory soon to add a few small features, but I want to know what you, my customers, would like to be added. I eventually plan on adding the ability to calculate the trajectory of a projectile in the x, y, and z (3D) dimensions. That may be a while down the road, depending on how much you feel this is a needed feature. So feel free to post here or PM me a feature you would like to be added!
     
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Looks cool! some questions:

    - js or c# source?
    - does it generate garbage
    - what does the api look like?
     
  4. superroxfan

    superroxfan

    Joined:
    Aug 23, 2013
    Posts:
    83
    Hi HippoCoder!

    Thank you! Now to the questions you had:

    -It's in C#.

    -I'm not quite sure what you mean by "garbage". It does create whatever you put in for the "Texture Object" parameter "Max Number Of Points" number of times. So if you set "Max Number Of Points" to 750 and it will instantiate 750 "Texture Object"s at the beginning of the script's life. This clutters the hierarchy quite a bit, and I plan on fixing the clutter next update. Though, if you put nothing in the "Texture Object" parameter it will just use Line Renderer with no clutter.

    -There's not really an API at the moment. No custom functions are made in this script. I've focused on allowing the user to do everything they need from the inspector window for easy customization. The entire asset for making the trajectory arc is contained inside of a single C# file; everything else is just for the demo scenes. If you don't mind my asking, what exactly would you expect an API to be for this asset?
     
    Last edited: Nov 9, 2013
  5. superroxfan

    superroxfan

    Joined:
    Aug 23, 2013
    Posts:
    83
    Just submitted an update for review! Hopefully it will be reviewed shortly and you can make use of the new features. It's a rather small update, but an update none-the-less. Here's what I added:

    -Vector2 gravity, which allows Trajectory to predict when an object has gravity forcing it in the X and Y directions.
    -Less hierarchy clutter, which allows you to focus on your objects rather than the "Texture Objects" that tend to flood the hierarchy. All "Texture Objects" are now stored in a single object.

    I was going to add drag also, but I have no idea how Unity implements drag. If drag is a much wanted feature I can always create my own physics-accurate drag script for you to use instead of the rigidbody drag. My next task, however, is to make Trajectory able to calculate the trajectory of an object in all 3 dimensions.
     
  6. I am da bawss

    I am da bawss

    Joined:
    Jun 2, 2011
    Posts:
    2,574


    What hippo meant was that will it create situation for Unity's GC (Garbage Collection) to be kick in? (which is not ideal - since we have limited control on that and they are a major performance issue associated with that - which is why people are trying to avoid GC in Unity)

    "Garbage, in the context of computer science, refers to objects, data, or other regions of the memory of a computer system (or other system resources), which will not be used in any future computation by the system, or by a program running on it. As computer systems all have finite amounts of memory, it is frequently necessary to deallocate garbage and return it to the heap, or memory pool, so the underlying memory can be reused."

    http://en.wikipedia.org/wiki/Garbage_(computer_science)


    "In computer science, garbage collection (GC) is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program"


    http://en.wikipedia.org/wiki/Garbage_collection_(computer_science)
     
  7. superroxfan

    superroxfan

    Joined:
    Aug 23, 2013
    Posts:
    83
    Hello Bawss!

    Thank you for clearing that up! I knew there was a process that got rid of unused data, but didn't realize it was called "Garbage collection". After doing some research into garbage collection, I have found that using "LineRenderer.SetVertexCount" may cause garbage. I am using Unity Free, so I can't use the profiler to see for sure. Now that I realize the possible performance cost of using it, I will change my code to use it less. Thank you for showing me this so I may increase performance!
     
  8. superroxfan

    superroxfan

    Joined:
    Aug 23, 2013
    Posts:
    83
    Trajectory has just been updated! Here's what's new in this update:

    -Vector2 gravity! This allows you to calculate the trajectory of a projectile when gravity is pulling in both the X and Y directions
    -Less hierarchy clutter! All "Texture Objects" generated are stored in an empty game object to reduce clutter to a single object

    What I'm currently trying to implement into Trajectory is 3D support. I have been sick this week and, sadly, have been unable to put much time into updating Trajectory further. But have no fear! I will begin creating the next update again A.S.A.P.
     
  9. kimsama

    kimsama

    Joined:
    Jan 2, 2009
    Posts:
    166
  10. superroxfan

    superroxfan

    Joined:
    Aug 23, 2013
    Posts:
    83
    Thank you for the link kimsama! But what I meant by drag is the force caused by air resistance that slows an object down.

    I am currently working on 3D. The math is a bit more complicated to implement than anticipated, but I plan on releasing it this week.
     
  11. superroxfan

    superroxfan

    Joined:
    Aug 23, 2013
    Posts:
    83
    The 3D Update!!

    That's right, folks, I've submitted a 3D update of Trajectory to the asset store! There will be two scripts in the next update - one specializes in 2D and the other in 3D. Note that this update increases the price to $15. It's still under review by the Asset Store officials, so if you only want to pay $10 for it your last chance is now!

    Demo of the 3D Update

    Thank you all for your support!
     
    Last edited: Nov 22, 2013
  12. superroxfan

    superroxfan

    Joined:
    Aug 23, 2013
    Posts:
    83
    Woah! That was a very fast acceptance time by Unity! Usually it takes about a week! Well, then, down to business

    The 3D update is now live!

    So here's what's new it this update:

    -3D support! This is a feature many people have asked for, and I have finally gotten it done! There are now 2 Trajectory scripts. The first one, named "TrajectoryPredictor", predicts solely in 2 dimensions. It's more efficient, however, so it still has a use in 2D only projects. The next script, named "TrajectoryPredictor3D", predicts the trajectory in all 3 dimensions.

    -Cleaned up some clutter in the Trajectory code.
     
    Last edited: Nov 22, 2013
  13. Brutang

    Brutang

    Joined:
    Nov 1, 2013
    Posts:
    23
    Just picked this package up for a 2D game I'm working on to throw rocks and spears at a correct trajectory. Hoping it works as it looks like it will!

    Edit: After digging around for a bit I realized I'll need to switch the rotation from Left/Right keys to the mouse. The way my game is set up is the player throws things towards the crosshair - any tips on having it smoothly recalculate the arc based on mouse position?

    Thanks,

    -Bryce
     
    Last edited: Nov 22, 2013
  14. superroxfan

    superroxfan

    Joined:
    Aug 23, 2013
    Posts:
    83
    Hello Brutang!

    Thank you for purchasing Trajectory! The angle of rotation is calculated from the z-axis rotation of the object that the script is on. The trajectory arc protrudes from the x-axis direction of the object. Therefore, I believe you need to have the x-axis of the object pointed toward the mouse. Possibly Quaternion.LookRotation would help?
     
    Last edited: Nov 22, 2013
  15. Brutang

    Brutang

    Joined:
    Nov 1, 2013
    Posts:
    23
    Thanks! Yep that's what I ended up doing. I made an empty game that follows my hand sprite around.

    Then, in Update I added this to your TrajectoryPredictor attached to that gameobject and removed the Left/Right controls

    (I converted your scripts to JavaScript because that's what I use :) )

    Code (csharp):
    1. //Setting Up Mouse Look At Vars
    2. var mouse_pos:Vector3;
    3. var target:Transform;
    4. var object_pos:Vector3;
    5. static var mouseAngle:float;
    6.  
    7. function Update()
    8. {
    9.         //follow mouse
    10.         mouse_pos = Input.mousePosition;
    11.         mouse_pos.z = 0; //The distance between the camera and object
    12.         object_pos = Camera.main.WorldToScreenPoint(target.position);
    13.         mouse_pos.x = mouse_pos.x - object_pos.x;
    14.         mouse_pos.y = mouse_pos.y - object_pos.y;
    15.         mouseAngle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
    16.         transform.rotation = Quaternion.Euler(Vector3(0, 0, mouseAngle));
    17.  
    18.         transform.position.x=left_hand.position.x;
    19.         transform.position.y=left_hand.position.y;
    20.         transform.position.z=left_hand.position.z;
    That will make the gameobject/Trajectory script point towards the mouse.

    Then I stuck the Spear object as a child of that GameObject so it rotates around. When you throw the spear it drops the Parent relationship. When you pick it back up it resets to being a child of the Trajectory object so they're always pointing the same direction when held.

    Everything works great so far! Very happy with how it's turning out. My big issue now is figuring out how to make the Spear object rotate along the arc of the trajectory so the tip is facing the correct way.
     
    Last edited: Nov 23, 2013
  16. superroxfan

    superroxfan

    Joined:
    Aug 23, 2013
    Posts:
    83
    Hello Brutang!

    I am ecstatic to hear that Trajectory is helping you in your game! I suppose you basically need your spear to look toward the direction in which it's moving? I tested a little bit and found that if you do "transform.LookAt(transform.position + rigidbody.velocity)" then the object points towards the direction you want it to. Just make sure you have all of the rotation constraint boxes checked on the rigidbody component! Hope this helps!
     
  17. Brutang

    Brutang

    Joined:
    Nov 1, 2013
    Posts:
    23
    Thanks for the reply.

    I'm using a rigidbody2D for the spear. As such, I tried

    Code (csharp):
    1.  
    2.         transform.LookAt(transform.position + rigidbody2D.velocity);
    3.  
    but it's rotating the spear on the X and Y, making it virtually flat.


    so then I changed it to:
    Code (csharp):
    1.  
    2. transform.LookAt(transform.position + rigidbody2D.velocity);
    3. transform.rotation.x=0;
    4. transform.rotation.y=0;
    5.  
    and it's working great if you throw it to the RIGHT, but it won't throw to the left. It just plants itself in the ground or launches backwards.

    EDIT: Got it all working perfectly. Just had to flip the Spear's localScale.X if the character is facing left.

    Thank you for the help! :)
     
    Last edited: Nov 26, 2013
  18. superroxfan

    superroxfan

    Joined:
    Aug 23, 2013
    Posts:
    83
    Hello Brutang!

    I completely forgot that "transform.LookAt" points the Z axis toward the Vector3 coordinate when I posted that! That's why I recommended "Quaternion.LookRotation" for the throwing object rotation earlier-it allows you to point ANY axis toward a position. Maybe using that function will help again? I can't test this at the moment, but hopefully it will work for you.
     
    Last edited: Nov 23, 2013
  19. Brutang

    Brutang

    Joined:
    Nov 1, 2013
    Posts:
    23
    I'm back to bug you for another question :)

    In my script I use a very similar method to reload the cannon as you do in your examples (you do yield waitfor reload time, I do a boolean = true to reload after 0.5 seconds, etc.)

    In both of our methods, there are instances where it will end up instantiating 2 projectiles in the cannon. You can see this on your script if you kinda mash the space bar during reload times; probably 1 in 10-15 times you'll end up with 2 Projectile(Clone)s in your cannon. Mine is around the same. I've tried both Update and FixedUpdate for reloading as well.

    Now on your example this isn't a big deal, but in my game if you randomly do double damage via throwing 2 projectiles it's very weird. You can really only tell in either of our cases by looking at the hierarchy and seeing there are 2x Projectile(Clone)s as children of the cannon.

    EDIT: OK I'm dumb... simply adding
    Code (csharp):
    1. if (transform.childCount == 0)
    to the reload script fixed it.
     
    Last edited: Nov 26, 2013
  20. superroxfan

    superroxfan

    Joined:
    Aug 23, 2013
    Posts:
    83
    I'm glad that you fixed your problem! This is the first bug that has been reported, so thank you for reporting it. If you don't mind, I may use your method for fixing it :) Also, make sure to report ANY bugs you find! I want my assets to be as bug-free as possible.
     
  21. 8r3nd4n

    8r3nd4n

    Joined:
    Sep 10, 2011
    Posts:
    30
    I've been using Trajectory in my game and it has been working splendidly.
    I have a question that I haven't been able to solve.

    When the trajectory line hits an object, it breaks the line which is as desired.
    What I would like to do is get the information of the object that the line has hit, so I can do things to that object like change the shader to a rim outline so the player know that they are hitting something a bit clearer.

    I have found the section where it should be (at line 200) but don't really get the layer mask bit. I'm used to Raycasting to get hit infor but not sure how it can be done with layer masks.

    Any thoughts?

    Cheers

    Code (CSharp):
    1. //Makes sure the line Index does not exceed the maxNumberOfPoints
    2.             while(lineIndex < maxNumberOfPoints){
    3.  
    4.                 if(Physics.CheckSphere(vector, 0.01f, layersToHit) == true){
    5.  
    6.                     //Get the hit objects info
    7.                 }
     
  22. superroxfan

    superroxfan

    Joined:
    Aug 23, 2013
    Posts:
    83
    Hello 8r3nd4n,

    There are three steps to adding your desired functionality:

    1.) Add this variable as a class field:
    Code (CSharp):
    1. [HideInInspector]
    2. public Collider[] collidersHit;
    3.  
    2.) Add this to the else statement of "if(Physics.CheckSphere(vector, 0, layersToHit) == false)":
    Code (CSharp):
    1. collidersHit = Physics.OverlapSphere(vector, 0, layersToHit);
    2.  
    3.) Add this inside the "while(lineIndex < maxNumberOfPoints)" loop and just above "lineIndex++;":
    Code (CSharp):
    1. if(lineIndex == maxNumberOfPoints - 1)
    2.     collidersHit = new Collider[0];
    You can then access the colliders hit by the Trajectory line by accessing the "collidersHit" variable. If you have trouble getting this to work just email me at swankyassets@gmail.com your invoice number and I'll email you the script!
     
  23. 8r3nd4n

    8r3nd4n

    Joined:
    Sep 10, 2011
    Posts:
    30
    Hey superroxfan

    Thanks a lot for that!

    Sorry it took so long to get back to you but for some reason I didn't get an alert saying that anyone had posted a reply. I need to go through my settings again and make sure I get those alerts.

    Cheers!
     
  24. byerdelen

    byerdelen

    Joined:
    Mar 27, 2010
    Posts:
    68
    Hi,
    This type of calculation was what I wanted and a sure buy but for me, an api is a must have since I need the data.
    Some questions on that way:
    -Can I just set a distance, speed, forces and get angle of launch?
    -Can I use two forces and still get calculations instead of the simulation of launch?
    -Do you use Unity.physics from throwing object or do you make your own calculations and how undertandable is it?

    Thanks
     
  25. superroxfan

    superroxfan

    Joined:
    Aug 23, 2013
    Posts:
    83
    Hi byerdelen,

    I just responded to the email you sent. I'll copy and paste my response here:

    1.) No, the angle of launch is an input required to launch the projectile.
    2.) Yes, it can do horizontal and vertical gravity at the same time. However, for it to be accurate these values must be constant. For example, you can do a constant wind, but you cannot have the wind change at any point.
    3) Yes, full source code is given and it is well commented.
     
  26. Technipoet

    Technipoet

    Joined:
    Mar 5, 2014
    Posts:
    4
    Hey,

    Prospective buyer here. Would I be able to use this to provide a target transform/position it needs to hit? I am looking for a system that I can provide a target to with the force already determined. Ideally I'd be able to choose between high and low arc solutions if they exist (My quadratic knowledge tells me there should always be 2 options).

    Is this something I could extract from your code?

    Thanks
     
  27. superroxfan

    superroxfan

    Joined:
    Aug 23, 2013
    Posts:
    83
    Hello Technipoet,

    I'm sorry, but Trajectory requires an input velocity and an input angle. It cannot take a target position as an input.
     
  28. CoderPro

    CoderPro

    Joined:
    Feb 21, 2014
    Posts:
    327
    Could you upload the latest version to webplayer ? I can run Web GL in my browser. Thanks
     
  29. TokyoDan

    TokyoDan

    Joined:
    Jun 16, 2012
    Posts:
    1,080
    Will this run in Unity 4.6.x and 5.2.x?
     
  30. TokyoDan

    TokyoDan

    Joined:
    Jun 16, 2012
    Posts:
    1,080
    I am making a game where I stand behind a counter, and try to fling food into customers' mouths. The customers are standing in front of the counter.

    I want to use my finger like this:

    1. Slide back to increase velocity/arc to adjust the vertical. If I slide back to much the point of contact may be on the customer's forehead. If I don't slide back enough the point of contact may be on the customer's chest.
    If I slide back just enough the point of contact will be the customer's mouth.

    2. Slide left/right to set horizontal angle. Slide too far left or right and I may hit the customer's cheeks, or altogether miss the customer's head, or even hit the wrong customer standing to the left or right of the target customer.
     
  31. pondo

    pondo

    Joined:
    Mar 22, 2015
    Posts:
    7
    Hey superroxfan,

    I just submitted a patch via email. It fixes a few different things but the main problem I tried to address was how Trajectory handles being repeatedly enabled and disabled. If you disable the object that uses a Predictor (or just the behavior) it will not clean up all the visual artifacts and I think it should do that.

    Anyway, thanks for a great asset. I'm using it in my game and it does its core job really well.
     
  32. phoenixrising

    phoenixrising

    Joined:
    Aug 9, 2013
    Posts:
    57
    Any word on incorporating drag?

    I really like this asset, it works really well but drag would really help me get a more accurate prediction!

    Thanks
     
  33. unityjpg

    unityjpg

    Joined:
    Feb 18, 2015
    Posts:
    1
    I just bought this asset, but i'm stuck, i also need drag for my game.
     
  34. BiffJenkins

    BiffJenkins

    Joined:
    Apr 2, 2015
    Posts:
    4
    I can't seem to get the LayersToHit to work with Mesh Colliders. Is this a known issue?
     
  35. GGStudiosLLC

    GGStudiosLLC

    Joined:
    Oct 14, 2015
    Posts:
    4
    I am having trouble getting the mask to work with 2D colliders. I changed the spherecast to a physics2D.Linecast and it works at first but then it breaks and the line disappears. I tried to debug the issue and it seems to go into a loop and the i variable is "0" and the lineIndex is "1". If anyone has got this to work with 2D colliders any help would be much appreciated. Also I would like to animate the line so the dots are always moving forward, if anyone has done this as well.