Hi,
I have been working on animation and movement programming for my character. Although I have gotten part of what I need, it still lacks work.
I want to show you in the next video what my character looks like in “action”.
At the beginning the character only moves from one side to the other, I think there is no problem with it. Later, he makes a move to get away, from some attack perhaps. I think it lacks some detail.
After that, the character jumps onto platforms. However, it seems to me that the jump is very “slow” and I would like to adjust that. It seems to jump in slow motion.
Also when it falls from the platform, it looks like it is “walking in the air” … I think I can fix that with a ground point.
Before the end, he makes an attack by moving a magic wand, yes, it is a wizard. I have not yet configured the shot or magic.
Finally, the character runs, with a different animation than walking, however, as you can see in the video, it still does not work well. Still, the character stopping the animation continues. I’m still not sure how to stop it, I have not found at what point to configure this.
For this, the character only executes running when his previous state is walking. It cannot run when it is stopped, jumping or shooting. Always run first walk and then run.
In the following code you can see what I have worked on and maybe you can tell me where I can improve my script. I would thank you a lot.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Rigidbody2D playerRb2d;
private Animator playerAnimator;
[SerializeField]
private float playerWalkSpeed;
private bool attack;
private bool isRunning = false;
public float jumpForce;
public float playerRunSpeed = 0;
public float retreatSpeed;
public float retreatForce;
private bool facingRight;
// Revisar doble click variables
public float timer = 0f;
public float clickSpeed = 0.5f;
// Start is called before the first frame update
void Start()
{
facingRight = true;
playerRb2d = GetComponent<Rigidbody2D>();
playerAnimator = GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate()
{
float horizontal = Input.GetAxis("Horizontal");
HandleMovement(horizontal);
Flip(horizontal);
HandleAttack();
DblClickCheck(horizontal);
ResetValues();
}
void Update() {
HandleInput();
}
private void HandleMovement(float horizontal) {
if (!this.playerAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Attack"))
{
playerRb2d.velocity = new Vector2(horizontal * (playerWalkSpeed + playerRunSpeed), playerRb2d.velocity.y);
}
playerAnimator.SetFloat("speedWalk", Mathf.Abs(horizontal));
}
private void HandleAttack() {
if (attack && !this.playerAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Attack"))
{
playerAnimator.SetTrigger("attackNormal");
playerRb2d.velocity = Vector2.zero;
}
}
private void HandleJump() {
playerRb2d.AddForce(transform.up * jumpForce);
playerAnimator.SetTrigger("isJumping");
}
private void HandleRetreat() {
if (facingRight)
{
playerRb2d.AddForce(Vector2.left * retreatSpeed * retreatForce);
}
else if(!facingRight) {
playerRb2d.AddForce(Vector2.right * retreatSpeed * retreatForce);
}
playerAnimator.SetTrigger("isRetreat");
}
private void HandleInput() {
if (Input.GetKeyDown(KeyCode.Q)) {
attack = true;
}
if (Input.GetKeyDown(KeyCode.Space)) {
HandleJump();
}
if (Input.GetKeyDown(KeyCode.E)) {
HandleRetreat();
}
}
private void DblClickCheck(float horizontal) {
if (timer < clickSpeed) {
timer += Time.deltaTime;
}
if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.D)) {
if (timer < clickSpeed)
{
playerRunSpeed = 10;
playerAnimator.SetBool("isRunning", true);
}
else {
playerRunSpeed = 0;
playerAnimator.SetBool("isRunning", false);
}
timer = 0;
}
}
private void Flip(float horizontal) {
if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight) {
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
private void ResetValues() {
attack = false;
isRunning = false;
}
}
In the following images, I show you how I have the configuration of the player and the animation tree, focused on the option to run.
And this is what I’m wearing at the moment. Any help or advice would appreciate it.
Greetings!