Search Unity

Trying to edit script, getting compiler error

Discussion in '2D' started by MiloRoban, Mar 21, 2018.

  1. MiloRoban

    MiloRoban

    Joined:
    Apr 19, 2017
    Posts:
    70
    So I have been editing this script, trying to use it just for jumping as I have a script that automatically moves the player. Although, when trying to remove the manual movement part from the script, I get compiler errors. I am open to all help. Thank you.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SimplePlatformScript : MonoBehaviour
    5. {
    6.  
    7.     [HideInInspector] public bool facingRight = true;
    8.     [HideInInspector] public bool jump = false;
    9.     public float moveForce = 365f;
    10.     public float maxSpeed = 5f;
    11.     public float jumpForce = 1000f;
    12.     public Transform groundCheck;
    13.  
    14.  
    15.     private bool grounded = false;
    16.     private Animator anim;
    17.     private Rigidbody2D rb2d;
    18.  
    19.  
    20.     // Use this for initialization
    21.     void Awake()
    22.     {
    23.         anim = GetComponent<Animator>();
    24.         rb2d = GetComponent<Rigidbody2D>();
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
    31.  
    32.         if (Input.GetButtonDown("Jump") && grounded)
    33.         {
    34.             jump = true;
    35.         }
    36.     }
    37.  
    38.     void FixedUpdate()
    39.     {
    40.         float h = Input.GetAxis("Horizontal");
    41.  
    42.         anim.SetFloat("Speed", Mathf.Abs(h));
    43.  
    44.         if (h * rb2d.velocity.x < maxSpeed)
    45.             rb2d.AddForce(Vector2.right * h * moveForce);
    46.  
    47.         if (Mathf.Abs(rb2d.velocity.x) > maxSpeed)
    48.             rb2d.velocity = new Vector2(Mathf.Sign(rb2d.velocity.x) * maxSpeed, rb2d.velocity.y);
    49.  
    50.         if (h > 0 && !facingRight)
    51.             Flip();
    52.         else if (h < 0 && facingRight)
    53.             Flip();
    54.  
    55.         if (jump)
    56.         {
    57.             anim.SetTrigger("Jump");
    58.             rb2d.AddForce(new Vector2(0f, jumpForce));
    59.             jump = false;
    60.         }
    61.     }
    62.  
    63.  
    64.     void Flip()
    65.     {
    66.         facingRight = !facingRight;
    67.         Vector3 theScale = transform.localScale;
    68.         theScale.x *= -1;
    69.         transform.localScale = theScale;
    70.     }
    71. }
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
    whats the actual error message?
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    ... and it might help to show the script with the code you tried to remove (that produces the error*).
     
  4. MiloRoban

    MiloRoban

    Joined:
    Apr 19, 2017
    Posts:
    70
     
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
    you are probably missing some { } or ( ) or ; there, or some other random character is breaking it..
    but the script above didnt't give any errors..
     
    vakabaka likes this.
  6. MiloRoban

    MiloRoban

    Joined:
    Apr 19, 2017
    Posts:
    70
    This is the new code. Sorry, i forgot to include it.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SimplePlatformScript : MonoBehaviour
    5. {
    6.  
    7.     [HideInInspector] public bool facingRight = true;
    8.     [HideInInspector] public bool jump = false;
    9.     public float moveForce = 365f;
    10.     public float maxSpeed = 5f;
    11.     public float jumpForce = 1000f;
    12.     public Transform groundCheck;
    13.  
    14.  
    15.     private bool grounded = false;
    16.     private Animator anim;
    17.     private Rigidbody2D rb2d;
    18.  
    19.  
    20.     // Use this for initialization
    21.     void Awake()
    22.     {
    23.         anim = GetComponent<Animator>();
    24.         rb2d = GetComponent<Rigidbody2D>();
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
    31.  
    32.         if (Input.GetButtonDown("Jump") && grounded)
    33.         {
    34.             jump = true;
    35.         }
    36.     }
     
  7. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    } is missed at the end of script (you should close the line 5 with new line 37). And the player will not jump, the jump code was deleted too. Check the jumping line in FixedUpdate:
    rb2d.AddForce(new Vector2(0f, jumpForce));

    maybe you don't need the commented (green) lines. They are not needed for jumping only.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SimplePlatformScript : MonoBehaviour
    5. {
    6.  
    7.     //[HideInInspector] public bool facingRight = true;
    8.     [HideInInspector] public bool jump = false;
    9.     //public float moveForce = 365f;
    10.     //public float maxSpeed = 5f;
    11.     public float jumpForce = 1000f;
    12.     public Transform groundCheck;
    13.  
    14.  
    15.     private bool grounded = false;
    16.     //private Animator anim;
    17.     private Rigidbody2D rb2d;
    18.  
    19.  
    20.     // Use this for initialization
    21.     void Awake()
    22.     {
    23.         //anim = GetComponent<Animator>();
    24.         rb2d = GetComponent<Rigidbody2D>();
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
    31.  
    32.         if (Input.GetButtonDown("Jump") && grounded)
    33.         {
    34.             jump = true;
    35.         }
    36.     }
    37.  
    38. void FixedUpdate()
    39.     {
    40.         if (jump)
    41.         {
    42.             //anim.SetTrigger("Jump");
    43.             rb2d.AddForce(new Vector2(0f, jumpForce));
    44.             jump = false;
    45.         }
    46.     }
    47. }
     
    Last edited: Mar 23, 2018
  8. MiloRoban

    MiloRoban

    Joined:
    Apr 19, 2017
    Posts:
    70
    That seemed to have worked.. thank you.

    One more question, how do I make it so the player can only jump once until hitting the ground again?
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    If should work with the grounded bool you have. Is the 'grounded' bool working correctly?
     
  10. MiloRoban

    MiloRoban

    Joined:
    Apr 19, 2017
    Posts:
    70
    I fixed it. How do I make it so the player doesn't tumble over and just moves standing straight up?
     
  11. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    Enable "freeze rotation" in players rigidbody2d.
     
    MiloRoban likes this.
  12. MiloRoban

    MiloRoban

    Joined:
    Apr 19, 2017
    Posts:
    70
    Thank you!