Jumping in Unity C#
So, I want my player (currently just a box), to jump. Here's the code I tried:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class jump : MonoBehaviour {
public float jumpSpd = 5f;
public Rigidbody rb;
private float isPrintDone = 0;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
void Jump()
{
rb.AddForce(0, 8, 0);
}
void Update ()
{
if (Input.GetKeyDown (KeyCode.F))
{
Jump();
if(isPrintDone > 0)
{
isPrintDone += 1;
print("Jump Called");
}
}
}
}
and in the console I don't get "Jump Called", and my player's acceleration doesn't change at all. Help? (Also I'm really bad at asking questions, please ask for specification if needed.) I've determined that it's a problem with calling if I press the letter F, not the jump code itself.
rigidbody.AddForce(Vector3.up * jumpSpd, ForceMode.Force);
Doesn't work either.
print is done = 0
If(0 > 0) then debug this is what you are asking , ;)
Answer by Dennisdb1997 · Jun 04, 2017 at 06:00 PM
rigidbody2D.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Force); Try this
Answer by TheGesor · Jun 04, 2017 at 06:02 PM
@Dennisdb1997 when changing vector2 to vector3, and remove all 2d's (It's a 3d game), it doesn't work. I think it doesn't recognize me pressing F
The gravity in Unity is really bad. In Unity gravity is based on your scale how big things are. I always write my own gravity which isn't that hard. Its better to have more control over the gravity it self.
Try this tutorial,
Your answer
![](https://koobas.hobune.stream/wayback/20220612123505im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Jump Input Problem 0 Answers
Working on dual controls using character controller 0 Answers
making snake game 0 Answers
Player does not jump? 0 Answers
i need help making a dash ability for my endless runner game 2 Answers