I
I
Ivan Tumanow2021-05-24 11:35:22
Unity
Ivan Tumanow, 2021-05-24 11:35:22

How to format Unity code properly?

I'm making a game on Unity, and in the game I only have wind force on the player.
How should I arrange the player script correctly: I created two classes, one PlayerController, the other static Wind. I just don’t really want to describe the player’s physics and other physical phenomena in the PlayerControl class. What would be the best way to do it?

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

[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour
{
    private CharacterController _controller;
    // Скорость игрока
    [SerializeField] private float _playerVelocity = 2.5f;
    // Скорость балансировки игрока
    [SerializeField] private float _balanceForce = 10.0f;
    // Точка вращения игрока
    [SerializeField] private Transform _pointOfRotateCharacter;
    [SerializeField] private float _playerRotationVelocity = 3.5f;
    private float _changeRotationZ;
    // Изменение направления вращения
    private float _rotationDeflect = 0.0f;

    private void Start(){
        _controller = GetComponent<CharacterController>();
        _changeRotationZ = _pointOfRotateCharacter.rotation.z;
    }

    private void Update(){
        Move();
        ChangeBalance();
    }

    private void Move(){
        // Метод передвижения игрока
        _controller.Move(_playerVelocity * transform.forward * Time.deltaTime);
    }

    private void ChangeBalance(){
        // Метод балансировки игрока
        _rotationDeflect = SetRotationDeflect(_rotationDeflect);
        // Учитывается скорость ветра + направление вращения
        _changeRotationZ += (Wind.WindForce(_pointOfRotateCharacter.rotation.z) * _rotationDeflect) * Time.deltaTime;

        if (Input.GetKeyDown(KeyCode.A)){
            _changeRotationZ += _balanceForce;
        } else if ((Input.GetKeyDown(KeyCode.D))){
            _changeRotationZ -= _balanceForce;
        }

        Quaternion deformRotation = Quaternion.Euler(_pointOfRotateCharacter.rotation.x, _pointOfRotateCharacter.rotation.y, _changeRotationZ);
        _pointOfRotateCharacter.rotation = Quaternion.Lerp(_pointOfRotateCharacter.rotation, deformRotation, _playerRotationVelocity * Time.deltaTime);
        // 0.7 это ~90 градусов
        if (Mathf.Abs(_pointOfRotateCharacter.rotation.z) > 0.7f) {
            Time.timeScale = 0.0f;
        }
    }

    private float SetRotationDeflect(float rotDefl){
        // Метод для изменения направления вращения игрока(чтобы игрок не уходил по обратной оси)
        if(_rotationDeflect == 0.0f){
            float[] deflection = {-1f, 1f};
            rotDefl = deflection[Random.Range(0, deflection.Length)];
        } else {
            rotDefl = _pointOfRotateCharacter.rotation.z > 0.0f ? -1.0f : 1.0f;
        }
        return rotDefl;
    }
}

public static class Wind{
    // Скорость ветра
    public static float WindVelocity = 3f;

    public static float WindForce(float changeParameter){
        // Метод для влияния скорости на некоторые параметры
        changeParameter -= WindVelocity * (Random.Range(1, 3) * 7.0f);
        return changeParameter;
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question