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

Error in PlayerMovement.cs

Discussion in 'Scripting' started by Deleted User, Dec 26, 2017.

  1. Deleted User

    Deleted User

    Guest

    I was trying to learn watching the Survival Shooter Tutorial, and got this error on PlayerMovement.cs:

    Code (csharp):
    1. Assets/Scripts/Player/PlayerMovement.cs(45,36): error CS0103: The name `floorHit' does not exist in the current context
    I don't know why it happened, I'm sure that I've done everything like they did.

    My script is writen like this:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerMovement : MonoBehaviour
    4. {
    5.     public float speed = 6f;
    6.  
    7.     Vector3 movement;
    8.     Animator anim;
    9.     Rigidbody playerRigidbody;
    10.     int floorMask;
    11.     float camRayLength = 100f;
    12.  
    13.     void Awake()
    14.     {
    15.         floorMask = LayerMask.GetMask ("Floor");
    16.         anim = GetComponent <Animator> ();
    17.         playerRigidbody = GetComponent<Rigidbody> ();
    18.     }
    19.  
    20.     void FixedUpdate()
    21.     {
    22.         float h = Input.GetAxisRaw ("Horizontal");
    23.         float v = Input.GetAxisRaw ("Vertical");
    24.  
    25.         Move (h, v);
    26.         Turning ();
    27.         Animating (h, v);
    28.     }
    29.  
    30.     void Move (float h, float v)
    31.     {
    32.         movement.Set (h, 0f, v);
    33.  
    34.         movement = movement.normalized * speed * Time.deltaTime;
    35.  
    36.         playerRigidbody.MovePosition (transform.position + movement);
    37.     }
    38.  
    39.     void Turning()
    40.     {
    41.         Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    42.  
    43.         RaycastHit flootHit;
    44.  
    45.         if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
    46.         {
    47.             Vector3 playerToMouse = floorHit.point - transform.position;
    48.             playerToMouse.y = 0f;
    49.  
    50.             Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
    51.             playerRigidbody.MoveRotation (newRotation);
    52.         }
    53.     }
    54.  
    55.     void Animating(float h, float v)
    56.     {
    57.         bool walking = h != 0f || v != 0f;
    58.         anim.SetBool ("IsWalking", walking);
    59.     }
    60. }
    61.  
    Unity tells me the wrong line is this:
    if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))

    How can I fix this?
    Can someone help me here?
     
    Last edited by a moderator: Dec 26, 2017
  2. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Deleted User likes this.
  3. Deleted User

    Deleted User

    Guest

    Thanks!
    Is there any place to learn about creating a variable? This is my first try to script, and at the moment I can just follow instructions.
    Thanks again.
    _____
    EDIT:

    @Rotary-Heart
    I found my own mistake. In line 43 I wrote RaycastHit flootHit, instead of RaycastHit floorHit
    Thanks again!
     
    Last edited by a moderator: Dec 26, 2017
  4. qmitrox22

    qmitrox22

    Joined:
    Jan 11, 2020
    Posts:
    1
    Help me this is error

    using System.Collections;
    using System.Collections.Generic;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerMovment : MonoBehaviour
    {

    public CharacterController2D controller;

    public float runSpeed = 40f;

    float horizontaleMove = 0f;
    bool jump = false;


    void Update() {
    horizontaleMove = Input.GetAxisRaw("Horizontal") * runSpeed;


    if (input.GetButtonDown("jump"))
    {
    jump = true;
    }
    ]

    void FixedUpdate()
    {
    controller.Move(horizontalMove * Time.fixedDeltaTime, false, false);
    jump = false;
    {


    }

    }
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    1. use code tags (see above!)
    2. what is the error? (exact text please)
    3. what line is it on?
    4. what do you expect it to do?
    5. what does it do that displeases you?

    And generally, don't respond to threads from three years ago. Just make a new thread. Nobody is going to bill you for it and it simplifies what is going on.
     
  6. sardarovgabriell

    sardarovgabriell

    Joined:
    Apr 11, 2020
    Posts:
    6
    i have this problem too,pls help

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerMovement : MonoBehaviour
    {
    public CharacterController2D controller;
    private readonly Debug.Log input.AxisRawHorizontal;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update();

    private Debug.Log GetInput.AxisRawHorizontal() => input.AxisRawHorizontal;
    }