I’m pretty new to Unity and the whole library that comes with it, so I’m still getting my footing here.
I’m making a 2d arcade-style space shooter similar to Geometry Wars or other similar games. I want the bullet to project in the direction of the mouse’s click, regardless of the direction that the ship is facing. I’ve been toying around with all kinds of Vector3/Quaternion stuff and I haven’t had any luck. My code in the player script looks like this:
if (Input.GetMouseButtonDown(0)) {
bullpath = Input.mousePosition;
socketBullet.rotation.SetLookRotation(bullpath, Vector3.up);
Instantiate (bullet1, socketBullet.position, socketBullet.rotation);
}
Where bullpath
is a Vector3
and socketBullet
is a Transform
that is the origin of the bullet.
THEN, in the bullet script, I use:
void Update () {
transform.position += transform.up * speed * Time.deltaTime;
Destroy(gameObject, lifespan);
}
I’m sure that I’m missing one simple thing that I don’t know simply because I’m new to the tools that Unity provides, but as it stands the bullet will only fire in the direction that the ship is facing. I can, of course, control the direction the ship is facing with WASD and the bullet will fire in a straight line from the front, when I want it to fire in a straight line toward wherever the mouse was clicked.
Any help is greatly appreciated.