private void Awake() { // Assign to components characterController = GetComponent <CharacterController>(); OriginalHeight = characterController.height; bulletPistol = GetComponent <BulletPistol>(); //gunAS = GetComponent<AudioSource>(); animator = GetComponent <Animator>(); weaponWheel = GetComponent <WeaponWheel>(); }
public void PlayerAttack() { // Set forward to actually project forwards Vector3 forward = Camera.main.transform.forward; //transform.TransformDirection(Vector3.forward); // Set the origin as the GameObject created earlier, Pivot Vector3 origin = PlayerPivot.transform.position; Debug.Log("weaponChoice " + (int)weaponWheel.weaponChoice); selectedWeapon = weapons[(int)weaponWheel.weaponChoice]; // CODE FOR MELEE ATTACKS // Check using a weapon if (selectedWeapon != null) { randomAttack = Random.Range(1, numberOfAttacks + 1); // Play animation selectedWeapon.SetTrigger("Attack" + randomAttack); // If using any melee weapons fire this raycast to attack: if ((int)weaponWheel.weaponChoice == 1 || (int)weaponWheel.weaponChoice == 2) { // Create a new raycasthit RaycastHit hit; if (Physics.Raycast(origin, forward, out hit, playerMeleeRange)) { { if ((hit.transform.gameObject.tag == "AI")) { //Send info to AI hit.transform.gameObject.SendMessage("TakeDamage", damageTable[(int)weaponWheel.weaponChoice]); } } } } // SHOOTING PISTOL if ((int)weaponWheel.weaponChoice == 3 && !cantShoot) { // Shoot bullet from pistol barrel BulletPistol newBullet = Instantiate(bulletPrefab.gameObject).GetComponent <BulletPistol>(); // For shooting at the center of the screen Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); //Make the new bullet start at camera newBullet.transform.position = pistolBarrel.transform.position; //Set bullet direction newBullet.SetDirection(ray.direction); // Decrease ammo count _currentAmmo--; // Play particle effects // Play sound } // SHOOTING SHOTGUN if ((int)weaponWheel.weaponChoice == 4 && !cantShoot) { // Used to spawn multiple bullets cartridgeSize = new BulletPistol[6]; // Create random numbers for shell spread int spreadX = Random.Range(-35, 36); int spreadY = Random.Range(-35, 36); // For shooting at the center of the screen Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition + new Vector3(spreadX, spreadY, 0)); // FOR EVERY SHELL for (int i = 0; i < cartridgeSize.Length; i++) { // Spawn shell BulletPistol newShell = Instantiate(shotgunPrefab.gameObject).GetComponent <BulletPistol>(); //Make the new shell start at camera newShell.transform.position = shotgunBarrel.transform.position; //Set shell direction newShell.SetDirection(ray.direction); //Assign array indexes to each shell created cartridgeSize[i] = newShell; // Reset spread values spreadX = Random.Range(-50, 51); spreadY = Random.Range(-50, 51); // Alter next shell's direction ray = mainCamera.ScreenPointToRay(Input.mousePosition + new Vector3(spreadX, spreadY, 0)); } // Decrease ammo count _currentAmmo--; // Play particle effects // Play sound } } }