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 Yoshi Moto · May 19, 2016 at 05:00 PM · c#scripting problemplayergameobjects

Get player and enemy hit bars to respond to each others attack not their own

Hello, I have got halfway though my project game but I have came across the issue of making the player attack enemies but hit the right Health-bars for different enemies, at the moment everything in my game hits itself and I think it has something to do with my health.CurrentVal, I have been trying to understand this for days now and this is kinda a last resort.

I started by following this tutorial ( https://www.youtube.com/watch?annotation_id=annotation_146566169&feature=iv&src_vid=NgftVg3idB4&v=1lrkgdENfqM ) and I have kind of adapted these to suit my needs.

I think the problem is with this script (AttackBaddie and AttackPlayer)

the player has (AttackBaddie)

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class AttackBaddie : MonoBehaviour
 {
 
 
     [SerializeField]
     public Stat health;
    
    GameObject player;
     bool BaddieInRange;
     float timer; 
 
 
     public void Start()
     {
         timer = 1f;
     }
 
 
     public void Awake()
     {
         health.Initialize();
     }
 
    void OnTriggerEnter (Collider other)
         {
         if (other.transform.tag == "baddie")
             {
             BaddieInRange = true;
            Debug.Log("In Range");
             }
         }
         
         
         void OnTriggerExit (Collider other)
         {
 
         if (other.transform.tag == "baddie")
         {
             BaddieInRange = false;
            Debug.Log("NOT In Range");
 
             }
         }
 
 
     void Update()
     {
         timer += Time.deltaTime;
 
         if  (BaddieInRange == true && (Input.GetKeyDown("space")))
         {
             Debug.Log("Space is HERE!");
 
             Attack();
         }
         
     }
 
 
     public void Attack()
     {
         
         if (timer >= 2f)
 
         { 
             //GameObject.FindWithTag("baddie").GetComponent<AttackPlayer>().health.CurrentVal -= 10;
             health.CurrentVal -= 10;
             Debug.Log("OW This Hurts! Baddie 1");
             // Reset the timer.
             timer = 0f;
         }
 
 
         if  (health.CurrentVal <= 5)
         {
 
             Destroy (GameObject.FindWithTag("baddie"));    
         }
     }
 }
 

and the enemy has (AttackPlayer)

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
     public class AttackPlayer : MonoBehaviour  {
 
 
         [SerializeField]
         public Stat health;
 
         public float timeBetweenAttacks = 1f;
          
         GameObject baddie;
         bool playerInRange;                      
         float timer;          
 
     public void Start(){
     
         timer = 2f;
 
     }
 
 
     public void Awake()
     {
         health.Initialize();
     }        
 
         void OnTriggerEnter (Collider other)
         {
         if (GameObject.FindWithTag("Player"))
             {
                 playerInRange = true;
             }
         }
         
         
         void OnTriggerExit (Collider other)
         {
         if (GameObject.FindWithTag("Player"))
             {
                 playerInRange = false;
 
             }
         }
         
         
         void Update ()
         {
             timer += Time.deltaTime;
 
             if(timer >= timeBetweenAttacks && playerInRange == true)
             {
                 Attack ();
             }
         }
         
         
         public void Attack ()
         {
             
            if(timer >= 4f) //
 
             {       
             //I think the problem is here
             health.CurrentVal -= 10;
 
             timer = 0f;
         }
 
         }
 
 }
 

Could someone point me in the right direction to solve it.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by $$anonymous$$ · May 20, 2016 at 06:51 AM

I'm not sure I fully understand your code but in the OnTriggerEnter of the enemy you search if there is a player anywhere in your game and if true you say he is in range. But what if an enemy triggers OnTriggerEnter by another enemy or something else? Shouldn't you be checking if the other variable is the player?

Comment
Add comment · Share
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

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 to create a child object inside the parent (script) and assign independent variables? 0 Answers

Adjust the size of the object within a parent container 0 Answers

Save/Load Player System 0 Answers

Player not moving in the right direction instantly 1 Answer

How do i ajust the player height acording to the height of the roof in a first person game? 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