First of all, sorry for posting this because I know that it has been asked several times before, but none of the answers seem to be working. Being a beginner in making guns, I am asking for help.
Here is the player code
public class PlayerMovement : MonoBehaviour
{
public float speed;
private Vector3 mouse_pos;
public Transform target;
private Vector3 object_pos;
private float angle;
public GameObject bullet;
public Transform firePoint;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
mouse_pos = Input.mousePosition;
mouse_pos.z = -10;
object_pos = Camera.main.WorldToScreenPoint(target.position);
mouse_pos.x = mouse_pos.x - object_pos.x;
mouse_pos.y = mouse_pos.y - object_pos.y;
angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, 0, angle);
if (Input.GetKey(KeyCode.W))
{
transform.position += Vector3.up * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.S))
{
transform.position += Vector3.down * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.D))
{
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.A))
{
transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetMouseButtonDown(0))
{
Instantiate(bullet, firePoint.position, gameObject.transform.rotation);
}
}
}
And here is the bullet code
public class Bullet : MonoBehaviour
{
public float speed;
public Transform player;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position = new Vector2(transform.position.x + 0.25f * speed, transform.position.y);
}
}
If you got any suggestions, please tell me. I really want to get this working to improve my skills.
Thanks in advance!