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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

"foreach" and "}" and parsing error. Working on this code for my project.

Discussion in 'Scripting' started by SSINZ, Dec 7, 2015.

  1. SSINZ

    SSINZ

    Joined:
    Dec 2, 2015
    Posts:
    23
    voidStart ()
    {

    rig2d = GetComponent<Rigidbody2D> ();
    anim = GetComponentInChildren<Animator> ();

    jmpForce = JumpForce;
    GameObject[] players = GameObject.FindGameObjectsWithTag("Player")


    foreach(GameObjectplinplayers)
    {

    if (pl.transform != this.transform)
    {
    enemy = pl.transform;
    }
    }
    }

    voidUpdate()
    {
    AttackInput();
    ScaleCheck();
    OnGroundCheck();
    Damage();
    UpdateAnimator();
    }


    voidOnGroundCheck()
    {
    if (!onGround)
    {
    rig2d.gravityScale = 5;
    }
    else
    {
    rig2d.gravityScale = 1;
    }
    }

    voidFixedUpdate ()

    {

    horizontal = Input.GetAxis ("Horizontal" + PlayerNumber.ToString ());
    vertical = Input.GetAxis("Vertical" + PlayerNumber.ToString());

    Vector3movement = newVector3(horizontal, 0, 0);

    crouch = (vertical < -0.1f);

    if (vertical > 0.1f)
    {
    if (!jumpKey)
    {
    jmpDuration += Time.deltaTime;
    jmpForce += Time.deltaTime;

    if (jmpDuration < jumpDuration)
    {
    rig2d.velocity = newVector2(rig2d.velocity.x, jmpForce);
    }
    else
    {
    jumpKey = true;
    }
    }
    }

    if(!onGround && vertical < 0.1f)
    {
    falling = true;
    }

    if(attack[0] && !jumpKey || attack[1] && !jumpKey)
    {
    movement = Vector3.zero;
    }
    if (!crouch)
    rig2d.AddForce (movement * maxSpeed);
    else
    rig2d.velocity = Vector3.zero;
    }

    voidScaleCheck()
    {
    if(transform.position.x < enemy.position.x)
    transform.localScale = newVector3(-1,1,1);
    else
    transform.localScale = Vector3.one;
    }

    voidAttackInput()
    {
    if(Input.GetButtonDown("Attack1" + PlayerNumber.ToString()))
    {
    attack[0] = true;
    attacktimer[0] = 0;
    timesPressed[0] ++;
    }

    if(attack[0])
    {
    attacktimer[0] += Time.deltaTime;

    if(attacktimer[0] > attackRate || timesPressed[0] >=4)
    {
    attacktimer[0] = 0;
    attack[0] = false;
    timesPressed [0] = 0;
    }
    }


    if(Input.GetButtonDown("Attack2" + PlayerNumber.ToString()))
    {
    attack[1] = true;
    attacktimer[1] = 0;
    timesPressed[1] ++;
    }

    if(attack[1])
    {
    attacktimer[1] += Time.deltaTime;

    if(attacktimer[1] > attackRate || timesPressed[1] >=4)
    {
    attacktimer[1] = 0;
    attack[1] = false;
    timesPressed [1] = 0;
    }
    }
    }

    voidDamage()
    {
    {
    noDamageTimer += Time.deltaTime;

    if (noDamageTimer > noDamage)
    {
    damage = false;
    noDamgaeTimer = 0;
    }
    }

    //if(!onGround)
    //{
    //rig2d.gravityScale = 10;
    //Vector3dir = enemy.position - transform.position;
    //rig2d.AddForce(-dir * 25);
    //}

    }


    voidUpdateAnimator()
    {
    anim.SetBool ("Crouch", crouch);
    anim.SetBool ("OnGround", this.onGround);
    anim.SetBool ("Falling", this.falling);
    anim.SetFloat ("Movement", Mathf.Abs(horizontal));
    anim.SetBool ("Attack1", attack [0]);
    anim.SetBool ("Attack2", attack[1]);
    }

    voidOnCollisionEnter2D(Collision2Dcol)
    {

    if (col.collider.tag == "Ground")
    {
    onGround = true;

    jumpKey = false;
    jmpDuration = 0;
    jmpForce = JumpForce;
    falling = false;
    }

    }

    voidOnCollisionExit2D(Collision2Dcol)
    {
    if (col.collider.tag == "Ground")
    {
    onGround = false;
    }
    }
    }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    1) Use [code ]code tags[/code]
    2) The error message gives you a line number. We need that. We're not going to read this whole thing line by line for you.
     
  3. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    1. He's right, you need to use code tags properly.
    2. Didn't have to read the whole thing. Ctl+F (or Cmd+F) works wonders. After your foreach loop you have one too many "}".
     
  4. 3zzerland

    3zzerland

    Joined:
    Oct 31, 2014
    Posts:
    42
    This may not be true. It appears that he has an IF, a ForEach and a Start. That's 3 opening braces and 3 closing.
    It looks like the actual issue is this line:
    Code (csharp):
    1. GameObject[] players = GameObject.FindGameObjectsWithTag("Player")
    has no line ending ;

    Using proper code tags and giving us the error would help us identify the certainty.
     
  5. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
  6. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    Yes, found Waldo !
     
  7. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    foreach is one set of { } and if is another set. So he should only have two closing } but he has 3. BUT, you're right. If I re-read the thread's title, he's reporting two errors. So the first is the line ending, and the second is the one too many }'s.
     
    3zzerland likes this.
  8. 3zzerland

    3zzerland

    Joined:
    Oct 31, 2014
    Posts:
    42
    Void Start() is also a a set, which is what it appears his extra ending '}' is closing out, as the next line is a Update() function.
     
    DRRosen3 likes this.
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    I also just realized we're not even seeing the whole script file here...
     
  10. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    Damn! There's so much suspens in this thread, it could be a Night Shyamalan's movie!!
     
  11. 3zzerland

    3zzerland

    Joined:
    Oct 31, 2014
    Posts:
    42
    In second look, I think I found the error for the extra }, so it was two separate errors. I wont attempt to verify this though until the beach has been cleared and Waldo is visible :)
     
    Diablo404 and DRRosen3 like this.
  12. SSINZ

    SSINZ

    Joined:
    Dec 2, 2015
    Posts:
    23
    This?

    Assets/Scripts/PlayerControl.cs(1,1): Error CS8025: Parsing error (CS8025) (Assembly-CSharp)

    Assets/Scripts/PlayerControl.cs(8,8): Error CS1525: Unexpected symbol `}' (CS1525) (Assembly-CSharp)

    Assets/Scripts/PlayerControl.cs(8,8): Error CS1525: Unexpected symbol `}' (CS1525) (Assembly-CSharp)
     
  13. SSINZ

    SSINZ

    Joined:
    Dec 2, 2015
    Posts:
    23
     

    Attached Files:

    3zzerland likes this.
  14. 3zzerland

    3zzerland

    Joined:
    Oct 31, 2014
    Posts:
    42
    Yes, that. Can you post the whole script here in [code ][/code] tags so that we can get a better look? Also, did you fix the first error I pointed out?
     
  15. SSINZ

    SSINZ

    Joined:
    Dec 2, 2015
    Posts:
    23
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class PlayerControl : MonoBehaviour
    6. {
    7.    
    8.     public int PlayerNumber = 1;
    9.     Transform enemy;
    10.    
    11.     Rigidbody2D rig2d;
    12.     Animator anim;
    13.    
    14.     float horizontal;
    15.     float vertical;
    16.     public float maxSpeed = 25;
    17.     Vector3 movement;
    18.     bool crouch;
    19.    
    20.    
    21.     public float JumpForce = 20;
    22.     public float jumpDuration = .1f;
    23.     float jmpDuration;
    24.     float jmpForce;
    25.     bool jumpKey;
    26.     bool falling;
    27.     bool onGround;
    28.    
    29.     public float attackRate = 0.3f;
    30.         bool[] attack = new bool[2];
    31.         float[] attacktimer = new float[2];
    32.         int[] timesPressed = new int[2];
    33.  
    34.         public bool damage;
    35.         public bool noDamage = 1;
    36.         float noDamageTimer;
    37.    
    38.     void Start ()
    39.     {
    40.        
    41.         rig2d = GetComponent<Rigidbody2D> ();
    42.         anim = GetComponentInChildren<Animator> ();
    43.        
    44.         jmpForce = JumpForce;
    45.         GameObject[] players = GameObject.FindGameObjectsWithTag("Player")
    46.        
    47.  
    48.             foreach(GameObject pl in players)
    49.         {
    50.        
    51.             if (pl.transform != this.transform)
    52.             {
    53.                 enemy = pl.transform;
    54.             }
    55.         }
    56.     }
    57.    
    58.     void Update()
    59.     {
    60.         AttackInput();
    61.         ScaleCheck();
    62.         OnGroundCheck();
    63.         Damage();
    64.         UpdateAnimator();
    65.     }
    66.    
    67.  
    68.     void OnGroundCheck()
    69.     {
    70.         if (!onGround)
    71.         {
    72.             rig2d.gravityScale = 5;
    73.         }
    74.         else
    75.         {
    76.             rig2d.gravityScale = 1;
    77.         }
    78.     }
    79.    
    80.     void FixedUpdate ()
    81.        
    82.     {
    83.        
    84.         horizontal = Input.GetAxis ("Horizontal" + PlayerNumber.ToString ());
    85.         vertical = Input.GetAxis("Vertical" + PlayerNumber.ToString());
    86.        
    87.         Vector3 movement = newVector3(horizontal, 0, 0);
    88.        
    89.         crouch = (vertical < -0.1f);
    90.        
    91.         if (vertical > 0.1f)
    92.         {
    93.             if (!jumpKey)
    94.             {
    95.                 jmpDuration += Time.deltaTime;
    96.                 jmpForce += Time.deltaTime;
    97.                
    98.                 if (jmpDuration < jumpDuration)
    99.                 {
    100.                     rig2d.velocity = newVector2(rig2d.velocity.x, jmpForce);
    101.                 }
    102.                 else
    103.                 {
    104.                     jumpKey = true;
    105.                 }
    106.             }
    107.         }
    108.        
    109.         if(!onGround && vertical < 0.1f)
    110.         {
    111.             falling = true;
    112.         }
    113.  
    114.         if(attack[0] && !jumpKey || attack[1] && !jumpKey)
    115.         {
    116.             movement = Vector3.zero;
    117.         }
    118.         if (!crouch)
    119.             rig2d.AddForce (movement * maxSpeed);
    120.         else
    121.             rig2d.velocity = Vector3.zero;
    122.     }
    123.  
    124.     void ScaleCheck()
    125.     {
    126.         if(transform.position.x < enemy.position.x)
    127.             transform.localScale = new Vector3(-1,1,1);
    128.         else
    129.             transform.localScale = Vector3.one;
    130.     }
    131.  
    132.     void AttackInput()
    133.     {
    134.         if(Input.GetButtonDown("Attack1" + PlayerNumber.ToString()))
    135.           {
    136.             attack[0] = true;
    137.             attacktimer[0] = 0;
    138.             timesPressed[0] ++;
    139.          }
    140.  
    141.         if(attack[0])
    142.         {
    143.             attacktimer[0] += Time.deltaTime;
    144.  
    145.             if(attacktimer[0] > attackRate || timesPressed[0] >=4)
    146.             {
    147.                 attacktimer[0] = 0;
    148.                 attack[0] = false;
    149.                 timesPressed [0] = 0;
    150.             }
    151.         }
    152.    
    153.  
    154.         if(Input.GetButtonDown("Attack2" + PlayerNumber.ToString()))
    155.            {
    156.             attack[1] = true;
    157.             attacktimer[1] = 0;
    158.             timesPressed[1] ++;
    159.         }
    160.        
    161.         if(attack[1])
    162.         {
    163.             attacktimer[1] += Time.deltaTime;
    164.            
    165.             if(attacktimer[1] > attackRate || timesPressed[1] >=4)
    166.             {
    167.                 attacktimer[1] = 0;
    168.                 attack[1] = false;
    169.                 timesPressed [1] = 0;
    170.             }
    171.         }
    172.     }
    173.  
    174.     void Damage()
    175.     {
    176.         {
    177.             noDamageTimer += Time.deltaTime;
    178.  
    179.             if (noDamageTimer > noDamage)
    180.             {
    181.                 damage = false;
    182.                 noDamgaeTimer = 0;
    183.             }
    184.         }
    185.  
    186.         // if(!onGround)
    187.         //{
    188.             //rig2d.gravityScale = 10;
    189.             //Vector3 dir = enemy.position - transform.position;
    190.             //rig2d.AddForce(-dir * 25);
    191.         //}
    192.  
    193.     }
    194.  
    195.  
    196.     void UpdateAnimator()
    197.     {
    198.         anim.SetBool ("Crouch", crouch);
    199.         anim.SetBool ("OnGround", this.onGround);
    200.         anim.SetBool ("Falling", this.falling);
    201.         anim.SetFloat ("Movement", Mathf.Abs(horizontal));
    202.         anim.SetBool ("Attack1", attack [0]);
    203.         anim.SetBool ("Attack2", attack[1]);
    204.     }
    205.    
    206.     void OnCollisionEnter2D(Collision2D col)
    207.     {
    208.  
    209.         if (col.collider.tag == "Ground")
    210.         {
    211.             onGround = true;
    212.            
    213.             jumpKey = false;
    214.             jmpDuration = 0;
    215.             jmpForce = JumpForce;
    216.             falling = false;
    217.         }
    218.    
    219.     }
    220.    
    221.     void OnCollisionExit2D(Collision2D col)
    222.     {
    223.         if (col.collider.tag == "Ground")
    224.         {
    225.             onGround = false;
    226.         }
    227.     }
    228. }
     
  16. 3zzerland

    3zzerland

    Joined:
    Oct 31, 2014
    Posts:
    42
    Thanks., why don't we start with giving line 45 a line ending. I'll check it out a little more when I am home. After you do that change, are there any new/different errors?
     
    SSINZ likes this.
  17. SSINZ

    SSINZ

    Joined:
    Dec 2, 2015
    Posts:
    23
    Now I have two errors after I closed line 45. They are both on line 58.

    58 Assets/Scripts/PlayerControl.cs(9,9): Error CS1547: Keyword `void' cannot be used in this context (CS1547) (Assembly-CSharp)

    58 Assets/Scripts/PlayerControl.cs(19,19): Error CS1525: Unexpected symbol `(', expecting `,', `;', or `=' (CS1525) (Assembly-CSharp)
     
  18. 3zzerland

    3zzerland

    Joined:
    Oct 31, 2014
    Posts:
    42
    I see errors (that I point out below), but not with the line you comment about. Are those error reports from within Unity or from your IDE?

    Line 35:
    Code (csharp):
    1. public bool noDamage = 1;
    1 is an int, not a bool. Reading further into your code, it actually looks like this should be a float.

    Line 182:
    Code (csharp):
    1. noDamgaeTimer = 0;
    spelling error. should be noDamageTimer.

    Line 87 + 100:
    Code (csharp):
    1. Vector3 movement = newVector3(horizontal, 0, 0);
    You need a space between 'new' and 'Vector3'. On Line 100 it's same error except with a new Vector2.


    Fix these errors and let me know what happens :)

    Regards,
    Rob
     
  19. SSINZ

    SSINZ

    Joined:
    Dec 2, 2015
    Posts:
    23
    I fixed all the errors you talked about but I still have a problem with this code. Unity tells me that the void cannot be used in this context
    Code (CSharp):
    1. void Update(){
    2.  
     
  20. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    In this case, when Unity says "in this context", it most likely means that it sees that line as already being inside a function (and you can't declare 'void' type variables within functions). This means that you still have mismatched curly brackets, and in this case it means that somewhere above "void Update" you are missing a }.
     
  21. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    I've noticed a copy-and-paste bug between MonoDevelop and Safari that sometimes removes spaces when pasting, unless it's pasted using "Paste and Match Style" (which removes formatting from the text on the clipboard). I assume that's what's happening there.
     
    3zzerland likes this.
  22. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Post the whole thing again at its current state. You're probably missing a {}[](); somewhere :)
     
  23. 3zzerland

    3zzerland

    Joined:
    Oct 31, 2014
    Posts:
    42
    I am home today so I went ahead and loaded this script up in unity. After making the corrections I mentioned above, I get no errors.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerControl : MonoBehaviour
    5. {
    6.    
    7.     public int PlayerNumber = 1;
    8.     Transform enemy;
    9.    
    10.     Rigidbody2D rig2d;
    11.     Animator anim;
    12.    
    13.     float horizontal;
    14.     float vertical;
    15.     public float maxSpeed = 25;
    16.     Vector3 movement;
    17.     bool crouch;
    18.    
    19.    
    20.     public float JumpForce = 20;
    21.     public float jumpDuration = .1f;
    22.     float jmpDuration;
    23.     float jmpForce;
    24.     bool jumpKey;
    25.     bool falling;
    26.     bool onGround;
    27.    
    28.     public float attackRate = 0.3f;
    29.     bool[] attack = new bool[2];
    30.     float[] attacktimer = new float[2];
    31.     int[] timesPressed = new int[2];
    32.  
    33.     public bool damage;
    34.     public float noDamage = 1;
    35.     float noDamageTimer;
    36.    
    37.     void Start ()
    38.     {
    39.        
    40.         rig2d = GetComponent<Rigidbody2D> ();
    41.         anim = GetComponentInChildren<Animator> ();
    42.        
    43.         jmpForce = JumpForce;
    44.         GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
    45.        
    46.  
    47.         foreach(GameObject pl in players)
    48.         {
    49.        
    50.             if (pl.transform != this.transform)
    51.             {
    52.                 enemy = pl.transform;
    53.             }
    54.         }
    55.     }
    56.    
    57.     void Update()
    58.     {
    59.         AttackInput();
    60.         ScaleCheck();
    61.         OnGroundCheck();
    62.         Damage();
    63.         UpdateAnimator();
    64.     }
    65.    
    66.  
    67.     void OnGroundCheck()
    68.     {
    69.         if (!onGround)
    70.         {
    71.             rig2d.gravityScale = 5;
    72.         }
    73.         else
    74.         {
    75.             rig2d.gravityScale = 1;
    76.         }
    77.     }
    78.    
    79.     void FixedUpdate ()
    80.        
    81.     {
    82.        
    83.         horizontal = Input.GetAxis ("Horizontal" + PlayerNumber.ToString ());
    84.         vertical = Input.GetAxis("Vertical" + PlayerNumber.ToString());
    85.        
    86.         Vector3 movement = new Vector3(horizontal, 0, 0);
    87.        
    88.         crouch = (vertical < -0.1f);
    89.        
    90.         if (vertical > 0.1f)
    91.         {
    92.             if (!jumpKey)
    93.             {
    94.                 jmpDuration += Time.deltaTime;
    95.                 jmpForce += Time.deltaTime;
    96.                
    97.                 if (jmpDuration < jumpDuration)
    98.                 {
    99.                     rig2d.velocity = new Vector2(rig2d.velocity.x, jmpForce);
    100.                 }
    101.                 else
    102.                 {
    103.                     jumpKey = true;
    104.                 }
    105.             }
    106.         }
    107.        
    108.         if(!onGround && vertical < 0.1f)
    109.         {
    110.             falling = true;
    111.         }
    112.  
    113.         if(attack[0] && !jumpKey || attack[1] && !jumpKey)
    114.         {
    115.             movement = Vector3.zero;
    116.         }
    117.         if (!crouch)
    118.             rig2d.AddForce (movement * maxSpeed);
    119.         else
    120.             rig2d.velocity = Vector3.zero;
    121.     }
    122.  
    123.     void ScaleCheck()
    124.     {
    125.         if(transform.position.x < enemy.position.x)
    126.             transform.localScale = new Vector3(-1,1,1);
    127.         else
    128.             transform.localScale = Vector3.one;
    129.     }
    130.  
    131.     void AttackInput()
    132.     {
    133.         if(Input.GetButtonDown("Attack1" + PlayerNumber.ToString()))
    134.           {
    135.             attack[0] = true;
    136.             attacktimer[0] = 0;
    137.             timesPressed[0] ++;
    138.          }
    139.  
    140.         if(attack[0])
    141.         {
    142.             attacktimer[0] += Time.deltaTime;
    143.  
    144.             if(attacktimer[0] > attackRate || timesPressed[0] >=4)
    145.             {
    146.                 attacktimer[0] = 0;
    147.                 attack[0] = false;
    148.                 timesPressed [0] = 0;
    149.             }
    150.         }
    151.    
    152.  
    153.         if(Input.GetButtonDown("Attack2" + PlayerNumber.ToString()))
    154.            {
    155.             attack[1] = true;
    156.             attacktimer[1] = 0;
    157.             timesPressed[1] ++;
    158.         }
    159.        
    160.         if(attack[1])
    161.         {
    162.             attacktimer[1] += Time.deltaTime;
    163.            
    164.             if(attacktimer[1] > attackRate || timesPressed[1] >=4)
    165.             {
    166.                 attacktimer[1] = 0;
    167.                 attack[1] = false;
    168.                 timesPressed [1] = 0;
    169.             }
    170.         }
    171.     }
    172.  
    173.     void Damage()
    174.     {
    175.         {
    176.             noDamageTimer += Time.deltaTime;
    177.  
    178.             if (noDamageTimer > noDamage)
    179.             {
    180.                 damage = false;
    181.                 noDamageTimer = 0;
    182.             }
    183.         }
    184.  
    185.         // if(!onGround)
    186.         //{
    187.             //rig2d.gravityScale = 10;
    188.             //Vector3 dir = enemy.position - transform.position;
    189.             //rig2d.AddForce(-dir * 25);
    190.         //}
    191.  
    192.     }
    193.  
    194.  
    195.     void UpdateAnimator()
    196.     {
    197.         anim.SetBool ("Crouch", crouch);
    198.         anim.SetBool ("OnGround", this.onGround);
    199.         anim.SetBool ("Falling", this.falling);
    200.         anim.SetFloat ("Movement", Mathf.Abs(horizontal));
    201.         anim.SetBool ("Attack1", attack [0]);
    202.         anim.SetBool ("Attack2", attack[1]);
    203.     }
    204.    
    205.     void OnCollisionEnter2D(Collision2D col)
    206.     {
    207.  
    208.         if (col.collider.tag == "Ground")
    209.         {
    210.             onGround = true;
    211.            
    212.             jumpKey = false;
    213.             jmpDuration = 0;
    214.             jmpForce = JumpForce;
    215.             falling = false;
    216.         }
    217.    
    218.     }
    219.    
    220.     void OnCollisionExit2D(Collision2D col)
    221.     {
    222.         if (col.collider.tag == "Ground")
    223.         {
    224.             onGround = false;
    225.         }
    226.     }
    227. }
    If you are still getting errors, post your updated code along with the snipper from unit's Console log about the error.

    Regards,
    Rob
     
  24. SSINZ

    SSINZ

    Joined:
    Dec 2, 2015
    Posts:
    23
    Here you go fam

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class PlayerControl : MonoBehaviour
    6. {
    7.    
    8.     public int PlayerNumber = 1;
    9.     Transform enemy;
    10.    
    11.     Rigidbody2D rig2d;
    12.     Animator anim;
    13.    
    14.     float horizontal;
    15.     float vertical;
    16.     public float maxSpeed = 25;
    17.     Vector3 movement;
    18.     bool crouch;
    19.    
    20.    
    21.     public float JumpForce = 20;
    22.     public float jumpDuration = .1f;
    23.     float jmpDuration;
    24.     float jmpForce;
    25.     bool jumpKey;
    26.     bool falling;
    27.     bool onGround;
    28.    
    29.     public float attackRate = 0.3f;
    30.         bool[] attack = new bool[2];
    31.         float[] attacktimer = new float[2];
    32.         int[] timesPressed = new int[2];
    33.  
    34.         public bool damage;
    35.         public bool noDamage = 1;
    36.         float noDamageTimer;
    37.    
    38.     void Start ()
    39.     {
    40.        
    41.         rig2d = GetComponent<Rigidbody2D> ();
    42.         anim = GetComponentInChildren<Animator> ();
    43.        
    44.         jmpForce = JumpForce;
    45.         GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
    46.        
    47.  
    48.             foreach(GameObject pl in players)
    49.         {
    50.        
    51.             if (pl.transform != this.transform)
    52.             {
    53.                 enemy = pl.transform;
    54.             }
    55.         }
    56.    
    57.    
    58.     void Update(){
    59.  
    60.         AttackInput();
    61.         ScaleCheck();
    62.         OnGroundCheck();
    63.         Damage();
    64.         UpdateAnimator();
    65.     }
    66.    
    67.  
    68.     void OnGroundCheck()
    69.     {
    70.         if (!onGround)
    71.         {
    72.             rig2d.gravityScale = 5;
    73.         }
    74.         else
    75.         {
    76.             rig2d.gravityScale = 1;
    77.         }
    78.     }
    79.    
    80.     void FixedUpdate ()
    81.        
    82.     {
    83.        
    84.         horizontal = Input.GetAxis ("Horizontal" + PlayerNumber.ToString ());
    85.         vertical = Input.GetAxis("Vertical" + PlayerNumber.ToString());
    86.        
    87.         Vector3 movement = new Vector3(horizontal, 0, 0);
    88.        
    89.         crouch = (vertical < -0.1f);
    90.        
    91.         if (vertical > 0.1f)
    92.         {
    93.             if (!jumpKey)
    94.             {
    95.                 jmpDuration += Time.deltaTime;
    96.                 jmpForce += Time.deltaTime;
    97.                
    98.                 if (jmpDuration < jumpDuration)
    99.                 {
    100.                     rig2d.velocity = new Vector2(rig2d.velocity.x, jmpForce);
    101.                 }
    102.                 else
    103.                 {
    104.                     jumpKey = true;
    105.                 }
    106.             }
    107.         }
    108.        
    109.         if(!onGround && vertical < 0.1f)
    110.         {
    111.             falling = true;
    112.         }
    113.  
    114.         if(attack[0] && !jumpKey || attack[1] && !jumpKey)
    115.         {
    116.             movement = Vector3.zero;
    117.         }
    118.         if (!crouch)
    119.             rig2d.AddForce (movement * maxSpeed);
    120.         else
    121.             rig2d.velocity = Vector3.zero;
    122.     }
    123.  
    124.     void ScaleCheck()
    125.     {
    126.         if(transform.position.x < enemy.position.x)
    127.             transform.localScale = new Vector3(-1,1,1);
    128.         else
    129.             transform.localScale = Vector3.one;
    130.     }
    131.  
    132.     void AttackInput()
    133.     {
    134.         if(Input.GetButtonDown("Attack1" + PlayerNumber.ToString()))
    135.           {
    136.             attack[0] = true;
    137.             attacktimer[0] = 0;
    138.             timesPressed[0] ++;
    139.          }
    140.  
    141.         if(attack[0])
    142.         {
    143.             attacktimer[0] += Time.deltaTime;
    144.  
    145.             if(attacktimer[0] > attackRate || timesPressed[0] >=4)
    146.             {
    147.                 attacktimer[0] = 0;
    148.                 attack[0] = false;
    149.                 timesPressed [0] = 0;
    150.             }
    151.         }
    152.    
    153.  
    154.         if(Input.GetButtonDown("Attack2" + PlayerNumber.ToString()))
    155.            {
    156.             attack[1] = true;
    157.             attacktimer[1] = 0;
    158.             timesPressed[1] ++;
    159.         }
    160.        
    161.         if(attack[1])
    162.         {
    163.             attacktimer[1] += Time.deltaTime;
    164.            
    165.             if(attacktimer[1] > attackRate || timesPressed[1] >=4)
    166.             {
    167.                 attacktimer[1] = 0;
    168.                 attack[1] = false;
    169.                 timesPressed [1] = 0;
    170.             }
    171.         }
    172.     }
    173.  
    174.     void Damage()
    175.     {
    176.         {
    177.             noDamageTimer += Time.deltaTime;
    178.  
    179.             if (noDamageTimer > noDamage)
    180.             {
    181.                 damage = false;
    182.                 noDamageTimer = 0;
    183.             }
    184.         }
    185.  
    186.         // if(!onGround)
    187.         //{
    188.             //rig2d.gravityScale = 10;
    189.             //Vector3 dir = enemy.position - transform.position;
    190.             //rig2d.AddForce(-dir * 25);
    191.         //}
    192.  
    193.     }
    194.  
    195.  
    196.     void UpdateAnimator()
    197.     {
    198.         anim.SetBool ("Crouch", crouch);
    199.         anim.SetBool ("OnGround", this.onGround);
    200.         anim.SetBool ("Falling", this.falling);
    201.         anim.SetFloat ("Movement", Mathf.Abs(horizontal));
    202.         anim.SetBool ("Attack1", attack [0]);
    203.         anim.SetBool ("Attack2", attack[1]);
    204.     }
    205.    
    206.     void OnCollisionEnter2D(Collision2D col)
    207.     {
    208.  
    209.         if (col.collider.tag == "Ground")
    210.         {
    211.             onGround = true;
    212.            
    213.             jumpKey = false;
    214.             jmpDuration = 0;
    215.             jmpForce = JumpForce;
    216.             falling = false;
    217.         }
    218.    
    219.     }
    220.    
    221.     void OnCollisionExit2D(Collision2D col)
    222.     {
    223.         if (col.collider.tag == "Ground")
    224.         {
    225.             onGround = false;
    226.         }
    227.     }
    228. }
     
  25. SSINZ

    SSINZ

    Joined:
    Dec 2, 2015
    Posts:
    23

    Thank you for the help.
     
  26. 3zzerland

    3zzerland

    Joined:
    Oct 31, 2014
    Posts:
    42
    You're welcome.

    The problem with the script you posted is that you are missing a closing bracket on line 56 to close out your Start() function.

    Also, is there a reason you are using UnityEngine.UI? Just curious :p
     
  27. SSINZ

    SSINZ

    Joined:
    Dec 2, 2015
    Posts:
    23

    I'm working on game and someone told me to use UnityEngine.UI for the game.
     
  28. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Yes, it doesn't need to be included in every script though - just the ones that deal with the UI.