Search Unity

How to desactivate script of external gameObject

Discussion in '2D' started by mexicanlefty, Nov 26, 2019.

  1. mexicanlefty

    mexicanlefty

    Joined:
    Oct 17, 2019
    Posts:
    7
    So im starting my small game by following a book tutorial and some youtube videos, i have a player character that has a sword childObject, which i have attached a knockback script to:

    Code (CSharp):
    1. public class knockback : MonoBehaviour
    2. {
    3.  
    4.     public float thrust;
    5.  
    6.     public float knockTime;
    7.  
    8.  
    9.     private void OnTriggerEnter2D(Collider2D other)
    10.     {
    11.         if(other is BoxCollider2D && other.gameObject.CompareTag("Enemy"))
    12.         {
    13.             Rigidbody2D Enemy = other.GetComponent<Rigidbody2D>();
    14.            
    15.             if (Enemy != null)
    16.             {
    17.                 Enemy.isKinematic = false;
    18.                
    19.                 Vector2 difference = Enemy.transform.position - transform.position;
    20.                 difference = difference.normalized * thrust;
    21.                 Enemy.AddForce(difference, ForceMode2D.Impulse);
    22.                 StartCoroutine(KnockCo(Enemy));
    23.              
    24.  
    25.             }
    26.         }
    27.     }
    28.  
    29.     private IEnumerator KnockCo(Rigidbody2D Enemy)
    30.     {
    31.         if(Enemy != null)
    32.         {
    33.            
    34.             yield return new WaitForSeconds(knockTime);
    35.             Enemy.velocity = Vector2.zero;
    36.             Enemy.isKinematic = true;
    37.            
    38.         }
    39.     }
    40. }
    So via a tag my it register when it collides with an enemy, my enemy has a script as component called Wander, which is what makes it move and follow the player:

    Code (CSharp):
    1. [RequireComponent(typeof(Rigidbody2D))]
    2. [RequireComponent(typeof(CircleCollider2D))]
    3. [RequireComponent(typeof(Animator))]
    4.  
    5. public class Wander : MonoBehaviour
    6. {
    7.     public float pursuitSpeed;
    8.     public float wanderSpeed;
    9.     float currentSpeed;
    10.  
    11.     public float directionChangeInterval;
    12.  
    13.     public bool followPlayer;
    14.     Coroutine moveCoroutine;
    15.     Rigidbody2D rb2d;
    16.  
    17.     Animator animator;
    18.     Transform targetTransform = null;
    19.  
    20.     Vector3 endPosition;
    21.  
    22.     float currentAngle = 0;
    23.  
    24.     CircleCollider2D circleCollider;
    25.  
    26.     // Start is called before the first frame update
    27.     void Start()
    28.     {
    29.         animator = GetComponent<Animator>();
    30.         currentSpeed = wanderSpeed;
    31.  
    32.         rb2d = GetComponent<Rigidbody2D>();
    33.  
    34.         circleCollider = GetComponent<CircleCollider2D>();
    35.  
    36.         StartCoroutine(WanderRoutine());
    37.     }
    38.  
    39.     // Update is called once per frame
    40.     void Update()
    41.     {
    42.         Debug.DrawLine(rb2d.position, endPosition, Color.red);  
    43.     }
    44.  
    45.     public IEnumerator WanderRoutine()
    46.     {
    47.         while(true)
    48.         {
    49.             ChooseNewEndpoint();
    50.  
    51.             if(moveCoroutine != null)
    52.             {
    53.                 StopCoroutine(moveCoroutine);
    54.             }
    55.  
    56.             moveCoroutine = StartCoroutine(Move(rb2d, currentSpeed));
    57.  
    58.             yield return new WaitForSeconds(directionChangeInterval);
    59.         }
    60.     }
    61.  
    62.     void ChooseNewEndpoint()
    63.     {
    64.         currentAngle += Random.Range(0, 360);
    65.  
    66.         currentAngle = Mathf.Repeat(currentAngle, 360);
    67.  
    68.         endPosition += Vector3FromAngle(currentAngle);
    69.     }
    70.  
    71.     Vector3 Vector3FromAngle(float inputAngleDegrees)
    72.     {
    73.         float inputAngleRadians = inputAngleDegrees * Mathf.Deg2Rad;
    74.         return new Vector3(Mathf.Cos(inputAngleRadians), Mathf.Sin(inputAngleRadians), 0);
    75.     }
    76.  
    77.     public IEnumerator Move(Rigidbody2D rigidBodyToMove, float speed)
    78.     {
    79.         float remainingDistance = (transform.position - endPosition).sqrMagnitude;
    80.  
    81.         while(remainingDistance > float.Epsilon)
    82.         {
    83.             if(targetTransform != null)
    84.             {
    85.                 endPosition = targetTransform.position;
    86.             }
    87.  
    88.             if(rigidBodyToMove != null)
    89.             {
    90.                 animator.SetBool("isWalking", true);
    91.  
    92.                 Vector3 newPosition = Vector3.MoveTowards(rigidBodyToMove.position, endPosition, speed * Time.deltaTime);
    93.  
    94.                 rb2d.MovePosition(newPosition);
    95.  
    96.                 remainingDistance = (transform.position - endPosition).sqrMagnitude;
    97.  
    98.             }
    99.  
    100.             yield return new WaitForFixedUpdate();
    101.         }
    102.  
    103.         animator.SetBool("isWalking", false);
    104.     }
    105.  
    106.     void OnTriggerEnter2D(Collider2D collision)
    107.     {
    108.         if(collision.gameObject.CompareTag("Player") && followPlayer)
    109.         {
    110.             currentSpeed = pursuitSpeed;
    111.  
    112.             targetTransform = collision.gameObject.transform;
    113.  
    114.             if(moveCoroutine != null)
    115.             {
    116.                 StopCoroutine(moveCoroutine);
    117.             }
    118.  
    119.             moveCoroutine = StartCoroutine(Move(rb2d, currentSpeed));
    120.  
    121.         }
    122.     }
    123.  
    124.     void OnDrawGizmos()
    125.     {
    126.         if(circleCollider != null)
    127.         {
    128.             Gizmos.DrawWireSphere(transform.position, circleCollider.radius);
    129.         }
    130.     }
    131.  
    132.     void OnTriggerExit2D(Collider2D collision)
    133.     {
    134.         if(collision.gameObject.CompareTag("Player"))
    135.         {
    136.             animator.SetBool("isWalking", false);
    137.  
    138.             currentSpeed = wanderSpeed;
    139.  
    140.             if(moveCoroutine != null)
    141.             {
    142.                 StopCoroutine(moveCoroutine);
    143.             }
    144.  
    145.             targetTransform = null;
    146.         }
    147.     }
    148.  
    149.  
    150. }

    When i desactivate wander i can do the knockback no issue, so i want to implement a way to desactivate the wander script from the knockback class while the hit is done so it can do the push and then reactivate it.

    Also if someone can help me find out why the wander script makes it ignore the addforce push it will help me understand the issue, cant help it im a newbie lol.

    Thanks in advance.