@SamSayonara

Unity 2020.3.42f1 ругается на код, как быть?

Я писал код по видео с ютуба(Мне это нужно, так как я слегка подзабыл основу). Весь код я писал в точности как автор видео. И как только я дописал строки про состояние crouching и решил проверить работоспособность(также как и автор видео), то меня встретило несколько ошибок не дающих запустить тест. Ошибки и код я указал ниже. Заранее спасибо за ответ и помощь

> PlayerMovement.cs(111,5): error CS0106: The modifier 'private' is not valid for this item

> PlayerMovement.cs(140,5): error CS0106: The modifier 'private' is not valid for this item

> PlayerMovement.cs(154,5): error CS0106: The modifier 'private' is not valid for this item

> PlayerMovement.cs(166,5): error CS0106: The modifier 'private' is not valid for this item

> PlayerMovement.cs(173,5): error CS0106: The modifier 'private' is not valid for this item

> PlayerMovement.cs(177,2): error CS1513: } expected

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

    public class PlayerMovement : MonoBehaviour
    {
        [Header("Movement")]
        private float moveSpeed;
        public float walkSpeed;
        public float sprintSpeed;
    
        public float groundDrag;
    
        [Header("Jumping")]
        public float jumpForce;
        public float jumpCooldown;
        public float airMultiplier;
        bool readyToJump;
    
        [Header("Crouching")]
        public float crouchSpeed;
        public float crouchYScale;
        private float startYScale;
    
        [Header("Keybinds")]
        public KeyCode jumpKey = KeyCode.Space;
        public KeyCode sprintKey = KeyCode.LeftShift;
        public KeyCode crouchKey = KeyCode.LeftControl;
    
        [Header("Ground Check")]
        public float playerHeight;
        public LayerMask whatIsGround;
        bool grounded;
    
        public Transform orientation;
    
        float horizontalInput;
        float verticalInput;
    
        Vector3 moveDirection;
    
        Rigidbody rb;
    
        public MovementState state;
        public enum MovementState
        {
            walking,
            sprinting,
            crouching,
            air
        }
    
        private void Start()
        {
            rb = GetComponent<Rigidbody>();
            rb.freezeRotation = true;
    
            readyToJump = true;
    
            startYScale = transform.localScale.y;
        }
    
        private void Update()
        {
            // ground check
            grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.3f, whatIsGround);
    
            MyInput();
            SpeedControl();
            StateHandler();
    
            // handle drag
            if (grounded)
                rb.drag = groundDrag;
            else
                rb.drag = 0;
        }
    
        private void FixedUpdate()
        {
            MovePlayer();
        }
    
        private void MyInput()
        {
            horizontalInput = Input.GetAxisRaw("Horizontal");
            verticalInput = Input.GetAxisRaw("Vertical");
    
            // when to jump
            if (Input.GetKey(jumpKey) && readyToJump && grounded)
            {
                readyToJump = false;
    
                Jump();
    
                Invoke(nameof(ResetJump), jumpCooldown);
            }
    
            //start crouch
            if(Input.GetKeyDown(crouchKey))
            {
                transform.localScale = new Vector3(transform.localScale.x, crouchYScale, transform.localScale.z);
                rb.AddForce(Vector3.down * 5f, ForceMode.Impulse);
            }
    
            if (Input.GetKeyUp(crouchKey))
            {
                transform.localScale = new Vector3(transform.localScale.x, startYScale, transform.localScale.z);
            }
    
        private void StateHandler()
        {
            //Mode - Crouching
            if(Input.GetKey(crouchKey))
            {
                state = MovementState.crouching;
                moveSpeed = crouchSpeed;
            }
            
            //Mode - Sprinting 
            if(grounded && Input.GetKey(sprintKey))
            {
                state = MovementState.sprinting;
                moveSpeed = sprintSpeed;
            }
    
            //Mode - Walking
            else if(grounded)
            {
                state = MovementState.walking;
                moveSpeed = walkSpeed;
            }
    
            //Mode - Air
            {
                state = MovementState.air;
            }
        }
    
        private void MovePlayer()
        {
            // calculate movement direction
            moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
    
            // on ground
            if (grounded)
                rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
    
            // in air
            else if (!grounded)
                rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
        }
    
        private void SpeedControl()
        {
            Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
    
            // limit velocity if needed
            if (flatVel.magnitude > moveSpeed)
            {
                Vector3 limitedVel = flatVel.normalized * moveSpeed;
                rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
            }
        }
    
        private void Jump()
        {
            // reset y velocity
            rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
    
            rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
        }
        private void ResetJump()
        {
            readyToJump = true;
        }
    }
  • Вопрос задан
  • 136 просмотров
Решения вопроса 1
@sand3001
Всего по немногу
private void MyInput() отсутствует закрывающая фигурная скобка
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы