Advertisement

Click, hold, pull back, release to fire

Started by September 14, 2016 06:54 PM
10 comments, last by jpetrie 8 years, 2 months ago

2d game. click object, hold down, pull back, release and it fires in opposite direction you pulled back. Pull back distance increases force when released. You can rotate the object 360 degrees to fire in any angle. Whats the code to do this?

You've described it thoroughly enough to begin implementation. Is there a specific part you're having trouble with?
Advertisement

I sent you a personal message. I just had 13 viewers with no replies so I figure I should reply to your comment here so they all know I'm serious.

I need the code please. Someone out there with 1o-15 minutes of their time to code it and send it to me. If its too much to ask for, please personal message me what you want and I'll touch back with you to see what I can do for you.

That's not how game development works.

We aim to teach you how to do it yourself

What better way to teach as to show? Did Jesus only say things or did he show you how? Come on guys. I understand nobody likes the idea of working for free. So think of it as a charity donation to help a poor soul out. I will learn if you show me.

Advertisement
Have you programmed anything before? We need to know what you already know so we can connect our explanations to your knowledge.

I personally messaged you and told you. Hmm. Well, I used unity to make "roll a ball". but that was easy because they show you all the coding in the tutorial videos. but i want to change up the movement mechanics to click, hold, move, release to fire.


The mechanics don't have to be 100% like this. I mean, there are other ways to do this. Something very similar to what I want is a pool table game. I thought it would be easy to ask for help here.

(For context for other readers, the PM he sent me says he's using Unity, so I'm basing this post on that)

Ok, so:

When the user presses the mouse/touch on the ball, keep track of that position (either the ball position or the click position, whichever makes more sense for you) and set a flag that indicates that the user is starting to drag. You'll need either a Vector3 and a bool or a Vector3? (the ? in C# means "nullable", which is sort of like combining a bool and another type in a single variable.) Those variables need to live for multiple frames, so define them as members of a MonoBehaviour class.

When the user moves the mouse while dragging, take the starting position and the current position of the ball/mouse (depending on whether you're moving the ball while you pull or not) and make a vector from that: (start - current). Imagine that subtracting two position vectors creates an arrow between them, pointing towards the first position, from the second position. This will be a vector that represents both the direction and power of the pull. You can use this to draw a line or whatever you want to do to show the user what's happening.

When the user releases the mouse while dragging, set the ball's velocity equal to that vector. Clear your dragging flag, start any additional animations, play a sound, etc. After that, just let Unity's physics engine move the ball normally.

Make a good bold effort to code @[member='Nypyren']'s algorithm, if it fails, post your code attempt and people can guide you to improve it.. That way you are actively learning and will learn better

Asking others to code it from scratch for you and hand it to you is a disservice,, and means you will come back a hundred more times asking for more of the same

can't help being grumpy...

Just need to let some steam out, so my head doesn't explode...

This topic is closed to new replies.

Advertisement