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 /
avatar image
0
Question by BiletM · Sep 01, 2016 at 05:33 AM · 2d2d-platformerjumping

How to reduce moveability for player when releasing button mid-air

Hi! I'm completely new to Unity and programming, and trying to learn, so bear with me. I have a 2D-platformer, made via the help of a tutorial.

Now I'm trying to make it so that when the player jumps, he does not have full moveability in the air to move around as much as when on the ground. My thought was to do this by reducing the speed of the player when airborne, when releasing the button used for movement. So that in order to jump full length, you have to hold the walking-key while jumping, and if you release it mid-air, you go a shorter distance. I have been trying different things like cutting moveSpeed in half, but I'm not sure how to make it do that in both directions.

Here is my PlayerController-script:

using UnityEngine; using System.Collections;

public class PlayerController : MonoBehaviour {

 public float moveSpeed;
 private Rigidbody2D myRigidbody;

 public float jumpSpeed;

 public Transform groundCheck;
 public float groundCheckRadius;
 public LayerMask whatIsGround;

 public bool isGrounded;

 private Animator myAnim;

 // Use this for initialization
 void Start () {
     myRigidbody = GetComponent<Rigidbody2D>();
     myAnim = GetComponent<Animator>();

 }

 // Update is called once per frame
 void Update () {


     isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);

         //move right
         if (Input.GetAxisRaw("Horizontal") > 0f)
         {
             myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y);
             transform.localScale = new Vector2(1f, 1f); //facing right

         }
         //move left
         else if (Input.GetAxisRaw("Horizontal") < 0f)
         {
             myRigidbody.velocity = new Vector2(-moveSpeed, myRigidbody.velocity.y);
             transform.localScale = new Vector2(-1f, 1f); //facing left
         }

         //stop moving(only when grounded)
         else if (isGrounded)
         { 
             myRigidbody.velocity = new Vector2(0f, myRigidbody.velocity.y);
         }
         //jump and check if grounded(prevent doublejump)
         if (Input.GetButtonDown("Jump") && isGrounded)
         {
             myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpSpeed);
         }
     

     }

   // For animator: Checks whether speed is >0.1 and converts -5 to 5 and if Player is on the ground

     myAnim.SetFloat("Speed", Mathf.Abs(myRigidbody.velocity.x));
     myAnim.SetBool("Grounded", isGrounded);


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
1
Best Answer

Answer by sparkzbarca · Sep 01, 2016 at 05:42 AM

well mostly you shouldn't be modifying velocity directly. You should be using addforce.

Velocity is your speed in all three directions yes but by modifying it your basically breaking physics, just like using addforce to move an object is much different than transform.position. using addforce is much different than modifying the velocity.

If you modify velocity directly you are basically making something go from speed X to speed Y without any intervening speed in between.

However if you want to do that use a modifier and multiply the velocity by that.

the easiest is.

if (!grounded) myRigidbody.velocity *= .5;

now if your x value is negative, it's still negative, if positive, it's still positive, whatever direction they were going they stil are, just half as fast.

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 BiletM · Sep 01, 2016 at 09:00 AM

Thank you @sparkzbarca! It worked! And thank you for the tip. I agree, using addForce is probably a better way for 2D-movement physics-wise, but for this simple tutorial-game it works OK, and gives more "snappy" controls than I was able to achieve with addForce.

         //if "Horizontal" input released when not grounded, reduce speed 
         else if (Input.GetButtonUp("Horizontal") && !isGrounded)
         {
             myRigidbody.velocity *= .5f;
         }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Im trying to make it so it limits jumps in my 2d game. But when I play it, It only jumps once then your not able to jump again. Anyhelp? I need a answer fast because this is for the blackthornprod game jam 2 Answers

2D Platformer Jumping at a 45 degree angle 2 Answers

Problem with Jump 1 Answer

2D Platform Player moving instantly from upper platform to lower, only when moving left 0 Answers

Scale object, collision not working 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