so im trying to add the Fire 1 so that the player could shoot out a bullet but im having a problem getting it to work the line is if (Input.GetButtonDown ("Fire 1")); and it's not really working. any ideas? Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class weapon : MonoBehaviour { public Transform firePoint; public int damage = 40; public GameObject impactEffect; // Update is called once per frame void Update(); { if(Input.GetButtonDown ("Fire 1")); { shoot(); } void shoot() { RaycastHit2D hitInfo = Physics2D.Raycast(firePoint.position, firePoint.right); if (hitInfo) { Enemy enemy = hitInfo.transform.GetComponent<enemy>(); if (enemy != null) { enemy.TakeDamage(damage); } Instantiate(impactEffect, hitInfo.point, Quaternion.identity); } }
It's also quite possible that your raycast is hitting the object it's supposed to come out from (or some other incorrect object). I recommend you use Physics2D.RaycastAll() instead of Physics2D.Raycast() and loop through all of the hit objects to see if any of them are enemies.
You are also missing a curly bracket at line 18. I recommend looking for errors in your code editor and Unity's console, those type of mistakes are really easy to fix alone. Debug.Log() is your friend too, it can help check what code blocks are going through or not, making it easier for you to pinpoint what could be the issue.
You've got a semicolon after your Update method definition and your if statement, and you're missing two closing curly braces. You've also defined a variable of type Enemy but then try to assign it using "GetComponent<enemy>". Case matters there. Makes sure every single opening curly brace has a matching closing one. You should observe C# standards and capitalize your class names and function names. Here is your code with those issues fixed: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class Weapon : MonoBehaviour { public Transform firePoint; public int damage = 40; public GameObject impactEffect; // Update is called once per frame private void Update() { if (Input.GetButtonDown("Fire 1")) { Shoot(); } } private void Shoot() { RaycastHit2D hitInfo = Physics2D.Raycast(firePoint.position, firePoint.right); if (hitInfo.collider != null) { Enemy enemy = hitInfo.transform.GetComponent<Enemy>(); if (enemy != null) { enemy.TakeDamage(damage); } Instantiate(impactEffect, hitInfo.point, Quaternion.identity); } } }