Shooting bullets in mouse direction for 2D game
I am working on a top down game where the player can shoot bullets by aiming with his mouse. However, I am quite new to programming and am having a bit of trouble figuring out how to do so. My bullet prefab has this code on it:
public float speed;
public PlayerController player;
public GameObject enemyDeathEffect;
public GameObject impactEffect;
// Use this for initialization
void Start () {
player = FindObjectOfType<PlayerController> ();
if (player.transform.localScale.x < 0)
speed = -speed;
}
// Update is called once per frame
void Update () {
GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, speed);
}
void OnTriggerEnter2D(Collider2D other) {
if (other.tag == "Enemy") {
Destroy (other.gameObject);
//Instantiate (enemyDeathEffect, other.transform.position, other.transform.rotation);
}
//Instantiate(impactEffect, other.transform.position, other.transform.rotation);
if (other.name == "player") {
return;
}
Destroy (gameObject);
}
}
I instantiate the bullet using my player controller script. On it, I have a GameObject variable called firePoint, which is an empty GameObject situated at the center of my player sprite. I also have a GameObject variable in which I drag the prefab of my bullet for the player to access.
For now, the bullet goes upwards.
public Transform firePoint; public GameObject friendlyBullet;
public bool hasGun;
public bool savedAnn;
public int memoriesCollected;
public bool nextToAnn;
public bool nextToMemory;
public bool memoryExists;
// Use this for initialization
void Start () {
playerBody = this.gameObject.GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update () {
//go right
if (Input.GetKey (KeyCode.D)) {
playerBody.AddForce(new Vector2 (speed, 0));
//transform.position += new Vector3 (speed * Time.deltaTime, 0.0f, 0.0f);
}
//go left
if (Input.GetKey (KeyCode.A)){
playerBody.AddForce(new Vector2 (-speed, 0));
//transform.position -= new Vector3 (speed * Time.deltaTime, 0.0f, 0.0f);
}
//go up
if (Input.GetKey (KeyCode.W)){
playerBody.AddForce(new Vector2 (0, speed));
//transform.position += new Vector3 (0.0f,speed * Time.deltaTime, 0.0f);
}
//go up
if (Input.GetKey (KeyCode.S)){
playerBody.AddForce(new Vector2 (0, -speed));
//transform.position -= new Vector3 (0.0f,speed * Time.deltaTime, 0.0f);
} if (Input.GetKeyDown (KeyCode.Space) && (hasGun) ) {
Instantiate (friendlyBullet, firePoint.position, firePoint.rotation);
}
}
Any pointers as to where to integrate the mouse position would be greatly appreciated!
Answer by Jason2014 · Mar 19, 2016 at 10:19 PM
var Rotation : Quaternion = Quaternion.LookRotation (transform.position - Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector3.forward);
transform.rotation = Rotation;
transform.eulerAngles = Vector3 (0, 0, transform.eulerAngles.z);
This works perfectly for aiming by mouse. Insert it in FixedUpdate function at start.
Additionally let me share you some another tips. Firstly, NEVER use GetComponent in Update function! Update function do instructions 60 frames per second, so you are getting access to the same component 60 times per second. Much better is to insert component to variable as reference and then access it in Awake or Start (Awake do instructions once even when script is disabled, but Start do instructions once when script IS enabled). Secondly you can define movement by AddForce much simpler. Look at this:
PlayerRigidbody2D.AddForce (Vector2 (Input.GetAxis ("Horizontal") * MovementSpeed, Input.GetAxis ("Vertical") * MovementSpeed));
And the last advice. All code defining physics insert in FixedUpdate function. It works as normal Update, but it is counted by "fixed rate". See documentation for further details.
Hi! First off, thank you for your suggestion and advice. I just had some concerns about the 'var' and ':' sign in the code. Both are seen as errors in my script. If I'm correct, these do not work in c#, but it may be something else entirely. Here is what it looks like:
void FixedUpdate () {
var Rotation : Quaternion = Quaternion.LookRotation (transform.position - Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector3.forward);
transform.rotation = Rotation;
transform.eulerAngles = Vector3 (0, 0, transform.eulerAngles.z);
}
Of course, sorry for this mistake. You're right, I'm currently using UnityScript. I didn't noticed you are using C#. Looks like this:
Quaternion Rotation = Quaternion.LookRotation (transform.position - Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector3.forward);
But I assume you will get error for assigning value to transform.rotation. Unfortunately, C# doesn't allow to change values directly, you have to use "temporary variables". You have to first declare at start transform.rotation in a variable for example: private Quaternion playerRotation and then assign value in Start function. Same thing as eulerAngles. You can try as I wrote earlier, but I'm was using C# and I know it usually throws an error, while trying to change value directly. Sorry again for mistake!