Unity2d jump animation not working properly












0















My character is stuck on the "AirLayer" of my animator despite the fact that it is always grounded. My AirLayer has my jump animations(Takeoff and Landing) and my GroundLayer(BaseLayer) has my walking animations. When I play the game, the AirLayer always has its weight set to 1 for some reason.



My code is below.



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewPlayerController : MonoBehaviour
{
private Rigidbody2D myRigidBody;
private Animator anim;
private SpriteRenderer sr;

private bool facingRight;

[SerializeField]
private Transform groundPoints;

[SerializeField]
private float groundRadius;

[SerializeField]
private LayerMask whatIsGround;

[SerializeField]
private float movementSpeed;

private bool isGrounded;

private bool jump;

[SerializeField]
private float jumpForce;

[SerializeField]
private GameObject bullet;

// Start is called before the first frame update
void Start()
{
myRigidBody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
sr = GetComponent<SpriteRenderer>();


}
void Update()
{
HandleInput();
}
// Update is called once per frame
void FixedUpdate()
{
float horizontal = Input.GetAxisRaw("Horizontal");

isGrounded = IsGrounded();

HandleMovement(horizontal);

Flip(horizontal);

HandleLayers();

ResetValues();
}

private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.Space))
{
jump = true;

}
if (Input.GetKeyDown(KeyCode.V))
{
ShootBullet(0);
}

}

private void HandleMovement(float horizontal)
{
if (myRigidBody.velocity.y<0)
{
anim.SetBool("Land", true);
}

myRigidBody.velocity = new Vector2(horizontal * movementSpeed, myRigidBody.velocity.y);
anim.SetFloat("speed", Mathf.Abs(horizontal));

if(isGrounded && jump)
{

isGrounded = false;

myRigidBody.AddForce(new Vector2(0, jumpForce));

anim.SetTrigger("Jump");

}

}
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 bool IsGrounded()
{
if (myRigidBody.velocity.y <= 0)
{
foreach (Transform point in groundPoints)
{
Collider2D colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
anim.ResetTrigger("Jump");
anim.SetBool("Land", false);
return true;

}
}
}
}
return false;

}

private void ResetValues()
{
jump = false;
}
public void ShootBullet(int value)
{
if (facingRight)
{
GameObject tmp = (GameObject)Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,-90)));
tmp.GetComponent<BulletBehaviour>().Initialize(Vector2.right);
}
else
{
GameObject tmp = (GameObject)Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0, 0, 90)));
tmp.GetComponent<BulletBehaviour>().Initialize(Vector2.left);
}
}
private void HandleLayers()
{
if (!isGrounded)
{
anim.SetLayerWeight(1, 1);
}
else
{
anim.SetLayerWeight(1, 0);
}
}
}









share|improve this question













migrated from superuser.com Feb 8 at 5:27


This question came from our site for computer enthusiasts and power users.














  • 1





    You're going to want to ask this on StackOverflow instead.

    – HazardousGlitch
    Feb 5 at 23:46
















0















My character is stuck on the "AirLayer" of my animator despite the fact that it is always grounded. My AirLayer has my jump animations(Takeoff and Landing) and my GroundLayer(BaseLayer) has my walking animations. When I play the game, the AirLayer always has its weight set to 1 for some reason.



My code is below.



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewPlayerController : MonoBehaviour
{
private Rigidbody2D myRigidBody;
private Animator anim;
private SpriteRenderer sr;

private bool facingRight;

[SerializeField]
private Transform groundPoints;

[SerializeField]
private float groundRadius;

[SerializeField]
private LayerMask whatIsGround;

[SerializeField]
private float movementSpeed;

private bool isGrounded;

private bool jump;

[SerializeField]
private float jumpForce;

[SerializeField]
private GameObject bullet;

// Start is called before the first frame update
void Start()
{
myRigidBody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
sr = GetComponent<SpriteRenderer>();


}
void Update()
{
HandleInput();
}
// Update is called once per frame
void FixedUpdate()
{
float horizontal = Input.GetAxisRaw("Horizontal");

isGrounded = IsGrounded();

HandleMovement(horizontal);

Flip(horizontal);

HandleLayers();

ResetValues();
}

private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.Space))
{
jump = true;

}
if (Input.GetKeyDown(KeyCode.V))
{
ShootBullet(0);
}

}

private void HandleMovement(float horizontal)
{
if (myRigidBody.velocity.y<0)
{
anim.SetBool("Land", true);
}

myRigidBody.velocity = new Vector2(horizontal * movementSpeed, myRigidBody.velocity.y);
anim.SetFloat("speed", Mathf.Abs(horizontal));

if(isGrounded && jump)
{

isGrounded = false;

myRigidBody.AddForce(new Vector2(0, jumpForce));

anim.SetTrigger("Jump");

}

}
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 bool IsGrounded()
{
if (myRigidBody.velocity.y <= 0)
{
foreach (Transform point in groundPoints)
{
Collider2D colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
anim.ResetTrigger("Jump");
anim.SetBool("Land", false);
return true;

}
}
}
}
return false;

}

private void ResetValues()
{
jump = false;
}
public void ShootBullet(int value)
{
if (facingRight)
{
GameObject tmp = (GameObject)Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,-90)));
tmp.GetComponent<BulletBehaviour>().Initialize(Vector2.right);
}
else
{
GameObject tmp = (GameObject)Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0, 0, 90)));
tmp.GetComponent<BulletBehaviour>().Initialize(Vector2.left);
}
}
private void HandleLayers()
{
if (!isGrounded)
{
anim.SetLayerWeight(1, 1);
}
else
{
anim.SetLayerWeight(1, 0);
}
}
}









share|improve this question













migrated from superuser.com Feb 8 at 5:27


This question came from our site for computer enthusiasts and power users.














  • 1





    You're going to want to ask this on StackOverflow instead.

    – HazardousGlitch
    Feb 5 at 23:46














0












0








0








My character is stuck on the "AirLayer" of my animator despite the fact that it is always grounded. My AirLayer has my jump animations(Takeoff and Landing) and my GroundLayer(BaseLayer) has my walking animations. When I play the game, the AirLayer always has its weight set to 1 for some reason.



My code is below.



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewPlayerController : MonoBehaviour
{
private Rigidbody2D myRigidBody;
private Animator anim;
private SpriteRenderer sr;

private bool facingRight;

[SerializeField]
private Transform groundPoints;

[SerializeField]
private float groundRadius;

[SerializeField]
private LayerMask whatIsGround;

[SerializeField]
private float movementSpeed;

private bool isGrounded;

private bool jump;

[SerializeField]
private float jumpForce;

[SerializeField]
private GameObject bullet;

// Start is called before the first frame update
void Start()
{
myRigidBody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
sr = GetComponent<SpriteRenderer>();


}
void Update()
{
HandleInput();
}
// Update is called once per frame
void FixedUpdate()
{
float horizontal = Input.GetAxisRaw("Horizontal");

isGrounded = IsGrounded();

HandleMovement(horizontal);

Flip(horizontal);

HandleLayers();

ResetValues();
}

private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.Space))
{
jump = true;

}
if (Input.GetKeyDown(KeyCode.V))
{
ShootBullet(0);
}

}

private void HandleMovement(float horizontal)
{
if (myRigidBody.velocity.y<0)
{
anim.SetBool("Land", true);
}

myRigidBody.velocity = new Vector2(horizontal * movementSpeed, myRigidBody.velocity.y);
anim.SetFloat("speed", Mathf.Abs(horizontal));

if(isGrounded && jump)
{

isGrounded = false;

myRigidBody.AddForce(new Vector2(0, jumpForce));

anim.SetTrigger("Jump");

}

}
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 bool IsGrounded()
{
if (myRigidBody.velocity.y <= 0)
{
foreach (Transform point in groundPoints)
{
Collider2D colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
anim.ResetTrigger("Jump");
anim.SetBool("Land", false);
return true;

}
}
}
}
return false;

}

private void ResetValues()
{
jump = false;
}
public void ShootBullet(int value)
{
if (facingRight)
{
GameObject tmp = (GameObject)Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,-90)));
tmp.GetComponent<BulletBehaviour>().Initialize(Vector2.right);
}
else
{
GameObject tmp = (GameObject)Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0, 0, 90)));
tmp.GetComponent<BulletBehaviour>().Initialize(Vector2.left);
}
}
private void HandleLayers()
{
if (!isGrounded)
{
anim.SetLayerWeight(1, 1);
}
else
{
anim.SetLayerWeight(1, 0);
}
}
}









share|improve this question














My character is stuck on the "AirLayer" of my animator despite the fact that it is always grounded. My AirLayer has my jump animations(Takeoff and Landing) and my GroundLayer(BaseLayer) has my walking animations. When I play the game, the AirLayer always has its weight set to 1 for some reason.



My code is below.



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewPlayerController : MonoBehaviour
{
private Rigidbody2D myRigidBody;
private Animator anim;
private SpriteRenderer sr;

private bool facingRight;

[SerializeField]
private Transform groundPoints;

[SerializeField]
private float groundRadius;

[SerializeField]
private LayerMask whatIsGround;

[SerializeField]
private float movementSpeed;

private bool isGrounded;

private bool jump;

[SerializeField]
private float jumpForce;

[SerializeField]
private GameObject bullet;

// Start is called before the first frame update
void Start()
{
myRigidBody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
sr = GetComponent<SpriteRenderer>();


}
void Update()
{
HandleInput();
}
// Update is called once per frame
void FixedUpdate()
{
float horizontal = Input.GetAxisRaw("Horizontal");

isGrounded = IsGrounded();

HandleMovement(horizontal);

Flip(horizontal);

HandleLayers();

ResetValues();
}

private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.Space))
{
jump = true;

}
if (Input.GetKeyDown(KeyCode.V))
{
ShootBullet(0);
}

}

private void HandleMovement(float horizontal)
{
if (myRigidBody.velocity.y<0)
{
anim.SetBool("Land", true);
}

myRigidBody.velocity = new Vector2(horizontal * movementSpeed, myRigidBody.velocity.y);
anim.SetFloat("speed", Mathf.Abs(horizontal));

if(isGrounded && jump)
{

isGrounded = false;

myRigidBody.AddForce(new Vector2(0, jumpForce));

anim.SetTrigger("Jump");

}

}
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 bool IsGrounded()
{
if (myRigidBody.velocity.y <= 0)
{
foreach (Transform point in groundPoints)
{
Collider2D colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
anim.ResetTrigger("Jump");
anim.SetBool("Land", false);
return true;

}
}
}
}
return false;

}

private void ResetValues()
{
jump = false;
}
public void ShootBullet(int value)
{
if (facingRight)
{
GameObject tmp = (GameObject)Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,-90)));
tmp.GetComponent<BulletBehaviour>().Initialize(Vector2.right);
}
else
{
GameObject tmp = (GameObject)Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0, 0, 90)));
tmp.GetComponent<BulletBehaviour>().Initialize(Vector2.left);
}
}
private void HandleLayers()
{
if (!isGrounded)
{
anim.SetLayerWeight(1, 1);
}
else
{
anim.SetLayerWeight(1, 0);
}
}
}






c# ubuntu-unity






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 5 at 23:39







user994602











migrated from superuser.com Feb 8 at 5:27


This question came from our site for computer enthusiasts and power users.









migrated from superuser.com Feb 8 at 5:27


This question came from our site for computer enthusiasts and power users.










  • 1





    You're going to want to ask this on StackOverflow instead.

    – HazardousGlitch
    Feb 5 at 23:46














  • 1





    You're going to want to ask this on StackOverflow instead.

    – HazardousGlitch
    Feb 5 at 23:46








1




1





You're going to want to ask this on StackOverflow instead.

– HazardousGlitch
Feb 5 at 23:46





You're going to want to ask this on StackOverflow instead.

– HazardousGlitch
Feb 5 at 23:46












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54586473%2funity2d-jump-animation-not-working-properly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown
























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54586473%2funity2d-jump-animation-not-working-properly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Aardman Animations

Are they similar matrix

“minimization” problem in Euclidean space related to orthonormal basis