Quantcast
Channel: Questions in topic: "error message"
Viewing all articles
Browse latest Browse all 2891

Why does my Melee System not work?

$
0
0
So i made an melee system but i cant hit my enemy? i keep getting an null error... Here is my Melee Script: using System.Collections; using System.Collections.Generic; using UnityEngine; public class MeleeSystem : MonoBehaviour { public int minDamage = 25; public int maxDamage = 50; public float weaponRange = 3.5f; public Camera FPSCamera; private TreeHealth treeHealth; private AdvancedEnemyAI enemyAI; private void Update() { Ray ray = FPSCamera.ScreenPointToRay(new Vector2 (Screen.width / 2, Screen.height / 2)); RaycastHit hitInfo; if (Input.GetKeyDown (KeyCode.Mouse0)) { if (Physics.Raycast (ray, out hitInfo, weaponRange)) { if (hitInfo.collider.tag == "Tree") { treeHealth = hitInfo.collider.GetComponentInParent (); AttackTree (); } else if (hitInfo.collider.tag == "Enemy") { enemyAI = hitInfo.collider.GetComponent (); AttackEnemy (); } } } } private void AttackTree() { int damage = Random.Range (minDamage, maxDamage); treeHealth.health -= damage; } private void AttackEnemy() { int damage = Random.Range(minDamage, maxDamage); enemyAI.TakeDamage(damage); } } private void AttackTree() { int damage = Random.Range (minDamage, maxDamage); treeHealth.health -= damage; } private void AttackEnemy() { int damage = Random.Range(minDamage, maxDamage); enemyAI.TakeDamage(damage); } } And here is my Enemy Script: using System.Collections; using System.Collections.Generic; using UnityEngine; public class AdvancedEnemyAI : MonoBehaviour { public int health = 100; public float viewRange = 25f; public float attackRange = 5f; public float thinkTimer = 5f; private float thinkTimerMin = 5f; private float thinkTimerMax = 15f; public float randomUnitCircleRadius = 25f; public float eyeHeight; public bool isChasing = false; public Transform playerTransform; private UnityEngine.AI.NavMeshAgent agent; private void Start() { agent = GetComponent (); thinkTimer = Random.Range(thinkTimerMin, thinkTimerMax); } private void Update() { Vector3 eyePosition = new Vector3 (transform.position.x, transform.position.y + eyeHeight, transform.position.z); Ray ray = new Ray (eyePosition, transform.forward); RaycastHit hitInfo; CheckHealth (); thinkTimer -= Time.deltaTime; if (thinkTimer <= 0) { Think (); thinkTimer = Random.Range(thinkTimerMin, thinkTimerMax); } if (Physics.Raycast (ray, out hitInfo, viewRange)) { if (hitInfo.collider.tag == "Player") { if (isChasing == false) { isChasing = true; if (playerTransform == null) { playerTransform = hitInfo.collider.GetComponent (); } } } } if (Physics.Raycast (ray, out hitInfo, attackRange)) { // Attacking Here } Debug.DrawRay (ray.origin, ray.direction * viewRange, Color.red); if (isChasing == true) { agent.SetDestination (playerTransform.position); } } public void TakeDamage(int damage) { health -= damage; if (playerTransform != null) { isChasing = true; } Debug.Log ("Enemy took damage and now has " + health + " health!"); } private void CheckHealth() { if(health <= 0) { Destroy (gameObject); } } private void Think() { if (isChasing == false) { Vector3 newPos = transform.position + new Vector3 (Random.insideUnitCircle.x * randomUnitCircleRadius, transform.position.y, Random.insideUnitCircle.y * randomUnitCircleRadius); agent.SetDestination (newPos); } } } What is the problem?

Viewing all articles
Browse latest Browse all 2891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>