Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Player inCombat state problem

$
0
0
**Hey guys!** I have problem with combat state in my game. Currently it works like this: Enemy “see player” bool return true and send message to other script which turn “isInCombat” to true. I’ve fixed previous problems so now when ANY enemy see player, player is in combat, BUT now the problem is this, player stays in combat all the time even no enemy see him. Can someone help? **Scripts:** **Player Controller:** using UnityEngine; public class PlayerController : MonoBehaviour { public bool isInCombat; EnemyAI enemyAI; Targeting t; void Start () { enemyAI = FindObjectOfType(); t = FindObjectOfType(); } void Update () { CombatState(); } void CombatState () { foreach (Transform enemy in t.targets) { if (enemyAI.seePlayer) { isInCombat = true; } else { isInCombat = false; } } } } **Enemy AI:** using UnityEngine; using System.Collections; public class EnemyAI : MonoBehaviour { public float moveSpeed; //brzina pomeranja public float rotationSpeed; //brzina rotacije public float range; //okrug napada public float minDistance = 50f; //minimalna distanca na koju enemy moze prici playeru private float distance; //trenutna distanca enemia i playera public bool isOnMinDistance; //da li je enemy na minimalnoj distanci od playera public bool seePlayer; //da li je player u combatu public Transform player; //player transform private Transform myTransform;//enemy transform private Transform playerCamera; public string parameter; public int runID; public int idleID; public PlayerController playerController; private Animator anim; void Awake () { myTransform = transform;//postavljam transform } void Start () { //anim = GetComponent (); GameObject go = GameObject.FindGameObjectWithTag ("Player"); //trazim playera player = go.transform; //postavljam transform playera playerController = FindObjectOfType(); } // Update is called once per frame void Update () { FollowAndAttack (); } void FollowAndAttack() { distance = Vector3.Distance (player.transform.position, myTransform.transform.position); if (distance < minDistance) { //attack isOnMinDistance = true; seePlayer = true; // anim.SetInteger ("SkellyPar", 2); } else if (distance > minDistance && distance < range) { Debug.DrawLine (player.position, myTransform.position, Color.red); //animation run //anim.SetInteger (parameter, runID); //look at target myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation (player.position - myTransform.position), rotationSpeed * Time.deltaTime); //follow target myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; isOnMinDistance = false; seePlayer = true; } else { //anim.SetInteger (parameter, idleID); seePlayer = false; isOnMinDistance = false; } } } **Targeting:** using UnityEngine; using System.Collections; using System.Collections.Generic; public class Targeting : MonoBehaviour { public List targets; public Transform selectedTarget; public Transform myTransform; CombatTargetUI ct; void Start () { targets = new List(); selectedTarget = null; myTransform = transform; ct = FindObjectOfType(); AddAllEnemies(); } public void AddAllEnemies () { GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy"); foreach (GameObject enemy in go) { AddTarget(enemy.transform); } } public void AddTarget (Transform enemy) { targets.Add(enemy); } void SortTargetsByDistance () { targets.RemoveAll(target => target == null); targets.Sort(delegate(Transform t1, Transform t2) { return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position)); }); } public void TargetEnemy () { if (selectedTarget == null) { SortTargetsByDistance(); selectedTarget = targets[0]; } else { int index = targets.IndexOf(selectedTarget); if (index < targets.Count - 1) { index++; } else { index = 0; } DeselectTarget(); selectedTarget = targets[index]; } SelectTarget(); } void SelectTarget () { ct.ShowUI(); selectedTarget.GetComponent().material.color = Color.red; BasicAttack ba = (BasicAttack)GetComponent("BasicAttack"); ba.target = selectedTarget.gameObject; } public void DeselectTarget () { if (selectedTarget != null) { selectedTarget.GetComponent().material.color = Color.white; } selectedTarget = null; } void Update () { if (Input.GetKeyDown(KeyCode.Tab)) { TargetEnemy(); } } } **I have made this foreach loop to go through enemies and check if any of them see player, but player stays in combat.**

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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