void Fire() { if (_isObscured) return; _weapon?.Fire(_camera.transform.position, _camera.transform.forward); if (_displayDebugRays) Debug.DrawRay(_camera.transform.position, _camera.transform.forward * 10000f, Color.red, 5f); }
// Update is called once per frame protected override void Update() { if (References.alarmManager.AlarmHasSounded()) { alerted = true; } if (References.thePlayer != null) { Vector3 playerPosition = References.thePlayer.transform.position; Vector3 toPlayer = playerPosition - transform.position; myLight.color = Color.white; if (alerted) { ChasePlayer(); myLight.color = Color.red; if (CanSeePlayer()) { secondsSeeingPlayer += Time.deltaTime; transform.LookAt(playerPosition); if (secondsSeeingPlayer >= reactionTime) { myWeapon.Fire(playerPosition); } } else { secondsSeeingPlayer = 0; } } else { if (navAgent.remainingDistance < 0.5f) { GoToRandomNavPoint(); } // Checking if player is seen if (Vector3.Distance(playerPosition, transform.position) <= visionRange) { if (Vector3.Angle(transform.forward, toPlayer) <= visionConeAngle) { if (!Physics.Raycast(transform.position, toPlayer, toPlayer.magnitude, References.wallsLayer)) { alerted = true; References.alarmManager.SoundTheAlarm(); } } } } } }
// Update is called once per frame void Update() { // WASD to move Vector3 inputVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); Rigidbody ourRigidBody = GetComponent <Rigidbody>(); ourRigidBody.velocity = inputVector * speed; // Map cursor to playing plane Ray rayFromCameraToCursor = Camera.main.ScreenPointToRay(Input.mousePosition); Plane playerPlane = new Plane(Vector3.up, transform.position); playerPlane.Raycast(rayFromCameraToCursor, out float distanceFromCamera); Vector3 cursorPosition = rayFromCameraToCursor.GetPoint(distanceFromCamera); // Look at target position before moving Vector3 lookAtPosition = cursorPosition; transform.LookAt(lookAtPosition); // Click to fire if (mainWeapon != null && Input.GetButton("Fire1")) { mainWeapon.Fire(cursorPosition); } // Change weapon if (Input.GetButtonDown("Fire2")) { SwitchWeapon(); } // Use the nearest usable if (Input.GetButtonDown("Use")) { Useable nearestUseable = null; float nearestDistance = 3; // Max pickup distance foreach (var thisUseable in References.useables) { float thisDistance = Vector3.Distance(transform.position, thisUseable.transform.position); if (thisDistance <= nearestDistance) { nearestUseable = thisUseable; nearestDistance = thisDistance; } } if (nearestUseable != null) { nearestUseable.Use(); } } }
public void Control() { if (_weaponInput.IsAttack()) { _currentWeapon.Fire(); } if (_weaponInput.PreviousWeapon()) { ChangeWeapon(-1); } if (_weaponInput.NextWeapon()) { ChangeWeapon(1); } }
private void CheckForFire() { Collider[] hitColliders = Physics.OverlapSphere(transform.position, overlapRadius); if (hitColliders.Length > 0) { foreach (Collider col in hitColliders) { if (col.gameObject.layer.Equals(9)) { Vector3 target = (col.transform.position - transform.position).normalized; TowerHead.transform.rotation = Quaternion.LookRotation(target, transform.up); weapon.Fire(); } } } }