Hello! I’m incredibly new to c# and unity in general so forgive my ignorance! I’ve been messing around with rpg things and recently have been wanting to make my lil dude shoot projectiles! (similar to a game call rotmg where they go out a certain distance from the player at the direction of the mouse and disappear unless collision is detected!)
I attached a script to my player for instantiating my projectile (deals with spawn point and rotation) and then made a prefab of the bullet sprite with rigidbody2d, a boxcollider, and a super simple bulletmove script!
BulletInstantiateScript:
public GameObject bullet;
public float speed;
//private Rigidbody2D myRigidBody;
// Use this for initialization
void Start()
{
//myRigidBody = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector2 target = Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
Vector2 myPos = new Vector2(transform.position.x, transform.position.y);
Vector2 direction = target - myPos;
direction.Normalize();
Quaternion rotation = Quaternion.Euler(0, 0, Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg + -90f);
GameObject projectile = (GameObject)Instantiate(bullet, myPos, rotation);
GetComponent<Rigidbody2D>().velocity = direction * speed;
//myRigidBody.velocity = direction * speed;
}
}
and the bulletMoveScript:
public float timeToDestroy;
public float amount;
private Rigidbody2D myRigidBody;
void Start()
{
myRigidBody = GetComponent<Rigidbody2D>();
}
// Use this for initialization
void Update()
{
if (Input.GetKey(KeyCode.Mouse0))
{
Vector3 sp = Camera.main.WorldToScreenPoint(transform.position);
Vector3 direction = (Input.mousePosition - sp).normalized;
//myRigidBody.AddForce(direction * amount);
myRigidBody.velocity = direction * amount;
}
//here forward is the destroy over time script bc idk how to do distance yet
timeToDestroy -= Time.deltaTime;
if (timeToDestroy <= 0)
{
Destroy(gameObject);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Enemy")
{
Destroy(this.gameObject);
}
if (other.gameObject.tag == "collider")
{
Destroy(this.gameObject);
}
}
the original bulletinstantiate script was supposed to move the bullet by itself (hence the extra lines of code in there) but I couldn’t figure out how to do that!
The problem I’m having is that in this way I can create the projectile when I fire, its rotation is correct, and the initial direction is correct, but if I fire again, the projectile(s depending on if there are multiple) changes direction towards where I just clicked! I have scoured the internet for 7 ish hours now and have messed around with the code for an equal amount of time all to no avail! Idk what I’m doing but apparently I need some help! How do I make it so that each projectile spawn has a set direction (dictated by the mouse position at time of firing) that will not vary if I continue to shoot? (I would like to have it continue to shoot projectiles as long as the fire button is held down and have each new spawn of projectile aimed toward wherever the mouse is at time of spawn but that seems as simple as changing getkeydown to getkey and then adding a cooldown to the firing! I’m not quite sure though!)
Thanks for your help!
(P.s. I know my code is really messy but I just started about two weeks ago with c# so just bear with me!)