[ad_1]
I’m at present engaged on the PlayerController for my 2D Fighting Game in the meanwhile
Here’s my code:
utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
public class PlayerController : MonoBehaviour
{
personal Rigidbody2D rigid2D;
personal BoxCollider2D playerCollider;
[SerializeField] personal LayerMask jumpableGround;
[SerializeField] personal TrailRenderer playerTrailRenderer;
public float pace = 5;
public float jumpForce = 1;
personal float participantInputX; //Check for what button participant pressed horizontally -1 = Left, 0 = Idle, 1 = Right.
personal bool canDash = true;
personal bool isDashing;
public float dashingPower = 24f;
public float dashingTime = 0.2f;
public float dashingCooldown = 1f;
personal Animator animator;
personal string currentState;
//Player Animation States
const string PLAYER_IDLE = "Idle";
const string PLAYER_FORWARD = "Forward";
const string PLAYER_BACK = "Backward";
ControlManager controlManager;
int PresentComboPriority = 0;
// Start known as earlier than the primary body replace
void Awake()
{
rigid2D = GetElement<Rigidbody2D>();
playerCollider = GetElement<BoxCollider2D>();
animator = GetElement<Animator>();
if (controlManager == null)
{
controlManager = FindObjectOfType<ControlManager>();
}
currentState = PLAYER_IDLE;
}
void ChangeAnimationState(string newState)
{
//Stop similar animation from interrupting itself
if (currentState == newState) return;
//Play Animation
animator.Play(newState);
//reassingn present state
currentState = newState;
}
// Update known as as soon as per body
void Update()
{
//Movement Logic
if (isDashing)
{
return;
}
participantInputX = Input.GetAxisRaw("Horizontal");
if (isGrounded())
{
rigid2D.velocity = new Vector2(participantInputX * pace, rigid2D.velocity.y);
}
if (Input.GetButtonDown("Up") && isGrounded())
{
rigid2D.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
}
if (Input.GetKeyDown(KeyCode.RightShift) && canDash)
{
StartCoroutine(Dash());
}
//Movement Animations
if (isGrounded() && participantInputX > 0)
{
ChangeAnimationState(PLAYER_FORWARD);
}
else if (isGrounded() && participantInputX < 0)
{
ChangeAnimationState(PLAYER_BACK);
}
else
{
ChangeAnimationState(PLAYER_IDLE);
}
}
personal bool isGrounded()
{
return Physics2D.BoxCast(playerCollider.bounds.heart, playerCollider.bounds.dimension, 0f, Vector2.down, .1f, jumpableGround);
}
personal IEnumerator Dash()
{
canDash = false;
isDashing = true;
float originalGravity = rigid2D.gravityScale;
rigid2D.gravityScale = 0f;
rigid2D.velocity = new Vector2(participantInputX * dashingPower, 0f);
playerTrailRenderer.emitting = true;
yield return new WaitForSeconds(dashingTime);
playerTrailRenderer.emitting = false;
rigid2D.gravityScale = originalGravity;
rigid2D.velocity = new Vector2(participantInputX * pace, 0f);
isDashing = false;
yield return new WaitForSeconds(dashingCooldown);
canDash = true;
}
public void PlayMove(Moves transfer, int ComboPriority) //Get the Move and the Priority
{
if (Moves.None != transfer) //if the transfer is none ignore the perform
{
if (ComboPriority >= PresentComboPriority) //if the brand new transfer is increased Priority play it and ignore every part else
{
PresentComboPriority = ComboPriority; //Set the brand new Combo
controlManager.ResetCombo(); //Reset the List within the ControlsManager
}
else
return;
//Print Move in Debug Log (To be modified with precise assault animations)
swap (transfer)
{
case Moves.LightPunch:
Debug.Log("Light Punch");
break;
case Moves.LightKick:
Debug.Log("Light Kick");
break;
case Moves.MediumPunch:
Debug.Log("Medium Punch");
break;
case Moves.MediumKick:
Debug.Log("Medium Kick");
break;
case Moves.HeavyPunch:
Debug.Log("Heavy Punch");
break;
case Moves.HeavyKick:
Debug.Log("Heavy Kick");
break;
case Moves.RapidKick:
Debug.Log("Rapid Kick");
break;
case Moves.CorkscrewKick:
Debug.Log("Corkscrew Kick");
break;
}
PresentComboPriority = 0; //Reset the Combo Priority
}
}
}
The precise motion, dashing, and floor detection logic I coded for myself, however for the animation and combo stuff/All the transfer debug logs, it is a mixture of those two tutorials:
https://www.youtube.com/watch?v=nBkiSJ5z-hE https://www.youtube.com/watch?v=avl2bSyL9ZA
I’ve all of the elements off the Combo system tutorial, so Moves, ControlManager, Moves Enum, and the ScriptableObjects for each love with their properties. The Debug Logs in my Script are to get replaced with animations too
I took a glance into state machines in Unity, as state machines are undoubtedly one of the best ways to deal with characters in a combating recreation, and I actually like the way in which it’s dealt with on this tutorial, utilizing enums and Switch Statements because it’s one thing I’m used to coming from Gamemaker Studio 2: https://www.youtube.com/watch?v=db0KWYaWfeM
Has anybody bought any recommendation on turning my code to suit this type of state machine? The means my code is now, I’m nervous that if I proceed happening this route it will ultimately flip into spaghetti code with too many if statements and whatnot.
[ad_2]