Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by animepauly · Nov 23, 2013 at 09:10 PM · 2d-platformerdemo

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;
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image animepauly · Feb 19, 2014 at 03:44 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges