jump animation ending too quickly in first jump - c#

void FixedUpdate()
{
moveInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
anim.SetBool("isWalking", true);
if (facingRight == false && moveInput>0)
{
flip();
}
else if (facingRight == true && moveInput < 0)
{
flip();
}
else if (moveInput == 0)
{
anim.SetBool("isWalking", false);
}
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
}
void Update()
{
if (isGrounded == true)
{
Debug.Log("is grounded");
extraJumps = extraJumpValue;
anim.SetBool("isJumping", false);
}
if (Input.GetKeyDown(KeyCode.Space) && extraJumps + 1.0f > 0)
{
jump();
extraJumps--;
}
}
void flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
void jump()
{
rb.velocity = Vector2.up * jumpForce;
anim.SetBool("isJumping", true);
}
}
I just started to learn unity starting by following tutorials on YouTube. However I can't figure out why my jump animation is ending as soon as it starts on my first jump. On extra jumps it works fine. If you know how to fix this, pls help me.

In your update loop you call Jump which sets
anim.SetBool("isJumping", true);
and in your fixed update loop you have
anim.SetBool("isWalking", true);
So immediately after calling Jump on the next 'FixedUpdate' you are setting the animation bool of iswalking, unless moveInput is 0. I can't see all your code or what is happening to moveInput but I can tell you this is where your problem is. You need to change the animations of this code:
anim.SetBool("isWalking", true);
if (facingRight == false && moveInput>0)
{
flip();
}
else if (facingRight == true && moveInput < 0)
{
flip();
}
else if (moveInput == 0)
{
anim.SetBool("isWalking", false);
}

Related

Player constantly has a velocity of -7 on Y axis

I recently started making a new project and I'm still a beginner at coding. But recently I've made a jumping animation, and a falling animation. I've implemented those two things, and I made it so that if the Player has a velocity < 0 then activate falling anim. But, for some reason every time I press play the Player always has a velocity of -7 on the Y axis and never 0. Here's the code:
void OnMove()
{
Vector2 vec = actions.Movement.Move.ReadValue<Vector2>();
playerVelocity = new Vector2 (vec.x * speed, rb2d.velocity.y);
rb2d.velocity = playerVelocity;
}
void OnJump()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
if(isGrounded)
{
animator.SetBool("isFalling", false);
}
if(isGrounded && actions.Movement.Jump.GetButtonDown())
{
isJumping = true;
jumpTimeCounter = jumpTime;
rb2d.velocity += new Vector2 (0f, jumpForce);
}
if(actions.Movement.Jump.GetButton() && isJumping == true)
{
animator.ResetTrigger("hasJumped");
animator.SetBool("isFalling", false);
animator.SetTrigger("hasJumped");
if (jumpTimeCounter > 0)
{
rb2d.velocity = new Vector2 (0f, jumpForce);
jumpTimeCounter -= Time.deltaTime;
animator.SetTrigger("hasJumped");
}
else
{
isJumping = false;
animator.SetBool("isFalling", false);
animator.ResetTrigger("hasJumped");
}
}
if(rb2d.velocity.y < 0)
{
animator.SetBool("isFalling", true);
}
}
Here's the inspector:
What am I doing wrong?

JumpCount resets immediately after jumping 2D platformer

I'm pretty new to all of this so sorry for rookie mistakes. I'm making a 2D platformer
I am checking for ground via RayCast and right after the player jumps, the jumpCounter resets to 0, so I get a bonus jump. I tried adding a 0.2s timer before it can reset but that caused trouble when jumping multiple times in a row (bunny hopping). Any help?
private void FixedUpdate()
{
GroundCheck();
if (state != State.hurt)
{
Movement();
DoubleJump();
StateMachine();
}
HurtCheck();
anim.SetInteger("state", (int)state);
}
private void Movement()
{
//Input.GetAxis returns a value between -1 up to 1
//Edit> Project Settings> Input> Axis> Horizontal
float hDirection = Input.GetAxisRaw("Horizontal");
//holding down "D" makes the value positive and vice versa
if (hDirection < 0)
{
rb.velocity = new Vector2(-speed, rb.velocity.y);
transform.localScale = new Vector2(-1, 1);
}
else if (hDirection > 0)
{
rb.velocity = new Vector2(speed, rb.velocity.y);
transform.localScale = new Vector2(1, 1);
}
else
{
}
if (Input.GetButtonDown("Jump") && isGrounded == true)
{
Jump();
}
}
private void Jump()
{
rb.velocity = new Vector2(rb.velocity.x, jumpforce);
state = State.jumping;
jumpCount += 1;
}
private void GroundCheck()
{
Debug.DrawRay(transform.position, Vector2.down * hitDistance, Color.green);
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, hitDistance, ground);
if(hit.collider != null)
{
isGrounded = true;
jumpCount = 0;
}
else
{
isGrounded = false;
}
}
private void DoubleJump()
{
if (Input.GetButtonDown("Jump") && isGrounded == false && jumpCount < 2)
{
rb.velocity = new Vector2(rb.velocity.x, jumpforce);
jumpCount += 1;
}
}
Hey Have you made sure your player's layer isn't ground layer or set ground layermask properly or maybe the hitDistance for the raycast is too high it should be very low like 0.1 or less or does jumpCount start from 0 or 1 try and alternate that or maybe have it start from two to lose the extra bonus jump...These would be where I would look, Good luck!

Falling animation when on slopes

I'm pretty new to all of this so sorry for rookie mistakes.
I've built a State Machine that I'm pretty happy with, with just one problem, whenever the player jumps on a slope and keeps running (either up or down) the animation won't switch to running and stays at falling.
private void FixedUpdate()
{
GroundCheck();
if (state != State.hurt)
{
Movement();
DoubleJump();
StateMachine();
}
HurtCheck();
anim.SetInteger("state", (int)state);
}
private void Movement()
{
float hDirection = Input.GetAxis("Horizontal");
//holding down "D" makes the value positive and vice versa
if (hDirection < 0)
{
rb.velocity = new Vector2(-speed, rb.velocity.y);
transform.localScale = new Vector2(-1, 1);
}
else if (hDirection > 0)
{
rb.velocity = new Vector2(speed, rb.velocity.y);
transform.localScale = new Vector2(1, 1);
}
else
{
}
if (Input.GetButtonDown("Jump") && isGrounded == true)
{
Jump();
}
}
private void StateMachine()
{
if(rb.velocity.y > 2f && isGrounded == false)
{
state = State.jumping;
}
if(rb.velocity.y < 2f && isGrounded == false)
{
state = State.falling;
}
if(rb.velocity.y == 0 && Mathf.Abs(rb.velocity.x) > 2f)
{
state = State.running;
}
if(rb.velocity.magnitude == 0)
{
state = State.idle;
}
}
It looks like you intend rb.velocity.y to be greater than 0 when on a slope. But in order to enter the running state you have a condition that rb.velocity.y == 0. If you want to run on a slope, then maybe you should change that condition like this.
if((rb.velocity.y < 2f) && (Mathf.Abs(rb.velocity.x) > 2f))
{
state = State.running;
}

How to fix 2D animation while player walks?

I'm trying to make my character idle, run, jump and fall. I did it, but i have bug with runing animation. While he goes left or right, he doesn't play animation "run", which called by integer "noob" with parameter "2",he plays animation "fall", which "noob" is parameter "4"
I'm using Unity 4.5.x and C# code
using UnityEngine;
using System.Collections;
public class movement : MonoBehaviour {
Rigidbody2D rb;
Animator anim;
public int jumpCount = 0;
public Camera main;
float jumpTimer = 0;
void Start () {
rb = GetComponent<Rigidbody2D> ();
anim = GetComponent<Animator>();
}
void Update () {
if (Input.GetKeyDown (KeyCode.X) && jumpCount < 2 && jumpTimer == 0) {
jump ();
jumpCount += 1;
jumpTimer = 1;
StartCoroutine("jumpTime");
}
if (rb.velocity.y > 0) {
Flip();
anim.SetInteger ("noob", 3);
}
if (rb.velocity.y < 0) {
Flip();
anim.SetInteger ("noob", 4);
}
if (Input.GetAxis ("Horizontal") == 0 && rb.velocity.y == 0) {
anim.SetInteger ("noob", 1);
}
if (rb.velocity.y == 0 && Input.GetAxis ("Horizontal") != 0) {
Flip();
anim.SetInteger("noob", 2);
}
if (rb.velocity.y == 0) {
jumpCount = 0;
}
}
void FixedUpdate(){
rb.velocity = new Vector2 (Input.GetAxis ("Horizontal") * 12f, rb.velocity.y);
}
void jump(){
rb.AddForce (transform.up * 14F, ForceMode2D.Impulse);
}
void Flip() {
if (Input.GetAxis ("Horizontal") > 0) {
transform.localRotation = Quaternion.Euler (0, 0, 0);
}
if (Input.GetAxis ("Horizontal") < 0) {
transform.localRotation = Quaternion.Euler (0, 180, 0);
}
}
IEnumerator jumpTime() {
yield return new WaitForSeconds (0.4f);
jumpTimer = 0;
}
}
I tried to do something with jumCount, jumpTimer and velocity.y, but nothing helped
I would suggested making a grounded and jumping bool, you can use this bool to determine whether your character is airbourne. Also using else if conditional statements may help here to avoid setting your value more than once. Then you can do something like this:
if (grounded && jumping) {
Flip();
anim.SetInteger ("noob", 3);
jumping = false;
}
else if (!grounded) {
Flip();
anim.SetInteger ("noob", 4);
}
else if (Input.GetAxis ("Horizontal") == 0 && grounded) {
anim.SetInteger ("noob", 1);
}
else if (grounded && Input.GetAxis ("Horizontal") != 0) {
Flip();
anim.SetInteger("noob", 2);
}

gameobjects on switch statement(col.tag)

im developing a game right now. It seems more like capture the flag in 2d. But im having problem with my character script. what i want is if my character has the flag and he goes back to her base with a flag(gameobject on) she will go to the winscene. but the thing is i also want her to not proceed to the winscene when the gameobject is not on.
Here's the Script
GameObject[] toEnable, toDisable;
public GameObject CharFlag;
private Rigidbody2D rb;
private Animator anim;
private float moveSpeed;
private float dirX;
private bool facingRight = true;
private Vector3 localScale;
//Use this for Initialization
private void Start(){
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
localScale = transform.localScale;
moveSpeed = 5f;
// Finding game objects with tags "ToEnable" and "ToDisable"
toEnable = GameObject.FindGameObjectsWithTag ("CharFlag");
// Disabling game objects with tag "ToEnable"
foreach (GameObject element in toEnable)
{
element.gameObject.SetActive (false);
}
}
//Update is called once per frame
private void Update()
{
dirX = CrossPlatformInputManager.GetAxisRaw ("Horizontal") * moveSpeed;
if (CrossPlatformInputManager.GetButtonDown ("Jump") && rb.velocity.y == 0)
rb.AddForce(Vector2.up * 700f);
if (Mathf.Abs(dirX) > 0 && rb.velocity.y == 0)
anim.SetBool("isRunning", true);
else
anim.SetBool("isRunning", false);
if (rb.velocity.y == 0)
{
anim.SetBool("isJumping", false);
anim.SetBool("isFalling", false);
}
if (rb.velocity.y > 0)
{
anim.SetBool("isJumping", true);
}
if (rb.velocity.y < 0)
{
anim.SetBool("isJumping", false);
anim.SetBool("isFalling", true);
}
}
private void FixedUpdate()
{
rb.velocity = new Vector2(dirX, rb.velocity.y);
}
private void LateUpdate()
{
if (dirX > 0)
facingRight = true;
else if (dirX < 0)
facingRight = false;
if (((facingRight) && (localScale.x < 0)) || ((!facingRight) && (localScale.x > 0)))
localScale.x *= -1;
transform.localScale = localScale;
}
void OnTriggerEnter2D (Collider2D col)
{
switch (col.tag) {
case "EnemyField":
CharFlag.gameObject.SetActive (true);
break;
case "AlyField":
SceneManager.LoadScene ("YouWin");
break;
}
}
}
This would be better as a comment but I only have 40 reputation.
Simply check if the GameObject is active or not.
switch (col.tag)
{
case "EnemyField":
CharFlag.gameObject.SetActive (true);
break;
case "AllyField":
if(CharFlag.gameObject.activeSelf)
{
SceneManager.LoadScene ("YouWin");
}
break;

Categories