So I am creating a somewhat ambitious platformer/hack and slash game, and it is going pretty well.
However, while I have decent C# knowledge, I am no expert and am not very expirienced. This is my player controller, and I was just wondering if anyone has tips on how to clean it up and make it better. Its pretty messy right now, but there are a few things that need fixing:
- making it so that when it gets horizontal inputs it runs the running animation instead of having to make a seperate if statemnt(with flipping too)
- Making it so that you cant wall jump, as touching walls right now resets the jump, and also if you walk against them it bugs the character out and can break physics.
- Maybe make it expandablfe for custom controlls later on?
If you have any Ideas on how to acheive these, just tell me your ideas, I would love to hear some!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Sprites;
public class playerController : MonoBehaviour
{
// initialize all varaibles
private float speed = 8;
public GameObject hitbox;
public static float damage;
public static float coins = 0;
public static float LucaHP;
public static float LucaSP;
public bool grounded;
private float jumpspd = 12;
public Animator anim;
public bool isRunning;
public SpriteRenderer ren;
public bool canDash = true;
// Use this for initialization
void Start()
{
anim = gameObject.GetComponent<Animator>();
anim.SetBool("isRunning", false);
anim.SetBool("dashing", false);
canDash = true;
}
// Update is called once per frame
void Update()
{
playermove();
}
private void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "ground")
{
grounded = true;
}
}
void playermove()
{
float axixX = Input.GetAxis("Horizontal");
transform.Translate(new Vector3(axixX, 0) * Time.deltaTime * speed);
if (Input.GetButton("Horizontal"))
{
isRunning = true;
}
else
{
isRunning = false;
}
anim.SetBool("isRunning", isRunning);
if (Input.GetKeyDown(KeyCode.A))
{
ren.flipX = true;
}
if (Input.GetKeyDown(KeyCode.D))
{
ren.flipX = false;
}
if (Input.GetKeyDown(KeyCode.Space))
{
if (grounded == true)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpspd);
grounded = false;
}
}
if (Input.GetKeyDown(KeyCode.LeftShift))
{
StartCoroutine(dash());
}
}
IEnumerator dash()
{
if (canDash == true)
{
canDash = false;
anim.SetBool("dashing", true);
if (ren.flipX == true)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(-jumpspd / 2, GetComponent<Rigidbody2D>().velocity.y);
}
else
{
GetComponent<Rigidbody2D>().velocity = new Vector2(jumpspd / 2, GetComponent<Rigidbody2D>().velocity.y);
}
yield return new WaitForSeconds(0.5f);
anim.SetBool("dashing", false);
canDash = true;
}
}
}