- Home /
Player doesn`t jump, please help!
Hey Guys, i`ve been working with Unity for a month now and i`ve never seen something like that. It`s my first 2d game now and I want to make my player square jump. Unity doesn`t scream at me with errors, my player just doesn`t jump. If the code is still pretty simple, i`ve just created the project.
public class PlayerController : MonoBehaviour
{
public float speed = 7.0f;
public float jumpForce = 5.0f;
public Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb = GetComponent<Rigidbody2D>();
rb.AddForce(Vector2.up * jumpForce * Time.deltaTime);
}
transform.Translate(Input.GetAxis("Horizontal") * Vector2.right * speed * Time.deltaTime);
}
}
Answer by Satton38 · Nov 25, 2019 at 09:25 AM
I think Time.deltaTime is not need. If you use Time.deltaTime, try multiplyng a large value. Because Time.deltaTime is a very small value. And is need "transform.Translate(Input.GetAxis("Horizontal") Vector2.right speed * Time.deltaTime);"?
Right. Time.deltaTime
is not needed in jump code. But there's no need to multiply Time.deltaTime
with large values either, it's just pointless most of the time, especially in this particular case. Also, @niedery572 , don't ever use GetComponent()
within an Update()
loop. $$anonymous$$ove the rb = GetComponent<Rigidbody>()
line to Start()
.
Your answer
Follow this Question
Related Questions
Character looking at opposite direction of mouse 1 Answer
How can I add parallax to background sprites which are being instantiated only in play mode. 1 Answer
Character controller problem 2 Answers
Problem using DontDestroyOnLoad carrying over a player, but not a camera 2 Answers
how can i make my character run up ramps like in sonic, fancy pants or even line rider? 1 Answer