Bowman 2 style archer control AddForce?
Hello all.
I am trying to figure out how to script the mechanic (C#) from "Bowman 2" (web game), where your angle of leftMouseDown to leftMouseUp == player's firing arc, and the distance between leftMouseDown and leftMouseUp is the amount of force (AddForce?) applied to arrow. I'm sure it's not that hard, but I am very new to programming.
I was playing around with a Spring Joint2d, but I can't figure out how, and when to instantiate Arrow upon leftMouseDown vs leftMouseClickUp. Like, if I instantiate Arrow upon release (after the drag of the mouse), then how could the spring be stretched during the drag, and then be applied to the Arrow, if at that point the Arrow hadn't been created yet? Also, in a game with multiple Arrows shot by a moving character, it doesn't seem like I would want SpringJoints?!?
Maybe SpringJoint2D is a bad idea, but I'm trying...
Even just an explanation of an approach would be helpful. i.e. Thank you very much for your time!
This is the general approach I would take:
On a mouse click and hold, record the initial cursor position and then continually check the cursor position (and do any visual feedback stuff)
When the mouse is released, calculate the vector between the cursor position at release and the original click location (myVector = cursorInitialPosition - cursorPosition) and calculate the length of the vector (Vector2.length(myVector) I think).
myVector represents the direction the arrow will be shot, and it's length (multiplied by some constant) represents the strength of the shot.
Instantiate the arrow and use Rigidbody.AddForce with myVector as the direction and it's length as the force.
Hope this helps.
Thank you ZefanS! That was a very clear and helpful explanation. I have since figured out a way or two to shoot the arrow, now I'm struggling to rotate the thing properly. I have asked another question about it, can't find the link, but you have helped enough already. I appreciate you!
Answer by bowman123 · Sep 13, 2016 at 05:30 PM
@1instrument There is one more game similar to the one you linked called Apple Shooter. Also man i am struggling to find your thread on arrow rotation. Did you find an answer and can you link the thread from here? thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220612044649im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Add force with acceleration 2 Answers
[Unity2D] Firing bullets based on gun orientation 1 Answer
Pc controls to mobile controls 0 Answers
Angles and Linecasting 2 Answers
Raycast under crosshair 0 Answers