- Home /
My player does not jump on every click and if it jumps it is really short
Hello all, for some reason when I press spacebar and I try to jump, my player does not always jumps. Also when it does jump it kinda teleports to some point on the screen and then quickly falls down as if something really heavy pulls it down. This is my script, it is really simple: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody2D Player;
public float movementSpeed;
public float jumpHeight;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Player.velocity = new Vector2(movementSpeed * Input.GetAxis("Horizontal"), Player.position.y);
if (Input.GetButtonDown("Jump"))
{
Player.velocity = new Vector2(Player.position.x, jumpHeight);
}
}
}
My components that I have on the player:
I will be gratefull for any help. I really do not know why this is happening. If you want a demo of what is happening, I am willing to provide a video. I looked at other answers here, but they were about using FixedUpdate and I do not use it as you can see.
What's your gravity set to? also, try using transform, ie Player.transform.position.x as it seems you just setting a location for it to appear when the spacebar is pressed.
I needed to use velocity and I just did not saw this mistake in my code. Thank you for the suggestion. :)
Answer by minavazatoka · Apr 10 at 06:07 PM
I am sorry all, the bug in the code was that I was using:
Player.velocity = new Vector2(Player.position.x, jumpHeight);
Instead of this I had to use:
Player.velocity = new Vector2(Player.velocity.x, jumpHeight);
I think as divinereignoflords said I was setting new location for the object to appear to with the position property.
Your answer
Follow this Question
Related Questions
2D C# Jump Script Addforce 2 Answers
Player can jump infinitely when under collision 2 Answers
Rigidbody teleports when called AddForce() function 0 Answers
My script only partially works 2 Answers