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

using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerM

Discussion in 'Scripting' started by ChewBlocker, Aug 22, 2020.

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

    ChewBlocker

    Joined:
    Aug 22, 2020
    Posts:
    8
    I need help im lost
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public CharacterController controller;
    8.  
    9.     public float speed = 12f;
    10.     public float gravity = -9.81f;
    11.  
    12.     public Transform groundCheck;
    13.     public float groundDistance = 0.4f;
    14.     public Layermask groundMask;
    15.  
    16.     Vector3 velocity;
    17.     bool isGrounded;
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    23.  
    24.         if(isGrounded && velocity.y < 0)
    25.         {
    26.             velocity.y = -2f;
    27.         }
    28.  
    29.         float x = Input.GetAxis("Horizontal");
    30.         float z = Input.GetAxis("Vertical");
    31.  
    32.         Vector3 move = transform.right * x + transform.forward * z;
    33.  
    34.         controller.Move(move * speed * Time.deltaTime);
    35.  
    36.         velocity.y += gravity * Time.deltaTime;
    37.  
    38.         controller.Move(velocity * Time.deltaTime);
    39.       }  
    40. }
    41.  
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    I'm lost as to what you need help with.

    What is your question?
     
    Vryken likes this.
  3. ChewBlocker

    ChewBlocker

    Joined:
    Aug 22, 2020
    Posts:
    8
    the error
    using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerM
     
  4. ChewBlocker

    ChewBlocker

    Joined:
    Aug 22, 2020
    Posts:
    8
    i need help on how to fix the error
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    That's not an error.

    That's just some code with the end snipped off.

    Maybe it's the first part of an error because the error is pointing out a problem with that section of code... but you did not paste the entirity of the error... or isn't even related to the error at all. And more so, you posted it as only the title of your thread and not in the body of the thread.

    I do see an issue with your code, but it may be a typo from your posting. I don't know... thing is, you need to learn how to debug your own errors by first learning how to even read an error. As well as learning how to ask a question in an appropriate manner.

    It's a bit rude to just come on a forum, foist some code at us, and say "I need help, I'm lost" with zero explanation as to what you're lost at doing. Why should we help you if you can't even bother to help us understand your problem?
     
    Last edited: Aug 22, 2020
    Bunny83 and JasonBricco like this.
  6. ChewBlocker

    ChewBlocker

    Joined:
    Aug 22, 2020
    Posts:
    8
    I'm sorry man I just copy and pasted the wrong thing also I just got into programming you should have seen that i joined today
     
    Last edited: Aug 22, 2020
  7. ChewBlocker

    ChewBlocker

    Joined:
    Aug 22, 2020
    Posts:
    8
    Assets\PlayerMovement.cs(14,12): error CS0246: The type or namespace name 'Layermask' could not be found (are you missing a using directive or an assembly reference?) here is the error

    Here is the code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public CharacterController controller;
    8.  
    9.     public float speed = 12f;
    10.     public float gravity = -9.81f;
    11.  
    12.     public Transform groundCheck;
    13.     public float groundDistance = 0.4f;
    14.     public Layermask groundMask;
    15.  
    16.     Vector3 velocity;
    17.     bool isGrounded;
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    23.  
    24.         if(isGrounded && velocity.y < 0)
    25.         {
    26.             velocity.y = -2f;
    27.         }
    28.  
    29.         float x = Input.GetAxis("Horizontal");
    30.         float z = Input.GetAxis("Vertical");
    31.  
    32.         Vector3 move = transform.right * x + transform.forward * z;
    33.  
    34.         controller.Move(move * speed * Time.deltaTime);
    35.  
    36.         velocity.y += gravity * Time.deltaTime;
    37.  
    38.         controller.Move(velocity * Time.deltaTime);
    39.     }
    40. }
    41.    
     
  8. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Line 14:
    "Layermask" should be "LayerMask". Capitalization matters.
     
    Bunny83 and ChewBlocker like this.
  9. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Yep, so it was what I thought it was going to be. LayerMask.

    Yes, I notice that. And lesson number one when you get into programming is learning how to read errors. You're going to get a lot of them, even when you're good at programming. So it's a thing you need to learn right away.

    So like this error here:
    Assets\PlayerMovement.cs(14,12): error CS0246: The type or namespace name 'Layermask' could not be found (are you missing a using directive or an assembly reference?)

    It's telling you to go to PlayerMovement.cs, line 14, char 12. Which is the beginning of the word 'Layermask'.

    And it says "the type or namespace name 'Layermask' could not be found". This means that the compiler doesn't recognize this word. This can happen for various reasons.

    Missing a using directive/assembly reference - maybe the code that defines this word isn't imported correctly.

    Or another, which is often the case, and is here... you misspelled something.
     
    Vryken and Kurt-Dekker like this.
  10. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    I wanted to come back and add.

    If you have a fully featured IDE like Visual Studio Community Edition which comes with Unity. The error should also have jaggy red lines underneath it. And when you mouse over it, the error will show there as well.
    upload_2020-8-22_15-58-46.png

    Furthermore, there will be a little link for showing potential fixes.
    upload_2020-8-22_15-59-30.png

    Here I've highlighted the correct fix.

    And finally... when typing in Visual Studio, it will suggest what it might be and pressing tab will autocomplete it. Or if not it will even correct it when you press space.
    Typing.gif
     
    Vryken likes this.
  11. SpinningParagon

    SpinningParagon

    Joined:
    Jun 1, 2022
    Posts:
    1
    Can some tell me how to add crouch to first person cam asset
     
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    Please dont't hijack/necro threads, simply create your own threads and explain your problem well. Your kind of question requires tutorial(s) and isn't just a single sentence reply suited to a forum. Maybe you're asking for tutorials/examples because you cannot find any but I find that hard to understand as there must be hundreds.
     
Thread Status:
Not open for further replies.