- Home /
Question by
DH_kumar · May 20 at 12:02 PM ·
rigidbody.velocity
how to side jump just by pressing space key
in my game i want that when i press space my player jumps forward . but it is laging and only jumping upwards instead of upward+forward.
public class playermovement : MonoBehaviour { private Rigidbody2D rb; [SerializeField] float speed = 7f; private bool running = false;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update() {
float dirx = Input.GetAxisRaw("Horizontal");
if (Input.GetKeyDown(KeyCode.LeftShift))
{
speed = speed * 3f;
running = true;
}
else if(Input.GetKeyUp(KeyCode.LeftShift))
{
speed = speed / 3;
running = false;
}
rb.velocity = new Vector2(dirx, rb.velocity.y);
if (Input.GetButtonDown("Jump"))
{
rb.velocity = new Vector2(7, 5);
}
//Debug.Log(dirx);
}
}
Comment
Your answer
Follow this Question
Related Questions
rigidbody.velocity.x also affecting z-axis? 1 Answer
Velocity on a rigidbody won't stop? 2 Answers
Rigidbody.velocity Not working on parent rigidbody with four child rigidbodies attached to it 1 Answer
RigidBody2D.velocity inconsistant 1 Answer
How to use crossplatforminput buttons as getaxisraw("Horizontal") 1 Answer