Unity2d jump animation not working properly
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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
migrated from superuser.com Feb 8 at 5:27
This question came from our site for computer enthusiasts and power users.
add a comment |
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
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
add a comment |
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
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
c# ubuntu-unity
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
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
You're going to want to ask this on StackOverflow instead.
– HazardousGlitch
Feb 5 at 23:46