You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity新手开发游戏遇CS1022错误,代码调试无果求助

解决Unity CS1022错误:Type or namespace definition, or end-of-file expected

错误原因及修复点

你的代码存在多处语法错误,导致编译器无法正确解析类结构,具体问题如下:

  • 方法定义语法错误:代码中private void update;是非法写法,Unity的Update方法正确定义应为private void Update()(注意大写U,使用括号而非分号)。
  • 代码块位置错误:Update方法结束后,有一段if (isJumping)代码直接暴露在类作用域中,C#不允许类作用域下存在独立执行代码块,必须将其移入方法内部。
  • 大小写错误:camera.main.enabled = false;中的camera需改为大写Camera,Unity的Camera类为大写开头。
  • 重复代码冗余:存在两段完全相同的if (isJumping)代码块,需移除多余的那一段。

修正后的完整代码

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);
            }
        }     

        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); }
        }
    }
}

内容的提问来源于stack exchange,提问作者kman371

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 15:30:49