• Advertisement
Sign in to follow this  

Click, hold, pull back, release to fire

This topic is 584 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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. 

Share this post


Link to post
Share on other sites
Have you programmed anything before? We need to know what you already know so we can connect our explanations to your knowledge.

Share this post


Link to post
Share on other sites

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. 

Share this post


Link to post
Share on other sites
(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. Edited by Nypyren

Share this post


Link to post
Share on other sites

This topic is 584 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement