Project 2D. The character moves up and down. Gravity is at zero, as the movement goes up (a platformer with a view of the top of the head). The character moves indefinitely without stopping, stops when colliding with another object. How to make so that if the button is not pressed, the character stops?

using System.Collections.Generic; using UnityEngine; public class player_Control : MonoBehaviour { public Rigidbody2D RigBud; public float movespeed; public float jumpheight; public bool jump; public LayerMask WoIG; void Start() { RigBud = GetComponent<Rigidbody2D>(); } void FixedUpdate() { } void Update() { if (Input.GetKey(KeyCode.UpArrow)) { RigBud.velocity = new Vector2(RigBud.velocity.x, jumpheight); Debug.Log("Up"); } if (Input.GetKey(KeyCode.DownArrow)) { RigBud.velocity = new Vector2(RigBud.velocity.x, -jumpheight); Debug.Log("Down"); } } } 

    1 answer 1

    It is necessary to set the speed to zero, if no other forces act on the object.

     void Update() { if (Input.GetKey(KeyCode.UpArrow)) { RigBud.velocity = new Vector2(RigBud.velocity.x, jumpheight); Debug.Log("Up"); } else if (Input.GetKey(KeyCode.DownArrow)) { RigBud.velocity = new Vector2(RigBud.velocity.x, -jumpheight); Debug.Log("Down"); } else { RigBud.velocity = new Vector2(0, 0); } }