- Home /
Trajectory Projectile with Collision Detection
Right now I am trying to create projectile for cannon in which I want to detect collision also.
At present I have simple working projectile after using two references
Unity – How to display projectile trajectory path in Unity 3D?
Path/Trajectory prediction – What path will the object take
But in this I also want to represent bounce in projectile path. I searched everywhere but didn't find any useful content.
Please provide some guidance here.
You will need to read up on some physics to get that right. I don't know how to do it properly, but I would do probably do something like this:
When the cannon ball lands on the ground, calculate another trajectory path, based on the angle of the ball had when landing and its speed. And take into account how "bouncy" the ground is, since the less "bouncy" the ground is, the less will the canon ball bounce.
But before throwing ball, I want to display projectile motion with ground collision detection.
Then you still need to calculate the whole trajectory including the bounces first, and then draw them like that unity link you posted.
You need to make a collision check for every step to know when to calculate a bounce. And how to do that, all depends on how you have setup your game environment.
Then how to detect collision, At present in game environment I have only four walls not anything else. But for projectile I can't able to detect collision. Thanks for your suggestions.
Unity have collision objects which you can use. Use a simple BoxCollider on all objects. Get the Bounds from the objects, and see if they intersect on every update. If they intersect there is a collision.
This is a VERY simple way, and will not work very well if you use objects that doesnt fit into a BoxCollider, so you really need to read up on collisions to get it right.
Answer by Tehnique · Jul 14, 2014 at 06:18 AM
I'd start from here.
First, set drag and angular drag to 0 on the projectile (rigidbody), set all friction settings to 0, and bounciness to 1, on the projectile material.
Second, use your normal trajectory prediction/drawing algorithm (I suppose you have one from the links you posted), up until the first collision. When you have a collision, predict the bounce using a SphereCast to get the RaycastHit where the ball would hit the collision object. From the RaycastHit, get the Normal component and use Reflect with the raycast direction as inDirection and the RaycastHit.Normal as inNormal. This will give you the resulting vector of the bounce. Continue from there with your normal bouncing algorithm to find trajectory after bounce.
Later Edit: In order to use the SphereCast for your scenario do the following:
Select the last 2 points of the path (pointA-before last and pointB-last).
Make sure the first point (pointA) is above ground. If not, reselect your points as the ones before them (so A is last-2, B is last-1). Go to -> and repeat until the first point is above ground.
When you have PointA above ground calculate the vector from pointA to pointB. This is your SphereCast direction, and pointA (above ground) is your SphereCast origin. Run your SphereCast and continue as described above.
With all friction settings set to 0, your ball should behave as the SphereCast.
I have already gone through Vector3.Reflect. I want to draw path of moving object before it fires from cannon with bounce and collision detection.
To do it realistically, I would simulate the path first with an identical object that I do not render to the player. If nothing changes afterwards, the second path should be identical.
At present I am working on clone of this type of game http://www.2dplay.com/flick-headers-euro-2012/flick-headers-euro-2012-play.htm
In that I don't have time to throw dummy object and calculate position for it as per my assumption.
Try to set all the friction/material settings as in my original answer, and use SphereCast, RaycastHit.Normal and Reflect to predit the trajectory. With the mentioned friction/material settings, the ball should behave almost exactly like the SphereCast predicts (whn bounce is involved). You'd have to run the SphereCast very close to the object on which you bounce, so you can approximate the ball hit trajectory as straight (before bounce).
@Tehnique, Thanks for all these suggestions but all these confusing me at present so please update your answer and provide story in clear way.
Your answer
Follow this Question
Related Questions
Ball Isn't following Trajectory line : Unity3d 1 Answer
A node in a childnode? 1 Answer
Bounce when hitting wall 2 Answers
Unity Messing up Basic Math? Projectile Trajectory Algorythm 1 Answer