- Home /
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!
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.
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!
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#
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 ..
Your answer
Follow this Question
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