Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Wakeful · Jul 25, 2014 at 12:06 AM · c#2dontriggerentertagother

AI Attack Not Working! W/Video

Please watch the video, then look at the scripts! Hope you can fix my problem! Video:

https://www.youtube.com/watch?v=O6vpwZ1pqco

Weapon Stats:

 using UnityEngine;
 using System.Collections;
 
 public class WeaponStats : MonoBehaviour {
     public bool damaging;
     public bool attacking;
     public int weaponShortSwordMinDamage;
     public int weaponShortSwordMaxDamage;
     Animator anim;
     public int attachedToWhat;
     int addTorque;
     public bool dropped;
 
 
 
     void Start () {
         
         anim = GetComponent<Animator>();
     }
     
 
     void Update () {
         
         if (Input.GetMouseButtonDown(0) && attachedToWhat == 0)
         {
             anim.SetTrigger("Attack");
         }
         if (attachedToWhat == 2)
         {
             if(transform.parent.GetComponent<BasicAI_1>().attackObject == true)
             anim.SetTrigger("Attack");
         }
 
         if (transform.root.tag == "Player" && transform.root.tag != "Enemy"){
             attachedToWhat = 0;
             collider2D.isTrigger = true;
             anim.enabled = true;
             rigidbody2D.isKinematic = true;
             rigidbody2D.interpolation = RigidbodyInterpolation2D.None;
             dropped = false;
         }
 
         if (transform.root.tag == "Enemy" && transform.root.tag != "Player"){
             attachedToWhat = 2;
             collider2D.isTrigger = true;
             anim.enabled = true;
             rigidbody2D.isKinematic = true;
             rigidbody2D.interpolation = RigidbodyInterpolation2D.None;
             dropped = false;
         }
 
         if (transform.root.tag != "Player" && transform.root.tag != "Enemy"){
             attachedToWhat = 1;
             collider2D.isTrigger = false;
             anim.enabled = false;
             rigidbody2D.isKinematic = false;
             rigidbody2D.interpolation = RigidbodyInterpolation2D.Interpolate;
             Physics2D.IgnoreLayerCollision(14, 15);
             if(rigidbody2D.rotation == 0)
                 rigidbody2D.AddTorque(1000);
             dropped = true;
         }
 
     }
     
     void attackingFalse(){
         damaging = false;
         attacking = false;
     }
     void attackingTrue(){
         attacking = true; 
     }
     
 }

AttackCollider Script:

 using UnityEngine;
 using System.Collections;
 
 public class AttackCollider : MonoBehaviour {
 
     public Transform weapon;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     void OnTriggerEnter2D(Collider2D other) {
         if (other.tag == "Enemy" && weapon.gameObject.GetComponent<WeaponStats>().attacking == true && other is BoxCollider2D && weapon.gameObject.GetComponent<WeaponStats>().attachedToWhat == 0)
         {
             weapon.gameObject.GetComponent<WeaponStats>().damaging = true;
             
         }
         if (other.tag == "Player" && weapon.gameObject.GetComponent<WeaponStats>().attacking == true && other is BoxCollider2D && weapon.gameObject.GetComponent<WeaponStats>().attachedToWhat == 2)
         {
             weapon.gameObject.GetComponent<WeaponStats>().damaging = true;
             
         }
         
     }
 }
 

OrcEnemy Script:

 using UnityEngine;
 using System.Collections;
 
 public class orcEnemy : MonoBehaviour {
     Animator anim;
     public int health = 100;
 
     //Gets the Stats gameobject
     void Awake ()
     {
         anim = GetComponent<Animator>();
     }
     
     
     //Used to see if the Orc is dead
     void FixedUpdate () 
     {
         if(health < 1)
         {
             anim.SetTrigger("Dead");
         }
     }
     
     
     void OnTriggerEnter2D(Collider2D other) {
         if (other.tag == "Weapon"){
             if (other.gameObject.GetComponent<WeaponStats>().damaging == true && other.gameObject.GetComponent<WeaponStats>().attachedToWhat == 0)
         {
             health = health - Random.Range(other.gameObject.GetComponent<WeaponStats>().weaponShortSwordMinDamage, other.gameObject.GetComponent<WeaponStats>().weaponShortSwordMaxDamage);
             Debug.Log("Orc Health " + health);
             other.gameObject.GetComponent<WeaponStats>().damaging = false;
 
             }
         }
 
     }
 
 }
 

Player Controller Script:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     public float maxSpeed = 10f;
     bool facingRight = true;
 
     Animator anim;
 
     bool grounded = false;
     public Transform groundCheck;
     public Transform pushDownObject;
     float groundRadius = 0.2f;
     public LayerMask whatIsGround;
     public float jumpForce = 700f;
     public float pushDown;
     bool pushDownCheck;
     public int health = 100;
 
 
     // Use this for initialization
     void Start () {
         anim = GetComponent<Animator>();
     }
     
     // Update is called once per frame
     void FixedUpdate () {
 
         grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
         anim.SetBool("Ground", grounded);
 
         anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
 
         float move = Input.GetAxis ("Horizontal");
 
         anim.SetFloat("Speed", Mathf.Abs(move));
 
         pushDownCheck = Physics2D.OverlapCircle(pushDownObject.position, groundRadius, whatIsGround);
 
         rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
 
         if(move > 0 &&!facingRight)
             Flip ();
         else if(move < 0 && facingRight)
             Flip ();
     }
 
     void Update(){
         if(grounded && Input.GetKeyDown(KeyCode.W))
         {
             anim.SetBool("Ground", false);
             rigidbody2D.AddForce(new Vector2(0, jumpForce));
         }
     }
     void OnTriggerStay2D(Collider2D other) {
         if(other.name == "GroundEdges" && pushDownCheck == false){
 
         rigidbody2D.velocity = new Vector2(0, -pushDown);
         //Debug.Log("PushDown");
         }
 
     }
 
     void Flip()
     {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
     void OnTriggerEnter2D(Collider2D other) {
         //Debug.Log("Player Health1 " + health);
         if (other.tag == "Weapon"){
             Debug.Log("Player Health2 " + health);
             if (other.gameObject.GetComponent<WeaponStats>().damaging == true && other.gameObject.GetComponent<WeaponStats>().attachedToWhat == 2)
             {
                 health = health - Random.Range(other.gameObject.GetComponent<WeaponStats>().weaponShortSwordMinDamage, other.gameObject.GetComponent<WeaponStats>().weaponShortSwordMaxDamage);
                 Debug.Log("Player Health3 " + health);
                 other.gameObject.GetComponent<WeaponStats>().damaging = false;
                 
             }
         }
         
     }
 
 }

Basic AI script:

 using UnityEngine;
 using System.Collections;
 
 public class BasicAI_1 : MonoBehaviour {
     
     public float walkSpeed = 5f;
     public float runSpeed = 10f;
     public int mood;
     bool facingRight = true;
     public Transform sightStart, sightEnd, attackEnd, vFlip, flipSensor;
     public float move;
     public bool vFlipToggle;
     public bool flipCheck;
 
 
     Animator anim;
 
     public bool grounded = false;
     public Transform groundCheck;
     float groundRadius = 0.2f;
     float flipCheckRadius = 0.01f;
     public LayerMask whatIsGround;
     public LayerMask whatIsPlayer;
     public bool spotted;
     public bool attackObject;
 
 
     // Use this for initialization
     void Start () {
         anim = GetComponent<Animator>();
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         Debug.DrawLine(sightStart.position, sightEnd.position, Color.cyan);
         Debug.DrawLine(sightStart.position, vFlip.position, Color.green);
         Debug.DrawLine(sightStart.position, attackEnd.position, Color.yellow);
 
         anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
 
         anim.SetFloat("Speed", Mathf.Abs(move));
         move = rigidbody2D.velocity.x;
 
         grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
         anim.SetBool("Ground", grounded);
 
 
         spotted = Physics2D.Linecast (sightStart.position, sightEnd.position, whatIsPlayer);
         vFlipToggle = Physics2D.Linecast (sightStart.position, vFlip.position, whatIsPlayer);
         attackObject = Physics2D.Linecast (attackEnd.position, sightStart.position, whatIsPlayer);
 
         flipCheck = Physics2D.OverlapCircle(flipSensor.position, flipCheckRadius, whatIsGround);
 
         if(spotted == true) 
             mood = 1;
         
         if(mood == 0){
             patrol ();
         }
         if(mood == 1){
             attackMove(); 
         }
 
     }
     void Flip()
     {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
     void patrol()
     {
         if(mood == 0)
             if(facingRight == true && grounded == true)
                 {
             rigidbody2D.velocity = new Vector2(walkSpeed, 0);
             
                 }
             if(facingRight == false && grounded == true)
                 {
             rigidbody2D.velocity = new Vector2(-walkSpeed, 0);
             
                 }
             if(flipCheck == false && grounded == true){
             Flip();
         }
     }
     void attackMove()
     {
         if(vFlipToggle == true && spotted == false){    
             Flip();
         }
         if(spotted == true && facingRight == true && grounded == true && flipCheck == true && attackObject == false){
             rigidbody2D.velocity = new Vector2(runSpeed, 0);
             
         }
         if(spotted == true && facingRight == false && grounded == true&& flipCheck == true && attackObject == false){
             rigidbody2D.velocity = new Vector2(-runSpeed, 0);
         }
     
 
     }
 }



Hope you can help!

Comment
Add comment · Show 3
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
avatar image rutter · Jul 25, 2014 at 12:07 AM 1
Share

7 $$anonymous$$utes of video and multiple screens of source code?

No short description of problem?

Sounds like you need to make a short, self contained, correct example. Strip out code that isn't part of the problem. Understanding the problem well enough to do that might even help you to fix the problem.

As it stands, you're asking volunteers to put in an awful lot of work for you.

avatar image Wakeful · Jul 25, 2014 at 01:33 AM 0
Share

Thx merry_christmas you helped me fix my problem! if you repost what you commented in an answer i'll gladly accept it! Thx again!

avatar image jmgek · Jul 25, 2014 at 01:42 AM 0
Share

Also, my advice. I would stop using tags. When building out unity has to add another refrence to the objects.

Tags are not native c#

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ByteSheep · Jul 25, 2014 at 12:33 AM

Through the use of debug statements you already noticed that the line

 if (other.tag == "Weapon"){


is returning false and not being executed.

What you can do now is add a debug statement to the OnTriggerEnter2D function of the playerController script that debugs info about the collision. E.g.

 Debug.Log("Player was hit by " + other.tag);

This will help you figure out what is wrong ..

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a way to find other's components using OnTriggerEnter? 1 Answer

OnTriggerEnterProblem 1 Answer

OnTriggerEnter2D with multiple objects 0 Answers

OnTriggerEnter 1 Answer

Checking Tag of Trigger Prefab 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