Hey Guys
So I edited my character controller script to check for Ground before handling the “if” statements to change the animation parameters. it was working fairly well before but now my 1st jump animation gets stuck at frame one the double jump works as intended…
Below is my code and screen shots of the transition settings, i don’t know…![]()
any help will be appreciated.
Also I have a problem where if i quickly press jump the moment the character lands the animations don’t play at all, this is true for both the Jump animations.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControllerScript : MonoBehaviour
{
Animator animator;
Rigidbody2D rb2d;
SpriteRenderer spriteRenderer;
bool isGrounded;
private int weaponSwingCount = 0;
public float velocity = 1f;
public float jumpVelocity = 5f;
float axisHorizontal = 0f;
float moveHorizontal = 0f;
[SerializeField]
Transform groundCheck;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
rb2d = GetComponent<Rigidbody2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void FixedUpdate()
{
axisHorizontal = Input.GetAxisRaw("Horizontal");//collect axis also used for Flip X Calc
moveHorizontal = DigitizeAnalogInput(axisHorizontal);
rb2d.velocity = new Vector2(moveHorizontal, rb2d.velocity.y); // change in velocity
animator.SetFloat("Speed", Mathf.Abs(moveHorizontal)); //set parameter speed to 1 or 0
isGrounded = GroundedCheck(isGrounded); //grounded check
if (isGrounded)
{
if (Input.GetButtonDown("Jump"))//1st Jump
{
rb2d.AddForce(transform.up * jumpVelocity, ForceMode2D.Impulse);
animator.SetBool("IsJumping", true);
}
if (Input.GetButtonDown("Fire3") && !(animator.GetBool("IsSwordDrawn")))//Drawing Sword
{
animator.SetBool("IsSwordDrawn", true);
}
else if (Input.GetButtonDown("Fire3") && animator.GetBool("IsSwordDrawn"))//Sheathing Sword
{
animator.SetBool("IsSwordDrawn", false);
animator.SetInteger("Swing", 0);
}
if (animator.GetBool("IsSwordDrawn") && rb2d.velocity.x == 0)
{
if (Input.GetButtonDown("Fire1") && weaponSwingCount == 0)//Grounded Attack
{
++weaponSwingCount;
animator.SetInteger("Swing", 1);
animator.SetTrigger("SwingTrigger");
Debug.Log(weaponSwingCount);
}
else if (Input.GetButtonDown("Fire1") && weaponSwingCount == 1)
{
++weaponSwingCount;
animator.SetInteger("Swing", 2);
animator.SetTrigger("SwingTrigger");
Debug.Log(weaponSwingCount);
}
else if (Input.GetButtonDown("Fire1") && weaponSwingCount == 2)
{
++weaponSwingCount;
animator.SetInteger("Swing", 3);
animator.SetTrigger("SwingTrigger");
Debug.Log(weaponSwingCount);
}
else if (weaponSwingCount == 3)
{
weaponSwingCount = 0;
}
}
}
else if(!(isGrounded))
{
if (Input.GetButtonDown("Jump") && animator.GetBool("IsJumping"))
{
rb2d.AddForce(transform.up * jumpVelocity, ForceMode2D.Impulse);
animator.SetBool("IsJumping", false);
animator.SetBool("IsSummersalting", true);
}
}
FlipX(spriteRenderer, axisHorizontal);
}
bool GroundedCheck(bool grounded)
{
if (Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Player")))
{
grounded = true;
animator.SetBool("IsJumping", false);
animator.SetBool("IsSummersalting", false);
}
else
{
grounded = false;
}
return grounded;
}
void FlipX(SpriteRenderer sr, float horizontal)
{
if (axisHorizontal < 0)
{
spriteRenderer.flipX = true;
}
if (axisHorizontal > 0)
{
spriteRenderer.flipX = false;
}
}
int DigitizeAnalogInput(float axisMovement)//change input from controller to whole numbers
{
const float threshhold = 0.7f;
if (axisMovement > threshhold) return 1;
if (axisMovement < -threshhold) return -1;
return 0;
}
}

