// Update is called once per frame void Update() { // left mouse button click if (playerScript) { if (Input.GetButtonDown("Fire1") | Input.GetButton("Fire1")) { if (Time.time > lastFireTime + 1 / frequency) { // Spawn visual bullet //Quaternion tempRot = Quaternion.LookRotation(playerScript.GetMouseOnPlane() - muzzlePosition.position , Vector3.forward); //tempRot.z = 0f; //tempRot.w = 0.5f; Quaternion tempRot = transform.parent.transform.rotation; Quaternion coneRandomRotation = Quaternion.Euler(Random.Range(-coneAngle, coneAngle), Random.Range(-coneAngle, coneAngle), 0); GameObject go = (GameObject)Instantiate(bulletPrefab, muzzlePosition.position, tempRot * coneRandomRotation); SimpleBullet bullet = go.GetComponent <SimpleBullet> (); muzzleParticle.Emit(1); lastFireTime = Time.time; // Find the object hit by the raycast RaycastHit hitInfo = new RaycastHit(); Physics.Raycast(transform.position, transform.parent.forward, out hitInfo, 100); if (hitInfo.transform) { // Get the health component of the target if any HealthHandler targetHealth = hitInfo.transform.GetComponent <HealthHandler> (); if (targetHealth & (hitInfo.transform.root != transform.root)) { // Apply damage targetHealth.DeductHealth(Mathf.FloorToInt(damagePerSecond / frequency)); } // Get the rigidbody if any if (hitInfo.rigidbody) { // Apply force to the target object at the position of the hit point Vector3 force = transform.forward * (forcePerSecond / frequency); hitInfo.rigidbody.AddForceAtPosition(force, hitInfo.point, ForceMode.Impulse); } bullet.dist = hitInfo.distance; } else { bullet.dist = 1000; } } } } }
public static bool DeductHealth(Component entity, int health) { HealthHandler handler = entity.GetComponent <HealthHandler>(); if (handler) { return(handler.DeductHealth(health)); } return(false); }
void Update() { if (target) { myAgent.destination = target.position; if (healtScript) { if (Vector3.Distance(target.transform.position, transform.position) <= myAgent.stoppingDistance + 0.1 && Time.time > lastAttack + 1) { healtScript.DeductHealth(damagePerSecond); lastAttack = Time.time; } } } }