I’m trying to create a top down shooter in Unity for a college project and am having an issue programming a shooting mechanic, below is the script for the bullet prefab that’s suppose to make the bullet move in the direction that the player’s mouse was at upon instantiation.
I was able to use some code from one of unity’s online tutorials for having an object move towards the mouse position, the issue is that once it reaches the mouse position it stops. I need to know how to make it so that it moves towards the direction of the mouse position, then keeps moving in the same direction indefinitely?
public class Shoot : MonoBehaviour
{
public int speed;
Vector3 mp;
Vector2 md;
float mA;
// Start is called before the first frame update
void Awake()
{
mp = Camera.main.ScreenToWorldPoint(Input.mousePosition);
md = mp - transform.position;
mA = Vector2.SignedAngle(Vector2.up, md);
transform.eulerAngles = new Vector3(0, 0, mA);
}
// Update is called once per frame
void Update()
{
transform.position =
Vector2.MoveTowards(transform.position, mp, speed * Time.deltaTime);
}
}
Guy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.