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*, 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;* -
}*
}
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.
– animepauly