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

Enemy Not Working Due To Raycast i think

Discussion in 'Scripting' started by Aquazombie, Nov 7, 2020.

  1. Aquazombie

    Aquazombie

    Joined:
    Nov 5, 2020
    Posts:
    11
    Hello this is my first time posting for help so im new to this so sorry if any mistakes. I found a script to pickup items and throw them but when ever I push the pickup button my enemy stops attacking and wont attack again. im new to coding and all I no is I think my enemy has raycast on it and so douse my pickup script I will try to post the script. thank you for any help
    .
    this is the player grab script
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class Grabber : MonoBehaviour
    5. {
    6.  
    7.     public bool grabbed;
    8.     RaycastHit2D hit;
    9.     public float distance = 2f;
    10.     public Transform holdpoint;
    11.     public float throwforce;
    12.     public LayerMask notgrabbed;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.        
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         if (Input.GetKeyDown(KeyCode.Z))
    24.         {
    25.  
    26.             if (!grabbed)
    27.             {
    28.                 Physics2D.queriesStartInColliders = false;
    29.  
    30.  
    31.                hit = Physics2D.Raycast(transform.position, Vector2.right * transform.localScale.x,distance);
    32.  
    33.                 if(hit.collider!=null && hit.collider.tag=="Grabbable")
    34.                 {
    35.                     grabbed = true;
    36.                 }
    37.  
    38.                 //grab
    39.  
    40.             }
    41.             else if(!Physics2D.OverlapPoint(holdpoint.position,notgrabbed))
    42.             {
    43.                 grabbed = false;
    44.  
    45.                 if(hit.collider.gameObject.GetComponent<Rigidbody2D>()!=null)
    46.                 {
    47.                     hit.collider.gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(transform.localScale.x, 1) * throwforce;
    48.                 }
    49.                 //throw
    50.             }
    51.                
    52.  
    53.         }
    54.  
    55.         if (grabbed)
    56.             hit.collider.gameObject.transform.position = holdpoint.position;
    57.     }
    58.  
    59.     void OnDrawGizmos()
    60.     {
    61.         Gizmos.color = Color.green;
    62.  
    63.         Gizmos.DrawLine(transform.position, transform.position + Vector3.right * transform.localScale.x * distance);
    64.     }
    65. }

    this is my enemy script.
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Security.Cryptography;
    5. using UnityEngine;
    6.  
    7. public class Enemy_behaviour : MonoBehaviour
    8. {
    9.     #region public Variables
    10.     public Transform rayCast;
    11.     public LayerMask raycastMask;
    12.     public float rayCastLength;
    13.     public float attackDistance; //Minimum distance for attack
    14.     public float moveSpeed;
    15.     public float timer; // time of cooldown between attack
    16.     public Transform leftLimit;
    17.     public Transform rightLimit;
    18.     public LayerMask playerLayers;
    19.     public float attackRange = 0.5f;
    20.     #endregion
    21.  
    22.     #region Private Variables
    23.     private RaycastHit2D hit;
    24.     private Transform target;
    25.     private Animator anim;
    26.     private float distance; // store the distance b/w enemy and player
    27.     private bool attackMode;
    28.     private bool inRange; //cheack if player is in range
    29.     private bool cooling; //cheack if enemy is cooling after attack
    30.     private float intTimer;
    31.     #endregion
    32.  
    33.     void Awake()
    34.     {
    35.         SelectTarget();
    36.         intTimer = timer; // store the intital value of timer
    37.         anim = GetComponent<Animator>();
    38.     }
    39.  
    40.     void Update ()
    41.     {
    42.         if (!attackMode)
    43.         {
    44.             Move();
    45.         }
    46.  
    47.         if(!InsideofLimits() && !inRange && !anim.GetCurrentAnimatorStateInfo(0).IsName("Enemy_attack"))
    48.         {
    49.             SelectTarget();
    50.         }
    51.  
    52.        if (inRange)
    53.         {
    54.             hit = Physics2D.Raycast(rayCast.position, transform.right, rayCastLength, raycastMask);
    55.             RaycastDebugger();
    56.         }
    57.  
    58.        //when Player is detected
    59.        if(hit.collider != null)
    60.         {
    61.             EnemyLogic();
    62.         }
    63.        else if(hit.collider == null)
    64.         {
    65.             inRange = false;
    66.         }
    67.  
    68.        if(inRange == false)
    69.         {
    70.             StopAttack();
    71.         }
    72.     }
    73.  
    74.     void OnTriggerEnter2D(Collider2D trig)
    75.     {
    76.         if(trig.gameObject.tag == "Player")
    77.         {
    78.             target = trig.transform;
    79.             inRange = true;
    80.             Flip();
    81.         }
    82.     }
    83.  
    84.     void EnemyLogic()
    85.     {
    86.         distance = Vector2.Distance(transform.position, target.transform.position);
    87.  
    88.         if (distance > attackDistance)
    89.         {
    90.             StopAttack();
    91.         }
    92.         else if(attackDistance >= distance && cooling == false)
    93.         {
    94.             Attack();
    95.         }
    96.  
    97.         if (cooling)
    98.         {
    99.             anim.SetBool("Attack", false);
    100.         }
    101.     }
    102.  
    103.     void Move()
    104.     {
    105.         anim.SetBool("canRun", true);
    106.  
    107.         if(!anim.GetCurrentAnimatorStateInfo(0).IsName("Skel_attack"))
    108.         {
    109.             Vector2 targetPosition = new Vector2(target.position.x, transform.position.y);
    110.  
    111.             transform.position = Vector2.MoveTowards(transform.position, targetPosition, moveSpeed * Time.deltaTime);
    112.         }
    113.     }
    114.  
    115.     public void Attack()
    116.     {
    117.         timer = intTimer; //Reset Timer When Player enter Attack Range
    118.         attackMode = true; //To Check if Enemy can still attack or not
    119.  
    120.         anim.SetBool("canRun", false);
    121.         anim.SetBool("Attack", true);
    122.  
    123.        
    124.     }
    125.  
    126.     void StopAttack()
    127.     {
    128.         cooling = false;
    129.         attackMode = false;
    130.         anim.SetBool("Attack", false);
    131.     }
    132.  
    133.     void RaycastDebugger()
    134.     {
    135.         if(distance > attackDistance)
    136.         {
    137.             Debug.DrawRay(rayCast.position, transform.right * rayCastLength, Color.red);
    138.         }
    139.         else if(attackDistance > distance)
    140.         {
    141.             Debug.DrawRay(rayCast.position, transform.right * rayCastLength, Color.green);
    142.         }
    143.     }
    144.  
    145.  
    146.     public void TriggerCooling()
    147.     {
    148.         cooling = true;
    149.     }
    150.  
    151.     private bool InsideofLimits()
    152.     {
    153.         return transform.position.x > leftLimit.position.x && transform.position.x < rightLimit.position.x;
    154.     }
    155.  
    156.     private void SelectTarget()
    157.     {
    158.         float distanceToLeft = Vector2.Distance(transform.position, leftLimit.position);
    159.         float distanceToRight = Vector2.Distance(transform.position, rightLimit.position);
    160.  
    161.         if(distanceToLeft > distanceToRight)
    162.         {
    163.             target = leftLimit;
    164.         }
    165.         else
    166.         {
    167.             target = rightLimit;
    168.         }
    169.  
    170.         Flip();
    171.     }
    172.  
    173.     private void Flip()
    174.     {
    175.         Vector3 rotation = transform.eulerAngles;
    176.         if(transform.position.x > target.position.x)
    177.         {
    178.             rotation.y = 180f;
    179.         }
    180.         else
    181.         {
    182.             rotation.y = 0f;
    183.         }
    184.  
    185.         transform.eulerAngles = rotation;
    186.     }
    187.  
    188.    
    189. }

    I hope I did it right. thanks for any help
     
  2. Xhitman

    Xhitman

    Joined:
    Oct 30, 2015
    Posts:
    452
    You may use Debug.Log(hit) in enemy script to find out what the racyast actually hit.

    Btw, I am working on AI for my game right now. It is easy to find out which part it is suck by looking at which mode it stay forever.

    Code (CSharp):
    1.  private void FixedUpdate()
    2.     {
    3.  
    4.         switch (_aiMode)
    5.         {
    6.             case AIMode.Search:
    7.  
    8.                 if(_robotControl.actionMode == RobotControl.UnitActionMode.Idle && _attackCDTimer + attackCD <=Time.time)
    9.                     RadarScan();                                              
    10.  
    11.                 break;
    12.             case AIMode.Move:
    13.                              
    14.                 DistanceControl();
    15.  
    16.  
    17.                 break;
    18.             case AIMode.WaitAttack:
    19.  
    20.                 if (_robotControl.IdleAnimation)
    21.                 {
    22.                     _aiMode = AIMode.Search;
    23.                     _attackCDTimer = Time.time;
    24.                 }
    25.  
    26.                 break;
    27.         }
    28.  
    29.     }
     
  3. Aquazombie

    Aquazombie

    Joined:
    Nov 5, 2020
    Posts:
    11
    thank you for replying =D honestly I don't know what is happening but I will put everything I know. I put a pickup script on my player that when I push Z the player will pickup the crate. if I hit play and walk up to the enemy he will attack me and everything works he plays the animations and everything but when I push Z even if I don't pickup anything the enemy will just walk into me and not bather to attack as if the player is not there but also if I pick up the crate and drop it he will not attack. so meaning when I push Z the enemy will stop attacking me and just walk around. but when I take the script off the player he will attack and work fine. ive been trying to find a different pickup and throw script but no luck. And im trying to learn to code but I just started and struggle at times. thank you for the help =D
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,580
    As you been advised, learn to use Debug.Log method.
    Also, learn to use VS breakpoints for debugging.
     
  5. Aquazombie

    Aquazombie

    Joined:
    Nov 5, 2020
    Posts:
    11
    I shell try. I will probably fail for a while but I shell try lol
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,580
    It is nothing wrong with failing. As long you are persistent and eventually lead to solving a problem.
     
  7. Aquazombie

    Aquazombie

    Joined:
    Nov 5, 2020
    Posts:
    11
    =) I know how that feels I started unity and coding 2 mounts ago and i failed at so much and had to learn how to fix it but now I failed again lol and saw you could ask questions on unity forums and decided to try it out =D
     
    Antypodish likes this.