Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Vekosava · Jun 30, 2016 at 02:32 PM · playerenemycombatstates

Player inCombat state problem

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<EnemyAI>();
         t = FindObjectOfType<Targeting>();
     }
 
     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<Animator> ();
 
         GameObject go = GameObject.FindGameObjectWithTag ("Player"); //trazim playera
 
         player = go.transform; //postavljam transform playera
 
         playerController = FindObjectOfType<PlayerController>();
     }
 
     // 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<Transform> targets;
     public Transform selectedTarget;
 
     public Transform myTransform;
 
     CombatTargetUI ct;
 
     void Start () {
         targets = new List<Transform>();
         selectedTarget = null;
         myTransform = transform;
 
         ct = FindObjectOfType<CombatTargetUI>();
 
         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<Renderer>().material.color = Color.red;
 
         BasicAttack ba = (BasicAttack)GetComponent("BasicAttack");
         ba.target = selectedTarget.gameObject;
     }
 
     public void DeselectTarget () {
         if (selectedTarget != null) {
             selectedTarget.GetComponent<Renderer>().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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can I get my enemy to hurt/kill player 0 Answers

Enemy Isn't Hurting Player in Survival Shooter 0 Answers

2D How to get enemy to face the player while moving 0 Answers

Nothing is happening with these scripts, why? 0 Answers

Character continues moving when locked onto an enemy 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges