- Home /
How do I Fire an Arrow Properly ? [SOLVED]
I am making a 2D game, seen from the left, using textures on planes (some of which have... box colliders). And would like some help to achieve the following:
I have a screen which is 480 pixels wide and 320 pixels high. When I click the screen I make an arrow which has an arrow script attached to it (written in C - Sharp). In this script I record the y position of the click. Depending on how high the click is, determines how far the arrow goes. But the arrow can never go further than 480 pixels, because I don't want it to go off screen.
Any Idea what I could do to get this working?
Thanks to a reasonably easy to understand reply! (and no thanks to an unreasonably hard to understand reply!)
P.S: I am using the XZ - Plane, so if your answer has anything to do with rotation... it should take that into account.
Answer by MithosAnnar · May 17, 2012 at 11:07 AM
It took me bloody ages but i finally did it!!
Here it is for anyone who wants to fire an arrow properly.
make a plane with an archer sprite and animation script on him. put an attack script on the archer (just make sure you record the mouse position, so you can send it to your arrow script).
on the arrow script put the following:
ArrowScript.cs
using UnityEngine;
using System.Collections;
public class Arrow : MonoBehaviour {
public float damage = 5;
public float rotationSpeed;
public float arrowSpeed = 5;
private Transform myTransform;
private Vector3 mousePoint;
private bool canTranslate = true;
private bool canRotate = false;
void ArrowStart (Vector3 v) {
myTransform = transform;
mousePoint = v;
if (mousePoint.y > 200) {
myTransform.rotation = Quaternion.Euler(0, 150, 0);
rotationSpeed = Random.Range(2.5f, 5f);
StartCoroutine("CanRotate", Random.Range(0.25f, 0.5f));
}
if (mousePoint.y > 100 && mousePoint.y < 200) {
myTransform.rotation = Quaternion.Euler(0, 170, 0);
rotationSpeed = Random.Range(4f, 8f);
StartCoroutine("CanRotate", Random.Range(0.15f, 0.3f));
}
if (mousePoint.y < 100) {
myTransform.rotation = Quaternion.Euler(0, 200, 0);
rotationSpeed = Random.Range(20f, 40f);
StartCoroutine("CanRotate", Random.Range(0.05f, 0.1f));
}
}
void Update () {
if (canRotate == true) {
myTransform.Rotate (0, (mousePoint.y / 10) * Time.deltaTime * rotationSpeed, 0);
}
if (canTranslate == true) {
myTransform.Translate(new Vector3(-1, 0, 0) * arrowSpeed * Time.deltaTime);
}
if (myTransform.position.z <= - 2.2f) {
StartCoroutine("Destroy");
}
}
IEnumerator Destroy () {
canTranslate = false;
canRotate = false;
yield return new WaitForSeconds (2);
Destroy(gameObject);
}
IEnumerator CanRotate (float f) {
yield return new WaitForSeconds (f);
canRotate = true;
yield return new WaitForSeconds (f * 2);
canRotate = false;
}
}
Special thanks to Berenger for helping me!
Answer by Berenger · May 16, 2012 at 03:26 PM
You can either find what is the for maximum that can be applied given a certain angle (sounds complicated) or add walls at the borders of the screen. Either you set their position from the scene view, or by script. If the camera is orthogonal, the size of the frustrum orthographicSize 2 for the height, orthographicSize 2 * aspect for the width.
Could I have more on the angle theory?
I don't want to do the borders, because I want the arrows to hit the ground, but not go offscreen.
I don't know if there is a way to calculate an object trajectory given it's mass and the initial force. I'm afraid you'll need to do it empirically. Like, if the angle is > 80, maxForce is 500, > 60 maxForce is 200, > 40 100 etc. It's dirty but it's the best I can think of without the trajectory thing.
Your answer
![](https://koobas.hobune.stream/wayback/20220613071545im_/https://answers.unity.com/themes/thub/images/avi.jpg)