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

problem with script

Discussion in 'Getting Started' started by Melodee, Oct 19, 2016.

  1. Melodee

    Melodee

    Joined:
    Mar 16, 2016
    Posts:
    56
    hello i don't know if im posting in the right thing here but im making a game and someone made a script, i think it's just a tiny issue he made because he doesnt work with unity much and doesnt know what syntax the developers chose (his words). i hope someone can explain to me what's wrong, i will post pictures here of the script, the setup and the compiler errors.

    this is the error the compiler leads me to:
    upload_2016-10-19_13-26-3.png
    here are the compiler errors and how the player (girl2) is setup:
    upload_2016-10-19_13-26-55.png
     
  2. Philip-Rowlands

    Philip-Rowlands

    Joined:
    May 13, 2013
    Posts:
    353
    The most likely cause is that he's misspelt isFalling. Could you post the contents of PlayerScript?
     
  3. Melodee

    Melodee

    Joined:
    Mar 16, 2016
    Posts:
    56
    OK!
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerInput : MonoBehaviour
    5. {
    6.  
    7.     public float jumpTime;
    8.     public float runSpeed = 4f;
    9.     public bool isWalking = false;
    10.  
    11.     private float horizontalInput;
    12.     private float verticalInput;
    13.  
    14.     Animator animator;
    15.     Vector2 movement2D;
    16.     SpriteRenderer spriteRenderer;
    17.     Rigidbody2D rigidBody2D;
    18.     Animation animations;
    19.  
    20.     //public float jumpHeight = 1f;
    21.     public bool isJumping = false;
    22.     //public bool isFalling = false;
    23.     //public bool isOnJumpGround;
    24.     private float jumpInitialVelocity = 5f;  //tweak as needed —CP :3
    25.     private float accelerationDueToGravity = -1f;  //tweak as needed —CP :3
    26.     private float playerYVelocity;
    27.     private float groundYPositionWhenJumpStarted;
    28.  
    29.     void Start()
    30.     {
    31.         rigidBody2D = GetComponent<Rigidbody2D>();
    32.         animator = GetComponent<Animator>();
    33.         spriteRenderer = GetComponent<SpriteRenderer>();
    34.     }
    35.  
    36.     void Update()
    37.     {
    38.  
    39.         horizontalInput = Input.GetAxisRaw("Horizontal");
    40.         verticalInput = Input.GetAxisRaw("Vertical");
    41.  
    42.         isWalking = horizontalInput != 0 || verticalInput != 0;
    43.  
    44.         animator.SetBool("isWalking", isWalking);
    45.  
    46.         if (isWalking)
    47.         {
    48.             animator.SetFloat("X", horizontalInput);
    49.             animator.SetFloat("Y", verticalInput);
    50.  
    51.             FlipSprite();
    52.  
    53.             ReduceAnimationSpeedIfMovingDiagonally();
    54.         }
    55.     }
    56.  
    57.  
    58.  
    59.     void FixedUpdate()
    60.     {
    61.         if (isWalking || isJumping)
    62.             MoveUnit(horizontalInput, verticalInput);
    63.  
    64.         if (!isJumping && Input.GetKeyDown (KeyCode.Space))
    65.         {
    66.             //StartCoroutine(Jump());
    67.          
    68.             //Jump! :D
    69.             groundYPositionWhenJumpStarted = transform.position.y;
    70.             playerYVelocity = jumpInitialVelocity;
    71.             isJumping = true;
    72.         }
    73.      
    74.         /*
    75.         if (isJumping && !isFalling) {
    76.             transform.position += new Vector3 (0, heightPosition, 0);
    77.             heightPosition += (jumpHeight / 2) * Time.deltaTime;
    78.             jumpedDistance += (jumpHeight / 2) * Time.deltaTime;
    79.         } else if (isJumping && isFalling) {
    80.             transform.position -= new Vector3 (0, (jumpHeight / 2) * Time.deltaTime, 0);
    81.             heightPosition -= (jumpHeight / 2) * Time.deltaTime;
    82.             jumpedDistance -= (jumpHeight / 2) * Time.deltaTime;
    83.         } else if (!isJumping && isFalling) {
    84.             transform.position -= new Vector3 (transform.position.x, (jumpHeight / 2) * Time.deltaTime, 0);
    85.             heightPosition -= (jumpHeight / 2) * Time.deltaTime;
    86.         }
    87.  
    88.         if (heightPosition <= 0) {
    89.             isFalling = false;
    90.             heightPosition = 0;
    91.         }
    92.         */
    93.      
    94.         if (isJumping)
    95.         {
    96.             //We could use a fully analytical form here involving squares and have no 'playerYVelocity' state variable!, but having a second-order system (second order makes for two state variables: Position and Velocity!) and numerically integrating it is more flexible as it allows velocity to be modified during the integration in a non-trivial way! :3   (that is, if the code was altered to allow that XD )
    97.             //Also it might be a little more obvious what's going on ^^''
    98.  
    99.             transform.position += new Vector3(0, playerYVelocity * Time.deltaTime, 0);
    100.  
    101.             if (transform.position.y <= groundYPositionWhenJumpStarted)
    102.             {
    103.                 transform.position = new Vector3(transform.position.x, groundYPositionWhenJumpStarted, transform.position.z);
    104.                 isJumping = false;
    105.                 playerYVelocity = 0f;
    106.             }
    107.             else
    108.             {
    109.                 playerYVelocity += accelerationDueToGravity * Time.deltaTime;  //note that accelerationDueToGravity is negative ^_~
    110.             }
    111.         }
    112.     }
    113.  
    114. /*
    115.     IEnumerator Jump()
    116.     {
    117.         Debug.Log ("Jump!");
    118.  
    119.         GameObject[] groundArr = GameObject.FindGameObjectsWithTag ("jumpGround");
    120.  
    121.         foreach (GameObject ground in groundArr) {
    122.             ground.GetComponent<PolygonCollider2D> ().isTrigger = true;
    123.         }
    124.         GetComponentInChildren<BoxCollider2D> ().isTrigger = true;
    125.  
    126.         isJumping = true;
    127.  
    128.         yield return new WaitForSeconds (jumpTime / 2);
    129.  
    130.         isFalling = true;
    131.  
    132.         foreach (GameObject ground in groundArr) {
    133.             ground.GetComponent<PolygonCollider2D> ().isTrigger = false;
    134.         }
    135.  
    136.         yield return new WaitForSeconds (jumpTime / 2);
    137.  
    138.  
    139.         groundArr = GameObject.FindGameObjectsWithTag ("jumpGround");
    140.  
    141.         foreach (GameObject ground in groundArr) {
    142.             ground.GetComponent<PolygonCollider2D> ().isTrigger = true;
    143.         }
    144.         GetComponentInChildren<BoxCollider2D> ().isTrigger = false;
    145.  
    146.         transform.position -= new Vector3 (0, jumpedDistance, 0);
    147.         heightPosition -= jumpedDistance;
    148.         jumpedDistance = 0;
    149.  
    150.         isFalling = false;
    151.         isJumping = false;
    152.     }
    153. */
    154.  
    155.  
    156.     void ReduceAnimationSpeedIfMovingDiagonally()
    157.     {
    158.         if (horizontalInput == -1 && verticalInput == -1)
    159.             animator.speed = 0.65f;
    160.         else if (horizontalInput == 1 && verticalInput == 1)
    161.             animator.speed = 0.65f;
    162.         else if (horizontalInput == 1 && verticalInput == -1)
    163.             animator.speed = 0.65f;
    164.         else if (horizontalInput == -1 && verticalInput == 1)
    165.             animator.speed = 0.65f;
    166.         else if (horizontalInput == 0 && verticalInput == 1)
    167.             animator.speed = 0.5f;
    168.         else if (horizontalInput == 0 && verticalInput == -1)
    169.             animator.speed = 0.5f;
    170.         else
    171.             animator.speed = 1f;
    172.     }
    173.  
    174.     void FlipSprite()
    175.     {
    176.         if (horizontalInput == -1 && verticalInput == 0)
    177.         {
    178.             spriteRenderer.flipX = true;
    179.         }
    180.         else if (horizontalInput == -1 && verticalInput == 1)
    181.         {
    182.             spriteRenderer.flipX = true;
    183.         }
    184.         else if (horizontalInput == -1 && verticalInput == -1)
    185.         {
    186.             spriteRenderer.flipX = true;
    187.         }
    188.         else
    189.         {
    190.             spriteRenderer.flipX = false;
    191.         }
    192.     }
    193.  
    194.     void MoveUnit(float horizontalInput, float verticalInput)
    195.     {
    196.         Vector2 theInput = new Vector2 (horizontalInput, verticalInput);
    197.         movement2D = (theInput.normalized * runSpeed) * Time.deltaTime;
    198.  
    199.         //rigidBody2D.MovePosition(rigidBody2D.position + movement2D);
    200.  
    201.         //rigidBody2D.AddForce (movement2D);
    202.  
    203.         transform.position += new Vector3 (movement2D.x, 0, movement2D.y);
    204.     }
    205.  
    206. /*
    207.     void OnTriggerEnter2D (Collider2D coll) {
    208.         if (coll.tag == "jumpGround" && coll.transform.parent.GetComponent <ObjectHeight> ().height <= heightPosition && isFalling) {
    209.             GetComponentInChildren<BoxCollider2D> ().isTrigger = false;
    210.             jumpedDistance = 0;
    211.             isFalling = false;
    212.             isJumping = false;
    213.  
    214.             GameObject[] groundArr = GameObject.FindGameObjectsWithTag ("jumpGround");
    215.             foreach (GameObject ground in groundArr) {
    216.                 ground.GetComponent<PolygonCollider2D> ().isTrigger = true;
    217.             }
    218.             isOnJumpGround = true;
    219.         }
    220.     }
    221.  
    222.     void OnTriggerExit2D (Collider2D coll) {
    223.         Debug.Log ("LEFT");
    224.         if (coll.tag == "jumpGround" && !isJumping) {
    225.             isOnJumpGround = false;
    226.             isFalling = true;
    227.         }
    228.     }
    229. */
    230. }
    that is the player script. there is another script too attached to the player which is this one:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [ExecuteInEditMode]
    4. public class OrderInLayer : MonoBehaviour
    5. {
    6.     public int sortingOrder;
    7.     private SpriteRenderer spriteRenderer;
    8.     public float multiplier = 50f;
    9.     /// This is the difference between the very bottom of the sprite and the bottom of the frame
    10.     public int bottomOffset = 0;
    11.     public bool setCustomSortingOrder = false;
    12.     public int customSortingOrder;
    13.  
    14.     private PlayerInput playerScript;
    15.     private int LayerWhenJumping;
    16.  
    17.     // Use this for initialization
    18.     void Start()
    19.     {
    20.         spriteRenderer = GetComponent<SpriteRenderer>();
    21.         playerScript = GameObject.FindWithTag ("Player").GetComponent<PlayerInput> ();
    22.  
    23.         if (spriteRenderer)
    24.         {
    25.             if (!setCustomSortingOrder)
    26.             {
    27.                 spriteRenderer.sortingOrder = CalculateOrderInLayer();
    28.             }
    29.             else if (setCustomSortingOrder)
    30.             {
    31.                 spriteRenderer.sortingOrder = customSortingOrder;
    32.             }
    33.         }
    34.     }
    35.  
    36.     // Update is called once per frame
    37.     void Update()
    38.     {
    39.         if (spriteRenderer)
    40.         {
    41.             if (!setCustomSortingOrder)
    42.             {
    43.                 sortingOrder = CalculateOrderInLayer();
    44.  
    45.                 if (spriteRenderer.sortingOrder != sortingOrder) {
    46.                     if (!playerScript.isJumping)
    47.                     {
    48.                         spriteRenderer.sortingOrder = sortingOrder + LayerWhenJumping;
    49.                         LayerWhenJumping = 0;
    50.                     } else {
    51.                         LayerWhenJumping += sortingOrder - spriteRenderer.sortingOrder;
    52.                     }
    53.                 }
    54.             }
    55.             else if (setCustomSortingOrder)
    56.             {
    57.                 spriteRenderer.sortingOrder = customSortingOrder;
    58.             }
    59.         }
    60.     }
    61.  
    62.     int CalculateOrderInLayer()
    63.     {
    64.         int order = Mathf.RoundToInt(-((spriteRenderer.sprite.bounds.size.y - bottomOffset) - spriteRenderer.sprite.bounds.max.y) + (transform.localPosition.y * multiplier)) * -1;
    65.  
    66.         return order;
    67.     }
    68. }
    right now there is no error, we fixed it but we still have the same problem that the player doesnt move. only the camera. it'd be awesome if u could help us fixing that since no one could figure it out till now.
     
  4. Philip-Rowlands

    Philip-Rowlands

    Joined:
    May 13, 2013
    Posts:
    353
    Try parenting the player to the camera as a quick fix. Also, isFalling is now commented out - is it meant to be?