I am making a 2D game in which the arm of the player points toward the pointer. but the projectiles move in only x axis, instead of the direction of the arm.
this is the code for the gun (child of arm)
using UnityEngine;
public class Gun : MonoBehaviour
{
public Transform FirePoint;
public GameObject Bullet;
private void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
Instantiate(Bullet, new Vector3(FirePoint.position.x, FirePoint.position.y, 0), Quaternion.identity);
}
}
}
and this is the code on the bullet prefab(i know its bad; i’m new):
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float speed = 30f;
public GameObject BulletSelf;
public Rigidbody2D rb;
private void Start()
{
rb.velocity = new Vector2(speed, 0f);
}
private void OnCollisionEnter2D(Collision2D other)
{
Destroy(BulletSelf);
}
}
I am using brackeys’ tutorial for the rotation of the arm.
i can provide further info if needed.