New Defender Help - Parabolic Arc?

Hi All,

For the last couple of days I had been working on trying to get the Idle animation right for the Melonpult from Plants Vs Zombies in Glitch Garden, now it’s about as good as it going to get, but, before doing any animation for the attack phase other than a stationary graphic for now. I need to think about how to throw the “Melon”.

Now, it’s relatively easy to get the melon instantiating from an origin point it’s the next part has me a little bit muddled. The melon won’t be able to fly across the screen as the other projectiles, but, rather travel in a parabolic arc from the instantiation point to the x-coordinates of the first spawned object - which I will put-off until I need to worry about it. The main problem for now is how do I move an object on a parabolic arc? What is the mathematics behind moving an object in a for lack of a better word a perfect arc? For now all I am going to do for now is make the arc end at the last square of the playboard, ie, World co-ord of 9. So any tips, hints, or websites (talking about how I might code the arc), would be of tremendous help to at least getting me started on this.

Thankyou and Regards,
Vaughan.

Hi Vaughan,

You could throw some math at it, and I’m sure that we could find some articles on Google to help with that, but I wonder, do you need to?

For example… if you are going to create an animation for it, when not change the Y value so that the animator calculates this for you? Straight off the bat it might not be perfect but with a bit of tweaking I think you may be able to achieve the desired result.

What I’m thinking in my head is along these lines…

For say a 2 second animation, have keyframes at the start and end where the Y value of the position is at 0. Then add another in the middle of the animation timeline, and set it to what ever height you feel is going to be appropriate (consider the top lane when doing this as you’ll have less space to use). Run the animation and see what happens. It may just move on more linear diagonals by default, so you could amend the curves and perhaps add more keyframes if/where necessary.

slerp might help if you want to look at it from the coding perspective.


Unity - Scripting API : Vector3.Slerp

1 Like

Hello Rob,

There is a reason why I think I have to code it and it because of this. The attackers are going to be moving between the two hits and the arc is going to need to change to take this into consideration from say 9 squares to the point where the attacker is in front of you.

l think I understand where you are coming from creating an animation and passing the x.value of the attacker to the animator, but, I still don’t really understand what the animator is capable of. And coding should give me more control, with a constant speed the trajectory of the arch should be more controllable. The top lane is a problem for sure!

I really don’t know what I’m doing - I shouldn’t be allowed near a computer :rofl:.

Regards,
Vaughan.

…another thought…

Gravity. Use AddForce to the projectile when it is launched and then let the physics engine handle its arc, gravity will enable it to fall back to “earth” sensibly. The only thing you may need to do is prevent it falling further to earth than the Y value of the lane itself.

1 Like

I’m pretty sure you could find the x value of the target, use some interpolation and where (defender.x - attacker.x) / 2 would be the “peak” of the arc

2 Likes

I never ever would of thought of that. I don’t have a great understanding of mathematics or formulae, but, I do understand how this could work! Man, I am going to try this :sunglasses:.

1 Like

No problem! I did think of a few cool ways to animate the projectiles, but I found no use for it in the end. That was one of them. Who knows, maybe later?

2 Likes

After hours of struggling and forgetting how the code worked with regards to projectiles, and not using prefabs (doh, what was I thinking), I finally managed to get a projectile firing across the screen. Although I did stuff it up at first and had the catapult mechanism itself firing across the screen … oh man, was that hilarious :grin:. I suppose that’s what you get listening to hair metal whilst scipting - hair brained things happening :stuck_out_tongue:.

Now, I need to setup more prefab components, so, it can hit something, proper attack animation and a number of other things to get it working and then seeing if I can get it hitting the first spawned enemy as it moves across the screen.

Don’t forget to script your catapult’s projectile collider so that it only triggers when hitting the target. If you do not, it will trigger when hitting an attacker in an other lane.

And yes, prefabs are the key for everything. I made my game so that it’s incredibly easy to change or add something and also build the level by scripts rather than placing by hand. If ever you try mine, you will probably notice everything that is prefabed and why it is

1 Like

Oh dear, I never even got that far thinking about it :hushed:. And that might be really tricky for someone like me to do! I will need to spend some time thinking about how I might get around that.

At the moment I am trying to instantiate a prefab appearing on screen for when the 'pult hits the ground and makes a splatter graphic with slight downward and left/right movement plus contain a sound for the strike.

Thanks for the heads up!

You can either instantiate a new prefab on hit, though (somebody correct me if I am wrong) it’s better on ressources to already have it in the projectile’s hierarchy and make it active on hit.

About thinking further ahead, I don’t know how you work right now so excuse me if ever you already doing this, but this is what helped me. When I started Unity long ago I did it step by step and upon encountering something more difficult, to have the same reaction as you : “Oh dear, I did not think of that yet” or simply “I have no idea how I will do it”. Now I simply leave the computer aside and take my pen and paper and I think “Okay, what do I need to do this?” I make a list, which quickly turns into a flow chart.

Honestly, since I do that I have no problems anymore! I never have a moment asking myself how will I do it? I simply do it. Think of your logic before you start your code and it goes pretty well. If you do not, you will have a hard time. But I understand we are on forums for some simple unity courses and it’s already pretty good that you try to go outside of the box for the projects :slight_smile:

1 Like

Well, it’s about to get even worse for people like me who have had some experience with Unity in some areas and almost none is other areas, and, that is, Unity has gotten a whole new coding system called the Entity Component System - and it did my head in, and I only saw a short video on it. It almost reminded me of my old days trying to code in C/C++. I finally thought I had seen the last of the “struct” keyword from C and here it is back with a vengeance.

I expect that for the next couple of iterations of Unity they will support the current model, then became a legacy system and then, the phase it out completely.

Oh and thanks for the tips about getting out paper and pen, I’ll be sure to break that out in the next couple of days and give the current problem some thought.

Privacy & Terms