Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by jakubshark · Jan 16, 2017 at 07:42 AM · 2d-platformerplatformer

Cant move and jump at the same time. Only every couple frames you can jump.

For some reason my player cannot jump up and move left and right at the same time. Its wierd. Here is the controller script. This is a 2D Platformer.

public class PlayerController : MonoBehaviour { // Movement Settings [Header("Movement Settings:")] [Space] [Range(1, 10)] public float movementSpeed; [Range(1, 10)] public float jumpHeight;

 private bool canJump;

 // Components
 private Rigidbody2D rigid;

 void Start()
 {
     // Set Components to their Corresponding Variables
     rigid = GetComponent<Rigidbody2D>();
 }

 void FixedUpdate()
 {
     // Jumping
     if(canJump && Input.GetKeyDown(KeyCode.W))
     {
         rigid.AddForce(Vector2.up * jumpHeight, ForceMode2D.Impulse);
     }
 }

 void Update()
 {
     // Movement Left or Right
     Vector3 moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0, 0);
     transform.Translate(moveDirection * movementSpeed * Time.deltaTime);

     // Check if Player is Grounded
     Vector2 groundCheckStart = new Vector2(transform.position.x, transform.position.y - 0.55f);
     Vector2 groundCheckEnd = new Vector2(transform.position.x, transform.position.y - 0.6f);

     Debug.DrawLine(groundCheckStart, groundCheckEnd, Color.red);

     RaycastHit2D groundCheck = Physics2D.Linecast(groundCheckStart, groundCheckEnd);

     if (groundCheck.collider != null)
     {
         canJump = true;
     }
     else
     {
         canJump = 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 Centarius · Jan 16, 2017 at 08:24 AM

Hi @jakubshark, I think the problem is that you're getting Input from two different Update functions. Try moving everything to the FixedUpdate function.

Good Luck and Happy Coding!

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 ScaniX · Jan 16, 2017 at 01:36 PM

Input should be retrieved in the Update() as it is cleared per frame, which is the frequency of the Update() method.

You should not get Input() in the FixedUpdate() as that one is usually only called every few frames. As you are triggering physics, you can store the fact that the player pressed jump and add the force in the next FixedUpdate.

I doubt that this single AddForce for jumping would behave badly when done in the Update(), so you could try that as well.

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

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

6 People are following this question.

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

Related Questions

Player passenger moving when being pushed by two platforms 0 Answers

Character flickering on moving platform 3 Answers

why when the player position changed, the player get stuck in that positon,Why when i change the Player position to an object, the player get stuck 0 Answers

Having a couple of problems regarding 2D Movement. 1 Answer

Platformer 2D 1 Answer


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