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

Question First Person Camera Error

Discussion in 'Scripting' started by flossboi, Dec 8, 2022.

  1. flossboi

    flossboi

    Joined:
    Dec 8, 2022
    Posts:
    3
    I am trying to lock a camera onto my player, but it is giving me an error saying:

    Assets\FirstPersonCamera.cs(6,18): error CS0116: A namespace cannot directly contain members such as fields or methods

    I was wondering what it meant and how I would fix it. If anyone can help it would be greatly appreciated. (Also here is the script I am using)


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

    public class FirstPersonCamera : MonoBehaviour

    public Transform player;
    public float mouseSensitivity = 2f;
    float cameraVerticalRotation = 0f;

    bool lockedCursor=true;

    {
    // Start is called before the first frame update
    void Start()
    {
    lockedCursor.visible=false;
    lockedCursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update()
    {
    float inputX=inputX.GetAxis("Mouse X")*mouseSensitivity;
    float inputY=inputY.GetAxis("Mouse Y")*mouseSensitivity;

    cameraVerticalRotation -= inputY;
    cameraVerticalRotation = Mathf.Clamp(cameraVerticalRotation, -90f, 90f);
    transform.localEulerAngles=Vector3.right * cameraVerticalRotation;

    player.Rotate(Vector3.up*inputX);
    }
    }
     
    Last edited: Dec 8, 2022
  2. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    There is a } above void start
     
  3. flossboi

    flossboi

    Joined:
    Dec 8, 2022
    Posts:
    3
    I tried that and it made no difference.
     
  4. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    It belongs above the first public
     
  5. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    491
    I think you mean that there is a { above void Start, not a }
    The issue is that the opening { for your class is in the wrong place. Move the { from the line above
    void Start()
    to be directly below the
    public class FirstPersonCamera : MonoBehaviour
    line.
    Next time you post, please follow the guidelines and use code tags when posting your scripts. This makes it easier for us to find the right lines and also we can just tell you which line numbers to change.
     
  6. flossboi

    flossboi

    Joined:
    Dec 8, 2022
    Posts:
    3

    Thanks, I will make sure to try that out, about the guidelines, this is the first time I've used this so I had no idea.