- Home /
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);
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.
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;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612091940im_/https://answers.unity.com/themes/thub/images/avi.jpg)