Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

why this script don't work in my second mob(enemy ai)?

Discussion in 'Scripting' started by Riukensay, Mar 4, 2018.

  1. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    hi, yes I know that I have asked many questions but I don't have other solution X.x
    I followed a tutorial in youtube of Enemy movement and everything was going well until I put the script in a second mob... I checked all but I'm incapable of find the error :/



    and the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class Enemigo : MonoBehaviour
    7. {
    8.     public LayerMask enemyMask;
    9.     public float speed = 1;
    10.      Rigidbody2D myBody;
    11.      Transform myTrans;
    12.     float myWidth, myHeight;
    13.     public int hp;
    14.     public int maxHp;
    15.     Animator anim;
    16.     public Respaw res;
    17.  
    18.  
    19.     void Start ()
    20.     {
    21.         myTrans = this.transform;
    22.         myBody = this.GetComponent<Rigidbody2D>();
    23.         SpriteRenderer mySprite = this.GetComponent<SpriteRenderer>();
    24.         myWidth = mySprite.bounds.extents.x;
    25.         myHeight = mySprite.bounds.extents.y;
    26.         anim = this.GetComponent<Animator> ();
    27.         hp = maxHp;
    28.     }
    29.  
    30.     void FixedUpdate ()
    31.     {
    32.  
    33.         //Use this position to cast the isGrounded/isBlocked lines from
    34.         Vector2 lineCastPos = myTrans.position.toVector2() - myTrans.right.toVector2() * myWidth + Vector2.up * myHeight;
    35.         //Check to see if there's ground in front of us before moving forward
    36.         Debug.DrawLine(lineCastPos, lineCastPos + Vector2.down);
    37.         bool isGrounded = Physics2D.Linecast(lineCastPos, lineCastPos + Vector2.down, enemyMask);
    38.         //Check to see if there's a wall in front of us before moving forward
    39.         Debug.DrawLine(lineCastPos, lineCastPos - myTrans.right.toVector2() * .05f);
    40.         bool isBlocked = Physics2D.Linecast(lineCastPos, lineCastPos - myTrans.right.toVector2() * .05f, enemyMask);
    41.  
    42.         //If theres no ground, turn around. Or if I hit a wall, turn around
    43.         if(!isGrounded || isBlocked)
    44.         {
    45.             Vector3 currRot = myTrans.eulerAngles;
    46.             currRot.y += 180;
    47.             myTrans.eulerAngles = currRot;
    48.         }
    49.  
    50.         //Always move forward
    51.         Vector2 myVel = myBody.velocity;
    52.         myVel.x = -myTrans.right.x * speed;
    53.         myBody.velocity = myVel;
    54.         Animaciones ();
    55.  
    56.         Debug.Log (myBody.velocity.x);
    57.         Debug.Log (myBody.velocity.y);
    58.     }
    59.  
    60.     void Animaciones()
    61.     {
    62.         if (myBody.velocity != Vector2.zero) {
    63.             anim.SetFloat ("X", myBody.velocity.x);
    64.             anim.SetFloat ("Y", myBody.velocity.y);
    65.             anim.SetBool ("Walking", true);
    66.         } else {
    67.             anim.SetBool ("Walking", false);
    68.         }
    69.     }
    70.  
    71.     void OnCollisionEnter2D(Collision2D col)
    72.     {
    73.         int Dmg = 2;
    74.         Debug.Log (col.ToString());
    75.         if(col.gameObject.CompareTag("Jugador"))
    76.         {
    77.             col.gameObject.GetComponent<Controles>().Attacked(Dmg);
    78.         }
    79.     }
    80.  
    81.     public void EnemyAttacked(int dmg)
    82.     {
    83.  
    84.         hp = hp - dmg;
    85.         if (hp<=0) {
    86.             res.SendMessage("RespawnMob");
    87.             Destroy (gameObject);
    88.         }
    89.     }
    90. }
    91.  
     

    Attached Files:

  2. mehdimoji

    mehdimoji

    Joined:
    Oct 18, 2017
    Posts:
    22
    What's going on here? What is wrong? What should to be?
     
    Riukensay likes this.
  3. Deleted User

    Deleted User

    Guest

    That behaviour looks like the following if statement is repeatedly being called.
    Code (CSharp):
    1. //If theres no ground, turn around. Or if I hit a wall, turn around
    2. if(!isGrounded || isBlocked)
    3. {
    4. }
    I would add a debug message to confirm this.
    Something such as.
    Code (CSharp):
    1. Debug.Log("Is grounded: " + isGrounded);
    Using this you can see if the mob thinks its on the ground or not.
     
    Lethn likes this.
  4. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    The script is to move the enemy and make a raycast to detect the ground and blocks, if blocking or the ground end the enemy rotate until a block way or the other end of ground and repeat the behaviour(sorry for my bad english :/)
     
  5. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    Ok now I'm turning on my pc c:
     
  6. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    :O the skeleton debug for console is grounded: false and I don't know why, he have the sames parameters of spider(the spider have the same script and its realize the correct behaviour X.x
     
    Lethn likes this.
  7. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    Someone know why this don't work?
     
  8. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,004
    Code (csharp):
    1. bool isGrounded = Physics2D.Linecast(lineCastPos, lineCastPos + Vector2.down, enemyMask);
    You are casting from the top of the sprite, one unit down (Vector2.down). I'm guessing the skeleton is taller than 1 unit, so the cast doesn't go far enough to reach the ground.
     
  9. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    ohhhh ty, when i back to my house I will try it, thanks you c:, You know How to know the measures of the skeleton?
     
  10. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,004
    ?
    You're already getting the measurements.
    Code (csharp):
    1. myWidth = mySprite.bounds.extents.x;
    2. myHeight = mySprite.bounds.extents.y;
    I am not even sure why you're checking if it's grounded though. You only want it to turn around if it's blocked right? Why do you care if it's on the ground or not?
     
  11. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    is to when onGrounded=false then mob will turn 180degrees, this is to when mob are on a plataform
     
  12. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    but i can "fixed" it


    but the "ray" don't take all height of skeleton(look in the Scene window)

    I'm using
    Code (CSharp):
    1.         Vector2 lineCastPos = myTrans.position.toVector2() - myTrans.right.toVector2() * myWidth;
    2.  
    3.         //Check to see if there's ground in front of us before moving forward
    4.         Debug.DrawLine(lineCastPos, lineCastPos + Vector2.down);
    5.         bool isGrounded = Physics2D.Linecast(lineCastPos, lineCastPos + Vector2.down, enemyMask);
    6.         //Check to see if there's a wall in front of us before moving forward
    7.         Debug.DrawLine(lineCastPos, lineCastPos - myTrans.right.toVector2() * .05f);
    8.         bool isBlocked = Physics2D.Linecast(lineCastPos, lineCastPos - myTrans.right.toVector2() * .05f, enemyMask);
     
  13. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,004
    One unit is probably still not enough to reach the ground.

    try : bool isGrounded = Physics2D.Linecast(lineCastPos, lineCastPos + Vector2.down*1.5f, enemyMask);

    If that works at least we are sure that's the issue.
     
  14. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    it remains the same as before :/
     
  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Do you want an enemy mask for detecting the ground?
     
  16. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,004
    Even with *2f? *10f?
     
  17. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    yes, even with that
     
  18. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    Hi i remember you c: , again help me :eek: xd

    I think that enemy mask is where the enemy don't detected collisions
     
  19. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm pretty sure your line cast will never true false, because it's returning a struct. You should really be checking the colliders that you hit (or if the collider is null, when just getting 1 result).

    As for what's going on beyond that, it's hard to say without some debugging.
     
  20. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    Oh sorry I belived that I up a photo of my console when I checked this(of true and false) tomorrow when I back to my house I will up a screen capture of Console debuging onGrounded ( and as always sorry for my bad english)
     
    Last edited: Mar 8, 2018
  21. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49

    here is debug of onGrounded
     
  22. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    It wasn't that I didn't believe what you said, just as far as I know/knew literally the return result won't ever be 'false'.. so that is odd to me.

    I guess this is still unresolved for you, eh?

    I could take a look at your project if you can make a simple reproduction of just the character and the script - or whatever minimal amount of "things" are required to reproduce it. :) As a unity package you post in the thread, I mean.
     
  23. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    I don't know how do it, but i know how add you in Colaborate unity, it is same?
    edit: ok i Attached a file with the "things" that I think that you need to run my project(is's almost everything xd)
     
    Last edited: Mar 10, 2018
  24. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I don't see an attachment. If you make it really small you can post it here. I suppose you could add me to collaborate. If it's larger, you might have to post it on DropBox/OneDrive or something..sorry it's still stuck for you.
     
  25. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    uhhh that is rare I can see it :/

    but I try add you in calaborate too
     
  26. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sorry there, I didn't realize it was way up in the thread. I found it. lol
     
  27. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    lol i troight that, this thread is very long, it looks like a chat lol
     
  28. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, so there are a bunch of warnings if I die.. but how do I get in the position you're stuck at? I don't see the tall red guy glitching out at all...?
     
  29. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    oh I fixed this lol I trought u said about the Heing of "skeleton" xd
     
  30. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm sorry, what? You fixed the character being weird turning back n forth?

    What am I looking at then? lol :)
     
  31. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    yeah lol i thinkig u looking the "raycast" withe line in skeleton (to take his height)
     
  32. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    Is there any way to close the thread? maybe we get into a confusion xdd
    edit: and the problem was fixed
     
  33. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ah okay, cool. Well, I'm glad it's fixed.

    I just learned something , too - the bool operator may be overloaded to check if the collider isn't null, as the docs say users should do. So, that's cool.

    Anyways, threads don't get closed, but I can close your package ;) Take care.
     
  34. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    ok thank you very much
     
  35. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cheers. And thanks to you for inspiring me to check that overload based on this conversation lol. :)