IF
- the user presses and releases the F key,
- the player distance from the enemy is less than 2
- the enemy is in front of the player (on a rotation scale of -1 to 1, "Think of Quadrants on XY Graph")
- Player will attack and decrease enemy Health by 10.
1: using UnityEngine;
2: using System.Collections;
3: public class PlayerAttack : MonoBehaviour
4: {
5: public GameObject target;
6: void Start ()
7: {
8: }
9: void Update ()
10: {
11: if (Input.GetKeyUp(KeyCode.F))
12: {
13: Attack();
14: }
15: }
16: private void Attack()
17: {
18: float distance = Vector3.Distance (target.transform.position, transform.position);
19: Vector3 dir = (target.transform.position - transform.position).normalized;
20: float direction = Vector3.Dot (dir, transform.forward);
21: Debug.Log (direction);
22: if (distance < 2)
23: {
24: if (direction > 0)
25: {
26: EnemyHealth eh = (EnemyHealth)target.GetComponent ("EnemyHealth");
27: eh.AdjustCurrentHealth (-10);
28: }
29: }
30: }
31: }
No comments:
Post a Comment