- Home /
Using Raycasting to lob a projectile.
Hey there UnityAnswer Users.
I have a Question... (Ironic?) anyway to the point.
I'm currently making a first person catapult game.
EDIT: I wish to use raycasting to cast a circle on the ground so that when a projectile is fired it will go to where the circle has been cast. The cast circle needs to be controled by using w,a,s,d. I also need to have the projectile fire with a lobbing effect (Like a catapult) when "space" is pressed.
I need to be able to select 3 different projectiles using Keys "1,2 and 3"
Any help would be much appreciated. Even hints on what to do would be great.
Thanks in advanced, Castleforce. (Trent)
EDIT: If any feed back I'm coding in Java... (Only code I know, though I'm really bad)
Answer by mpavlinsky · Mar 12, 2012 at 10:29 PM
I'm not sure I understand fully. If you are asking how to raycast you can use Physics.RayCast() and it's pretty straightforward to get the position to launch at.
If you are asking how to correctly lob the projectile you can either do some physics math to figure out the correct velocity to get your projectile to the end point with your current angle (this might be kind of difficult depending on how sharp you are with this sort of thing) or you can just kind of hack it and make a Bezier Curve that simulates what the trajectory would be and just move it along that. Bezier curves are pretty easy to implement and it's a useful thing to have access to.
In a way yes, Though I want to have the raycast in a circle and controlable by the w,a,s,d or direction arrows. This way the player can position the ray cast then by pressing space the projectile will be launched from the catapult to the raycast circle... Sorry If I'm confusing.
What do you mean circle? Do you mean there is a circle on the ground where your shot is going to go?
I think you should edit your initial question to be more descriptive of the outcome you want, possibly with some sort of diagram or similar video. Also it would help to be more clear with exactly what your question is.
Yes that is what I mean. I have edited the oringal post. I'm not sure if it takes some time to chang or not.
So basically I think what you want is to have some GameObject that sits some distance above the level and hitting the WSAD keys will move it along the X and Z planes. Whenever it gets moved you would do a raycast straight down and see where the ground is directly below it. You can figure out the rendering part here, I assume it's some combination of multitexturing, particle effect, and transparent sphere. Then you can make the Bezier Curve between that location and your catapult that you can move your projectiles along.
Answer by milali · Mar 12, 2012 at 10:33 PM
The simplest way to hack a curve is to add a porabola to your Y axis. so it goes up and comes down the Y while travelling along the XZ in a straight line.
this will give you a predictable lob.
if you want a physics driven approach check projectile equations in wikipedia
Your answer
![](https://koobas.hobune.stream/wayback/20220613065042im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Question about FPS and Raycasting Javascript 2 Answers
Detect face of cube clicked 4 Answers
Projectile wont move 1 Answer
Raycast script help? 1 Answer