- Home /
Unity 4.3 2D Collision problem with the demo
Hi all. I am very new to Unity. I am a flash developer and have moved over to Unity because Flash is kind of dead and Unity's 2D solution looks amazing. That being said, I downloaded unity 4.3 and the demo from the asset store as a base for my platformer game. The main difference I want to do right now is change how the character animates. I want all sprite sheet/flip book style animation for my characters to really push the animation further than you can with just tweening body parts and/or a joint based system.
This gets me to my problem, I want a jump animation from a sprite sheet and a land animation from a sprite sheet. I call that land trigger when the character is "grounded" using the demo's code as a base. THe only thing I changed was adding the Debug.log for grounded. When I run the demo, grounded is set to true even when I am in the air. It seems that the box collider is colliding with the character.
Is this how it's supposed to be and I am missing something, or is this a bug with the new framework? Either way how do I fix it so grounded is false when I jump? Do I use another way to set grounded?
void Update()
{
// The player is grounded if a linecast to the groundcheck position hits anything on the ground layer.
grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
// If the jump button is pressed and the player is grounded then the player should jump.
if(Input.GetButtonDown("Jump") && grounded)
jump = true;
}
void FixedUpdate ()
{
// Cache the horizontal input.
float h = Input.GetAxis("Horizontal");
// The Speed animator parameter is set to the absolute value of the horizontal input.
anim.SetFloat("Speed", Mathf.Abs(h));
// If the player is changing direction (h has a different sign to velocity.x) or hasn't reached maxSpeed yet...
if(h * rigidbody2D.velocity.x < maxSpeed)
// ... add a force to the player.
rigidbody2D.AddForce(Vector2.right * h * moveForce);
// If the player's horizontal velocity is greater than the maxSpeed...
if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
// ... set the player's velocity to the maxSpeed in the x axis.
rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
// If the input is moving the player right and the player is facing left...
if(h > 0 && !facingRight)
// ... flip the player.
Flip();
// Otherwise if the input is moving the player left and the player is facing right...
else if(h < 0 && facingRight)
// ... flip the player.
Flip();
// If the player should jump...
if(jump)
{
// Set the Jump animator trigger parameter.
anim.SetTrigger("Jump");
Debug.Log ("grounded = "+grounded);
// Play a random jump audio clip.
int i = Random.Range(0, jumpClips.Length);
AudioSource.PlayClipAtPoint(jumpClips[i], transform.position);
// Add a vertical force to the player.
rigidbody2D.AddForce(new Vector2(0f, jumpForce));
// Make sure the player can't jump again until the jump conditions from Update are satisfied.
jump = false;
}
}
Answer by chatbear69 · Dec 15, 2013 at 02:48 PM
Try commenting out line 43 and see if that makes a difference. If it does try removing the "grounded = "+ portion from the line. If that works then change "grounded to "Grounded. It looks like Unity is seeing "grounded as if it were a keyword by changing it to green not unlike Jump in the prior line.
Answer by alphacat2 · Feb 19, 2014 at 01:38 AM
I am trying to do the exact same thing (i.e. use a sprite sheet for jump and fall animations). The problem is that the colliders on the characters feet are fixed near the bottom of the sprite. This works in the sample code since the character's feet are always in the same approximate position in the jump animation (whereas in my sprite sheet the character actually rises and falls within the animation frames). Each subsequent frame in the sprite animation has the character moving several pixels up, independent of the colliders which are fixed to the bottom of the game object. It's the colliders that are always grounded. I haven't figured out a fix for this yet; but if it is possible to re-position colliders for each individual frame in the sprite animation, that would most likely solve your problem (and mine). Hopefully someone with more experience can help with a solution.
Yeah I haven't been back to this since I have been working on a procedural level generator. But this is still a problem for sure. Hopefully someone will have a fix for it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613122607im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Platformer Character 1 Answer
How can I make my "Squat" button register with just a press? 1 Answer
I don't know how to ground check the player 3 Answers
Button doesn't react properly 0 Answers
2d-platformer climb 2 Answers