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 /
This question was closed Sep 02, 2020 at 01:08 AM by GinjaNinja510 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by GinjaNinja510 · Aug 31, 2020 at 10:37 PM · movement scriptjumping

i am trying to make some movement for my character but i am having some issues when it comes to jumping

i am trying to make some movement for my character but i am having some issues when it comes to jumping. I originally had all of my movement and jumping in one file like this:

 public Rigidbody2D rb;
 
 public float moveSpeed = 5f;
 public float jumpHeight = 30f;
 
 private bool isJumping;
 public bool onGround;
 private bool movingRight;
 private bool movingLeft;
 
   
     void FixedUpdate()
 {
     //Activating Movement
     if (Input.GetKey(KeyCode.D))
     {
         movingRight = true;
     }
     else
     {
         movingRight = false;
     }
     
     if (Input.GetKey(KeyCode.A))
     {
         movingLeft = true;
     }
     else
     {
         movingLeft = false;
     }
     
     
     //Right Movement
     if (movingRight == true)
     {
         rb.velocity = new Vector2(moveSpeed, 0.0f);
     }
     else if (movingLeft == false)
     {
         rb.velocity = new Vector2(0.0f, 0.0f);
     }
     //Left Movement
     if (movingLeft == true)
     {
         rb.velocity = new Vector2(-moveSpeed, 0.0f);
     } 
     else if (movingRight == false)
     {
         rb.velocity = new Vector2(0.0f, 0.0f);
     }
     //Stop if Pressing Left and Right At Same Time
     if (movingRight == true && movingLeft == true)
     {
         rb.velocity = new Vector2(0.0f, 0.0f);
     }
     //Jumping
     if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.Space))
     {
         isJumping = true;
     }
     else
     {
         isJumping = false;
     }
     if (isJumping == true && onGround == true)
     {
         rb.velocity = Vector2.up * jumpHeight;
         Debug.Log("JUMP!!");
     }
 }

 //Checking if Player is On Ground
 void OnCollisionEnter2D(Collision2D col)
  {
     if (col.gameObject.tag == "Ground")
     {
         onGround = true;
     }
 }
 void OnCollisionExit2D(Collision2D collision)
 {               
         onGround = false;
 }
 }

it works exactly how i want it to except for when i jump it seems like it just abruptly goes to the peak of the jump and then starts going down normal.i kind of fixed this problem by separating the Movement and jumping into 2 different scripts and the jumping works as i want it to, but only when i disable the movement script. Here's my movement script

 public Rigidbody2D rb;
 public float moveSpeed = 5f;

 private bool movingRight;
 private bool movingLeft;
 
   
     void FixedUpdate()
 {
     //Activating Movement
     if (Input.GetKey(KeyCode.D))
     {
         movingRight = true;
     }
     else
     {
         movingRight = false;
     }
     
     if (Input.GetKey(KeyCode.A))
     {
         movingLeft = true;
     }
     else
     {
         movingLeft = false;
     }
     
     
     //Right Movement
     if (movingRight == true)
     {
         rb.velocity = new Vector2(moveSpeed, 0.0f);
     }
     else if (movingLeft == false)
     {
         rb.velocity = new Vector2(0.0f, 0.0f);
     }
     //Left Movement
     if (movingLeft == true)
     {
         rb.velocity = new Vector2(-moveSpeed, 0.0f);
     } 
     else if (movingRight == false)
     {
         rb.velocity = new Vector2(0.0f, 0.0f);
     }
     //Stop if Pressing Left and Right At Same Time
     if (movingRight == true && movingLeft == true)
     {
         rb.velocity = new Vector2(0.0f, 0.0f);
     }
 }

and my jumping

 public Rigidbody2D rb;
 public float jumpHeight = 30f;
 private bool isJumping;
 public bool onGround;
 
 
 //Checking if Player is On Ground
 void OnCollisionEnter2D(Collision2D col)
  {
     if (col.gameObject.tag == "Ground")
     {
         onGround = true;
     }
 }
 void OnCollisionExit2D(Collision2D collision)
 {               
         onGround = false;
 }


 void Update()
 {
     if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.Space))
     {
         isJumping = true;
     }
     else
     {
         isJumping = false;
     }
     
     //Jump Movement
     if (isJumping == true && onGround == true)
     {
         rb.velocity = Vector2.up * jumpHeight;
         Debug.Log("JUMP!!");
     }
 }

I don't what i'm doing wrong

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

1 Reply

  • Sort: 
avatar image
0

Answer by Ymrasu · Sep 01, 2020 at 08:58 AM

You are assigning both x and y each time to rb.velocity which cancels out the previously assigned velocity. You'd want to separate the x (left and right) and y (up and down). Assign x and y separately while taking the other into account, like this:

 // example movespeed
 rb.velocity = new Vector2(movespeed, rb.velocity.y);
 
 // example jumpHeight
 rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
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 GinjaNinja510 · Sep 02, 2020 at 01:08 AM 0
Share

Thanks that worked. i see what the problem was now and i wont make that mistake again.

Follow this Question

Answers Answers and Comments

140 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 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 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 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 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 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 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 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

My player is lagging/stuttering when trying to move mid-air(after jumping) 1 Answer

My movement script is kind of broken for jumping (upward force is diminished after first jump) 1 Answer

Making jumping independent of normalized movement? 1 Answer

Character floats upward when looking up and down 0 Answers

isGrounded is always false, even with gravity, how do you fix that? 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