I am at present engaged on the PlayerController for my 2D Combating Recreation in the meanwhile
Here is my code:
utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
public class PlayerController : MonoBehaviour
{
non-public Rigidbody2D rigid2D;
non-public BoxCollider2D playerCollider;
[SerializeField] non-public LayerMask jumpableGround;
[SerializeField] non-public TrailRenderer playerTrailRenderer;
public float pace = 5;
public float jumpForce = 1;
non-public float playerInputX; //Examine for what button participant pressed horizontally -1 = Left, 0 = Idle, 1 = Proper.
non-public bool canDash = true;
non-public bool isDashing;
public float dashingPower = 24f;
public float dashingTime = 0.2f;
public float dashingCooldown = 1f;
non-public Animator animator;
non-public string currentState;
//Participant Animation States
const string PLAYER_IDLE = "Idle";
const string PLAYER_FORWARD = "Ahead";
const string PLAYER_BACK = "Backward";
ControlManager controlManager;
int CurrentComboPriority = 0;
// Begin is known as earlier than the primary body replace
void Awake()
{
rigid2D = GetComponent<Rigidbody2D>();
playerCollider = GetComponent<BoxCollider2D>();
animator = GetComponent<Animator>();
if (controlManager == null)
{
controlManager = FindObjectOfType<ControlManager>();
}
currentState = PLAYER_IDLE;
}
void ChangeAnimationState(string newState)
{
//Cease identical animation from interrupting itself
if (currentState == newState) return;
//Play Animation
animator.Play(newState);
//reassingn present state
currentState = newState;
}
// Replace is known as as soon as per body
void Replace()
{
//Motion Logic
if (isDashing)
{
return;
}
playerInputX = Enter.GetAxisRaw("Horizontal");
if (isGrounded())
{
rigid2D.velocity = new Vector2(playerInputX * pace, rigid2D.velocity.y);
}
if (Enter.GetButtonDown("Up") && isGrounded())
{
rigid2D.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
}
if (Enter.GetKeyDown(KeyCode.RightShift) && canDash)
{
StartCoroutine(Sprint());
}
//Motion Animations
if (isGrounded() && playerInputX > 0)
{
ChangeAnimationState(PLAYER_FORWARD);
}
else if (isGrounded() && playerInputX < 0)
{
ChangeAnimationState(PLAYER_BACK);
}
else
{
ChangeAnimationState(PLAYER_IDLE);
}
}
non-public bool isGrounded()
{
return Physics2D.BoxCast(playerCollider.bounds.heart, playerCollider.bounds.measurement, 0f, Vector2.down, .1f, jumpableGround);
}
non-public IEnumerator Sprint()
{
canDash = false;
isDashing = true;
float originalGravity = rigid2D.gravityScale;
rigid2D.gravityScale = 0f;
rigid2D.velocity = new Vector2(playerInputX * dashingPower, 0f);
playerTrailRenderer.emitting = true;
yield return new WaitForSeconds(dashingTime);
playerTrailRenderer.emitting = false;
rigid2D.gravityScale = originalGravity;
rigid2D.velocity = new Vector2(playerInputX * pace, 0f);
isDashing = false;
yield return new WaitForSeconds(dashingCooldown);
canDash = true;
}
public void PlayMove(Strikes transfer, int ComboPriority) //Get the Transfer and the Precedence
{
if (Strikes.None != transfer) //if the transfer is none ignore the operate
{
if (ComboPriority >= CurrentComboPriority) //if the brand new transfer is increased Precedence play it and ignore every thing else
{
CurrentComboPriority = ComboPriority; //Set the brand new Combo
controlManager.ResetCombo(); //Reset the Checklist within the ControlsManager
}
else
return;
//Print Transfer in Debug Log (To be modified with precise assault animations)
change (transfer)
{
case Strikes.LightPunch:
Debug.Log("Gentle Punch");
break;
case Strikes.LightKick:
Debug.Log("Gentle Kick");
break;
case Strikes.MediumPunch:
Debug.Log("Medium Punch");
break;
case Strikes.MediumKick:
Debug.Log("Medium Kick");
break;
case Strikes.HeavyPunch:
Debug.Log("Heavy Punch");
break;
case Strikes.HeavyKick:
Debug.Log("Heavy Kick");
break;
case Strikes.RapidKick:
Debug.Log("Fast Kick");
break;
case Strikes.CorkscrewKick:
Debug.Log("Corkscrew Kick");
break;
}
CurrentComboPriority = 0; //Reset the Combo Precedence
}
}
}
The precise motion, dashing, and floor detection logic I coded for myself, however for the animation and combo stuff/All of 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 parts off the Combo system tutorial, so Strikes, ControlManager, Strikes 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 the easiest way 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 Change Statements because it’s one thing I am used to coming from Gamemaker Studio 2: https://www.youtube.com/watch?v=db0KWYaWfeM
Has anybody obtained any recommendation on turning my code to suit this type of state machine? The way in which my code is now, I am nervous that if I proceed taking place this route it’s going to ultimately flip into spaghetti code with too many if statements and whatnot.