- Home /
2D Top Down Shooter Shooting Problems
so in my game the player rotates to face the mouse but i want to make it so that when i click or something a bullet is created and it shoots towards that direction im facing heres my move.rotate script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Playermove : MonoBehaviour {
public float moveSpeed = 2;
// Update is called once per frame
void Update () {
if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
{
transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
}
if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
{
transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
}
facemouse();
}
void facemouse ()
{
Vector3 mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
Vector2 direction = new Vector2(
mousePos.x - transform.position.x,
mousePos.y - transform.position.y
);
transform.up = direction;
}
}
Answer by UnityVeris · Nov 10, 2017 at 03:14 AM
You need to either instantiate a bullet object, or grab a bullet object from a pool that you've previously set up. The bullet object needs to inherent the player objects transform.forward Vector as it's "direction of fire".
If you don't understand that paragraph then I suggest you read about instantiation in Unity.
Your answer
Follow this Question
Related Questions
shooting in 2d problem 0 Answers
Top down 2d shooting - XZ rotation 0 Answers
How to make Enemy shoot at Player Top Down 1 Answer
Top Down 2D movement issue 1 Answer
Instantiate after setting values? 0 Answers