For some unknown reason i just cant get raycast to actually collide with something or it might not even properly made.
public class Shooting : MonoBehaviour
{
//Titik awal projectile di tembakkan
public Transform firePoint;
//Prefab untuk projectile
public GameObject bulletPrefab;
public float bulletForce = 20f;
public int damage = 20;
//Method yang akan dipanggil Input System saat tombol kiri mouse ditekan
public void onShooting(InputAction.CallbackContext context)
{
if (context.started)
{
Shoot();
}
}
//Method yang dipanggil pada onShooting
void Shoot()
{
//Var bullet untuk projectile yang akan ditembakkan. Mengambil prefab, dan menggunakan data lokasi beserta rotasi dari firePoint
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
//Mengambil rigidbody dari projectile yg ditembakkan
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
//Mengatur arah bergerakknya projectile yang ditembakkan
rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
//LINE ASAL
RaycastHit2D collision = Physics2D.Raycast(firePoint.position, firePoint.up);
Debug.DrawRay(firePoint.position, firePoint.up, Color.red);
Enemy enemy = collision.transform.GetComponent<Enemy>();
if (enemy != null)
{
enemy.TakeDamage(damage);
}
}
// Update is called once per frame
void Update()
{
}
}
I even used Debug.DrawRay to see the raycast but it doesnt even show anything (below), its supposed to be in front of the brown tank.
Any help is okay. I kinda need this for my assignment, thank you.
