Search Unity

help on ground check

Discussion in 'Scripting' started by NintendoAsh12, Apr 4, 2021.

Thread Status:
Not open for further replies.
  1. NintendoAsh12

    NintendoAsh12

    Joined:
    Feb 15, 2021
    Posts:
    91
    I tryed to add ground check plz fix this script:
    Code (CSharp):
    1. //--------------------------------------------------Gamer-Peebs-----------------------------------------
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class playerMovment : MonoBehaviour
    7. {
    8.  
    9.     public CharacterController2D controller;
    10.  
    11.     public float runSpeed = 40f;
    12.  
    13.     float horizontalMove = 0f;
    14.  
    15.     bool jump = false;
    16.     bool chouch = false;
    17.  
    18.     public  CircleCollider2D foot;
    19.  
    20.  
    21.     void Update ()
    22.     {
    23.         horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
    24.  
    25.         if (ongrawnd() && Input.GetButtonDown("Jump"))
    26.         {
    27.             jump = true;
    28.             ongrawnd = false;
    29.         }
    30.  
    31.         if (Input.GetButtonDown("chouch"))
    32.         {
    33.             chouch = true;
    34.         } else if (Input.GetButtonUp("chouch"))
    35.         {
    36.             chouch = false;
    37.         }
    38.     }
    39.  
    40.  
    41.     void FixedUpdate ()
    42.     {
    43.         public bool ongrawnd()
    44.         {
    45.             float extraHeightText = .01f;
    46.             RaycastHit2D raycastHit = Physics2D.Raycast(boxCollider2d.bounds.center, Vector2.down, boxCollider2d.boundsextents.y + extraHeightText);
    47.             Color rayColor;
    48.             if (raycastHit.collider != null)
    49.             {
    50.                 rayColor = rayColor.green;
    51.             }
    52.             else
    53.             {
    54.                 rayColor = rayColor.red;
    55.             }
    56.             Debug.DrawRay(boxCollider2d.bounds.center, Vector2.down * (boxCollider2d.boundsextents.y + extraHeightText);)
    57.         return raycastHit.collider != null;
    58.         }
    59.      
    60.  
    61.     controller.Move(horizontalMove * Time.fixedDeltaTime, chouch, jump);
    62.         jump = false;
    63.         ongrawnd = true;
    64.     }
    65.    
    66. }
    67.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    Beyond that, to help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
    hms0589 likes this.
  3. hms0589

    hms0589

    Joined:
    Jan 5, 2020
    Posts:
    27
    first of all, you basically did this
    Code (CSharp):
    1. void Update()
    2. {
    3.     void FixedUpdate()
    4. }

    if you still does not understand, reply me.
     
  4. NintendoAsh12

    NintendoAsh12

    Joined:
    Feb 15, 2021
    Posts:
    91
    no I did not this is void Update:
    Code (CSharp):
    1.   void Update ()
    2.     {
    3.         horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
    4.         if (ongrawnd() && Input.GetButtonDown("Jump"))
    5.         {
    6.             jump = true;
    7.             ongrawnd = false;
    8.         }
    9.         if (Input.GetButtonDown("chouch"))
    10.         {
    11.             chouch = true;
    12.         } else if (Input.GetButtonUp("chouch"))
    13.         {
    14.             chouch = false;
    15.         }
    16.     }
     
  5. NintendoAsh12

    NintendoAsh12

    Joined:
    Feb 15, 2021
    Posts:
    91
    some of my errors:
    Assets\playerMovment.cs(66,1): error CS1022: Type or namespace definition, or end-of-file expected
    Assets\playerMovment.cs(63,18): error CS1519: Invalid token '=' in class, struct, or interface member declaration
    Assets\playerMovment.cs(62,14): error CS1519: Invalid token '=' in class, struct, or interface member declaration
    Assets\playerMovment.cs(61,71): error CS1519: Invalid token ')' in class, struct, or interface member declaration
    Assets\playerMovment.cs(61,42): error CS1026: ) expected
    Assets\playerMovment.cs(61,42): error CS8124: Tuple must contain at least two elements.
    Assets\playerMovment.cs(61,20): error CS1519: Invalid token '(' in class, struct, or interface member declaration
    Assets\playerMovment.cs(56,121): error CS1513: } expected
    Assets\playerMovment.cs(56,120): error CS1026: ) expected
    Assets\playerMovment.cs(43,9): error CS0106: The modifier 'public' is not valid for this item
     
  6. hms0589

    hms0589

    Joined:
    Jan 5, 2020
    Posts:
    27
    put OnGround Field on Class, not FixedUpdate
     
  7. NintendoAsh12

    NintendoAsh12

    Joined:
    Feb 15, 2021
    Posts:
    91
    help me i am new and confused
     
  8. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    Pointless, lazy thread. Read the rules before posting again.
     
Thread Status:
Not open for further replies.