Search code examples
c#unity-game-enginegame-engine

Type or namespace definition, or end of file detected


I'm trying to make a game in unity I'm fairly new, but this is the error I get:

Assets\Scripts\Motion.cs(97,1): error CS1022: Type or namespace definition, or end-of-file expected

I've been tinkering a lot and still can't find it out I've searched around for a solution but still no luck

and this is my code

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

namespace Com.Apocstud.Simpleshooter
{
    public class Motion : MonoBehaviour
    {

        public float speed;
        public float sprintModifier;
        public float jumpForce;
        public Camera normalCam;
        public Transform groundDetector;
        public LayerMask ground;

        private Rigidbody rig;

        private float baseFOV;
        private float sprintFOVModifier = 1.5f;

        private void Start()
        {
            baseFOV = normalCam.fieldOfView;
            camera.main.enabled = false;
            rig = GetComponent<Rigidbody>();
        }

        private void update;
        {
            float t_hmove = Input.GetAxisRaw("Horizontal");
            float t_vmove = Input.GetAxisRaw("Vertical");

            bool jump = Input.GetKey(KeyCode.Space);
            bool sprint = Input.GetKey(KeyCode.LeftShift);

            bool isGrounded = Physics.Raycast(groundDetector.position, Vector3.down, 0.1f, ground);
            bool isJumping = jump && isGrounded;
            bool isSprinting = sprint && t_vmove > 0 && !isJumping && isGrounded;

            if(isJumping)
            {
                rig.AddForce(Vector3.up* jumpForce);
            }

        }      
     
        if (isJumping)
        {
            rig.AddForce(Vector3.up * jumpForce);
        }

        void FixedUpdate()
        {
            float t_hmove = Input.GetAxisRaw("Horizontal");
            float t_vmove = Input.GetAxisRaw("Vertical");

            bool sprint = Input.GetKey(KeyCode.LeftShift);
            bool jump = Input.GetKey(KeyCode.Space);

            bool isGrounded = Physics.Raycast(groundDetector.position, Vector3.down, 0.1f, ground);
            bool isJumping = jump && isGrounded;
            bool isSprinting = sprint && t_vmove > 0 && !isJumping && isGrounded;

            Vector3 t_direction = new Vector3(t_hmove, 0, t_vmove);
            t_direction.Normalize();
        
            float t_adjustedSpeed = speed;
            if (isSprinting) t_adjustedSpeed *= sprintModifier;

            Vector3 t_targetVelocity = transform.TransformDirection(t_direction) * t_adjustedSpeed * Time.deltaTime;
            t_targetVelocity.y = rig.velocity.y;
            rig.velocity = t_targetVelocity;

            if (isSprinting) { normalCam.fieldOfView = Mathf.Lerp(normalCam.fieldOfView, baseFOV * sprintFOVModifier, Time.deltaTime * 8f); }
            else { normalCam.fieldOfView = Mathf.Lerp(normalCam.fieldOfView, baseFOV, Time.deltaTime * 8f); }
        }
}

Solution

  • Before you go any further, you'd do well to properly set up a decent IDE e.g. Visual Studio Community Edition or Visual Studio Code, both of which are free.

    When set up correctly, they'll point out most of your errors. The big three being:

    1

    private void update;
    {
       ..
    }
    

    Should be:

    private void Update()
    {
        ..
    }
    

    2

    This should be INSIDE a method, not outside of any method.

    if (isJumping)
    {
        rig.AddForce(Vector3.up * jumpForce);
    }
    

    3

    You've not closed off your braces. For each opening brace "{", you need a corresponding closing brace "}". A proper IDE would highlight this, otherwise you need to go through and count them. This is where the indentation of your code comes in handy.

    These are the ones I could see at a quick glance.