Здравствуйте!
Во время написания кода задался вопросом: А "правильно" ли я все делаю?
Не хотелось бы углубляться дальше, не будучи уверенным в уже написанном
В общем, нужна критика/совет со стороны, как это все дело красиво прибрать, что можно подправить, изменить и т.д)
Имеется вот такой скрипт управления персонажем:
Код
using UnityEngine;
using System.Collections;
public class BasicPlatformerController01 : MonoBehaviour {
#region Variables
[Header("Basic Variables")]
public float Speed = 45.0f;
public float JumpForce = 60.0f;
[Range(0.0f,1.0f)] public float CrouchingSpeed = 0.5f;
public bool UseRawInput = true;
public Transform GroundCheckLeft, GroundCheckRight, HeadCheckPosition;
public LayerMask WhatIsGround;
protected float move;
protected bool facingRight = true;
protected bool grounded = false;
protected bool doubleJump = false;
protected bool canStand = true;
protected bool crouching = false;
protected float headTouchRadius = 1.5f;
protected Rigidbody2D rigi;
protected Animator anim;
protected BoxCollider2D coli;
#endregion
void Start ()
{
// Get necessary components
StartFunc ();
}
void Update ()
{
Jumping ();
Crouching ();
}
void FixedUpdate()
{
MainInput ();
Movement ();
}
/// <summary>
/// Changing localScale.x to flip character
/// </summary>
public void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
// Reseting level when trigger was touched
public void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Bottom")
{
Application.LoadLevel(Application.loadedLevel);
}
}
public void StartFunc()
{
rigi = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
coli = GetComponent<BoxCollider2D>();
}
/// <summary>
/// Getting Horizontal movement
/// </summary>
public void MainInput()
{
if(UseRawInput)
move = Input.GetAxisRaw("Horizontal");
else
move = Input.GetAxis("Horizontal");
if (crouching)
move *= CrouchingSpeed;
}
/// <summary>
/// Movement function.
/// Use it in FixedUpdate()
/// </summary>
public void Movement()
{
// Setting bools
grounded = Physics2D.Linecast(transform.position, GroundCheckLeft.position, WhatIsGround)
|| Physics2D.Linecast(transform.position, GroundCheckRight.position, WhatIsGround);
canStand = !Physics2D.OverlapCircle(HeadCheckPosition.position, headTouchRadius, WhatIsGround);
// Setting bools
if (grounded)
doubleJump = false;
anim.SetBool("Grounded", grounded);
// Setting anim velocity bools
anim.SetFloat("Speed", Mathf.Abs(move));
anim.SetFloat("vSpeed", rigi.velocity.y);
// When grounded - moving by changing velocity
// When in the air - moving by adding force
if (grounded)
rigi.velocity = new Vector2 (move * Speed, rigi.velocity.y);
else
rigi.AddForce (new Vector2(move * Speed * 4, 0.0f));
// Speed limit
if (Mathf.Abs (rigi.velocity.x) > Speed)
{
rigi.velocity = new Vector2(Speed * transform.localScale.x, rigi.velocity.y);
}
// Changing local scale to flip the character
if(rigi.velocity.x > 0.1f && !facingRight)
Flip();
else if(rigi.velocity.x < -0.1f && facingRight)
Flip();
}
/// <summary>
/// Crouching function.
/// Use it in Update()
/// </summary>
public void Crouching()
{
// Disable BigCollider when crouching
// Small colliders still there
if ((Input.GetAxisRaw("Vertical") == -1) && grounded)
{
crouching = true;
}
else if(canStand)
{
crouching = false;
}
coli.enabled = !crouching;
anim.SetBool("Crouch", crouching);
}
/// <summary>
/// Jumping & Double Jumping
/// Use it in Update() function
/// </summary>
public void Jumping()
{
// Jumping by adding force to rigidbody
if((grounded || !doubleJump) && Input.GetButtonDown("Jump"))
{
// Set "Y" velocity to 0 for correct jump when falling
rigi.velocity = new Vector2(rigi.velocity.x, 0);
// Adding force
anim.SetBool("Grounded", false);
rigi.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
if(!doubleJump && !grounded)
doubleJump = true;
}
}
}
И класс-потомок(коих еще будет несколько):
Код
using UnityEngine;
using System.Collections;
public class NinjaController01 : BasicPlatformerController01 {
[Header("Ninja Variables")]
public float ThrowingPower = 10000.0f;
public float WallJumpPushForce = 750.0f;
public Rigidbody2D SurikenPrefab;
public Transform WallCheckLeft, WallCheckRight;
public LayerMask WhatIsWall;
protected bool onWall = false;
protected bool wallJumping = false;
protected float wallTouchRadius = 0.1f;
// Use this for initialization
void Start () {
StartFunc ();
}
// Update is called once per frame
void Update () {
Jumping ();
WallJumping ();
Crouching ();
}
void FixedUpdate()
{
if(!wallJumping)
MainInput ();
Movement ();
}
public void WallJumping()
{
onWall = Physics2D.OverlapCircle (WallCheckLeft.position, wallTouchRadius, WhatIsWall)
|| Physics2D.OverlapCircle (WallCheckRight.position, wallTouchRadius, WhatIsWall);
anim.SetBool ("OnWall", onWall);
if (grounded)
{
wallJumping = false;
}
else if (doubleJump && onWall && Input.GetButtonDown ("Jump"))
{
Flip ();
rigi.velocity = new Vector2(rigi.velocity.x, 0);
rigi.AddForce(new Vector2(this.transform.localScale.x, 1.0f) * WallJumpPushForce, ForceMode2D.Impulse);
wallJumping = true;
}
}
}
С нетерпением жду советов