Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need help fixing this sript

Discussion in 'Scripting' started by ToastHatter, Dec 7, 2017.

  1. ToastHatter

    ToastHatter

    Joined:
    Nov 11, 2016
    Posts:
    53
    I have created a new script for an assist character in my game, he's supposed to chase down the opponent if they're close enough and attack them when they're closer.

    The script works fine however he doesn't move at all, the Moving Bool is activated when the opponent is close enough, i know this because of the animation attached to it.

    This script is an amalgamation of several other scripts that work, including the movement section. However it doesn't work, there are no errors that prevent me from going into play mode.

    Here's the code, if you notice an error anywhere that may be preventing the script from allowing him to move please inform me.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AssistRush : MonoBehaviour {
    6.  
    7.     private Vector3 v_diff;
    8.     public float atan2;
    9.     public bool P1;
    10.     public Transform target;
    11.     public float distance;
    12.     public bool right;
    13.     public bool Grounded;
    14.     public bool Moving;
    15.     public Animator anim;
    16.     public Rigidbody2D rb2d;
    17.     public bool Attacking;
    18.     public float Speed;
    19.     public float maxSpeed;
    20.  
    21.  
    22.     // Update is called once per frame
    23.     void Update () {
    24.        
    25.         anim.SetBool ("Grounded", Grounded);
    26.         anim.SetBool ("Moving", Moving);
    27.         anim.SetBool ("Attack", Attacking);
    28.  
    29.         Vector3 newScale = transform.localScale;
    30.  
    31.  
    32.         if (P1 == false) {
    33.             target = GameObject.FindWithTag ("Player").transform;
    34.         }
    35.         if (P1 == true) {
    36.             target = GameObject.FindWithTag ("Player2").transform;
    37.         }
    38.         v_diff = (target.position - transform.position);  
    39.         atan2 = Mathf.Atan2 (0f, v_diff.x);
    40.         if (atan2 >= 0.1f) {
    41.             newScale.x = -1.2f;
    42.             right = false;
    43.         } else {
    44.             newScale.x = 1.2f;
    45.             right = true;
    46.         }
    47.  
    48.         transform.localScale = newScale;
    49.  
    50.         distance = (target.position - transform.position).magnitude;
    51.  
    52.         if (distance <= 5 && Attacking == false) {
    53.             StartCoroutine ("Attack");
    54.         }
    55.  
    56.         if (distance <= 10 && distance >= 7) {
    57.             Moving = true;
    58.         } else {
    59.             Moving = false;
    60.         }
    61.  
    62.  
    63.  
    64.     }
    65. // movement
    66.     void FixedUpdate(){
    67.         if (Moving == true) {
    68.             if (right == true) {
    69.                 rb2d.AddForce ((Vector2.right * Speed) * 1f);
    70.             } else {
    71.                 rb2d.AddForce ((Vector2.left * Speed) * 1f);
    72.             }
    73.         }
    74.  
    75.         if (rb2d.velocity.x > maxSpeed) {
    76.             rb2d.velocity = new Vector2 (maxSpeed, rb2d.velocity.y);
    77.         }
    78.         if (rb2d.velocity.x < -maxSpeed) {
    79.             rb2d.velocity = new Vector2 (-maxSpeed, rb2d.velocity.y);
    80.         }
    81.        
    82.        
    83.     }
    84.  
    85.     void OnTriggerEnter2D(Collider2D other)
    86.     {
    87.         if (other.gameObject.CompareTag ("Ground")) {
    88.             Grounded = true;
    89.         }
    90.            
    91.  
    92.         if (other.gameObject.CompareTag ("CPU_Jump")) {
    93.             rb2d.AddForce ((Vector2.up * 350f));
    94.         }
    95.            
    96.     }
    97.     void onTriggerStay2D(Collider2D other){
    98.         if (other.gameObject.CompareTag ("Ground")) {
    99.             Grounded = true;
    100.         }
    101.     }
    102.  
    103.     void OnTriggerExit2D(Collider2D other)
    104.     {
    105.         if (other.gameObject.CompareTag ("Ground")) {
    106.             Grounded = false;
    107.         }
    108.     }
    109.  
    110.     IEnumerator Attack(){
    111.        
    112.         Attacking = true;
    113.         yield return new WaitForSeconds (1f);
    114.         Attacking = false;
    115.  
    116.     }
    117. }
    118.  
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,228
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The first things I would look at is if rb2d is set to the correct rigidbody2d, and if maxSpeed is a high enough value. If maxSpeed is 0 then your object will never move, as the x velocity will just keep getting reset to 0. If rb2d is set to the wrong rigidbody2d then nothing to this object will happen. If the value is just set very low, then you may just not notice it moving when it actually is moving.

    Also, Moving will only be true as long as distance remains between 7 and 10. That seems like a pretty small window, and depending on how fast the target is moving you might only have Moving true for a single frame.

    Throw Debug.Log messages everywhere, play with some of these values, and see what is going on.