Can someone help me make a simple jump script?
Hello im just making a game that is VERY simple and im really really new to this. Im coding in c# and i want to make a jump script where the cube just stands still and jumps. I dont know what to write in the code. I tried to do somethings but it didnt work as intended. I guess you could use this to help me and maybe ill learn. I also havent set up to check if its touching the ground, i was just trying to find a way to make it jump first then ill do that.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
//Spawn Variables
public float playerSetX;
public float playerSetY;
public float playerSetZ;
//Movement Variables
public float playerJumpHeight = 2;
void Start () {
//Player Spawn Point
//This is where our player will start when the game is played.
//player == game object. Game object == transform!
transform.position = new Vector3(playerSetX, playerSetY, playerSetZ);
}
void Update () {
//player to move up jumping
//player (gameobject) aka transform to move when i press the arrow keys or keyboard keys
//jump
if (Input.GetKeyDown (KeyCode.A) || Input.GetKeyDown (KeyCode.D) || Input.GetKeyDown (KeyCode.S) || Input.GetKeyDown (KeyCode.W) || Input.GetKeyDown (KeyCode.Space) || Input.GetKeyDown (KeyCode.UpArrow)) {
transform.position += new Vector3(playerSetX, playerJumpHeight, playerSetZ);
}
}
}
Answer by Positive7 · Aug 02, 2015 at 07:26 PM
You might want to check some tutorial at http://unity3d.com/learn
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
public class PlayerController : MonoBehaviour {
public Vector3 jump;
public float jumpForce = 2.0f;
public bool isGrounded;
Rigidbody rb;
void Start(){
rb = GetComponent<Rigidbody>();
jump = new Vector3(0.0f, 2.0f, 0.0f);
}
void OnCollisionStay()
{
isGrounded = true;
}
void Update(){
if(Input.GetKeyDown(KeyCode.Space) && isGrounded){
rb.AddForce(jump * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
}
Do you $$anonymous$$d explaining it please? I did ask just for a jump script but i thought i could just try to read and learn it. I also did alot of unity's learn pages but still dont understand how to make a jump script. Sorry if im annoying you... And btw that jump script did work.
@pnut03 I will try although I did not write this answer.
[RequireComponent(typeof(Rigidbody))]
> requires the rigidbody component on your GameObject
public Vector3 jump;
Vector3 is a variable storing three axis values. In this case it basically contains the information in which direction will the object move.
public bool isGrounded;
a boolean deter$$anonymous$$ing whether the GameObj is touching (colliding) with the ground. The bool itself does nothing, we have to set it true while the player collides with the ground, by doing
void OnCollisionStay()
{
isGrounded = true;
}
Phew. Then, there's Rigidbody rb;
. See what rigidbody is.
in Start()
, we assign the variable rb to the component attached to your GameObj and also we assign values to the Vector3 jump
.
Finally, in Update()
, this code:
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && isGrounded)
{
rb.AddForce(jump * jumpForce, Force$$anonymous$$ode.Impulse);
isGrounded = false;
}
means that if the player hits the Space button and at the same time, the GameObj is grounded, it will add a physic force to the rigidbody, using
AddForce(Vector3 force, Force$$anonymous$$ode mode)
where force is the Vector3 storing the movement info and mode is how the force will be applied (mode can be Force$$anonymous$$ode.Force, Force$$anonymous$$ode.Acceleration, Force$$anonymous$$ode.Impulse or Force$$anonymous$$ode.VelocityChange, see Force$$anonymous$$ode for more).
If you still don't understand something, feel free to let me know!
@pnut03 I suggest going through both feature-specific (as in Scripting for Beginners or Physics for Beginners etc...) AND full-project video tutorials (as in Roll-A-Ball project and then Survival Shooter Tutorial) found here and here. Also Live Training Archive is a good place to head to next since it covers best practices.
Also, @imilanspinka was being nice here to explain the code but mostly you have to solve the problems you're going through. You can still consult the community here after trial and error on specific issues you are having; that doesn't mean anybody would hesitate to help if they can!
Good luck and have fun :D cheers :)
Hi man, I tried the script and it works good. However, there are several issues that should be fixed:ç
When the player is jumping, if you hit the space again, it makes another small jump. This only happen once per jump, so you can't "fly", but it's annoying. Any way to solve this?
The length of the jump when the player is running is longer than it should be. How can I fix that too? Thank you!!
Hope i'm not too late, but first of all thank you, second of all, I found a way to prevent your double jumping.
private void OnCollisionStay() { if(!isGrounded && rb.velocity.y == 0) { isGrounded = true; //jumpsLeft = 1; } }
it helps to double check your Y-velocity when you collide, otherwise you reset your "isGrounded" before your object even left the floor. Hope this helps
Unfortunately, this causes issues for me when I am walking up a slope
I wrote all of the same code.What about the RequirementComponent part? $$anonymous$$icrosoft Visual Studio (where I typed in the code) doesn't explain what the problem is. Same as Vector3, is there a way to fix all this?
Answer by drearyplane · May 11, 2019 at 09:51 AM
I know I'm three years late but here's my two cents - the doyble jump is annoying but there's a simple fix:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public Vector3 jump;
public float jumpForce = 2.0f;
public bool isGrounded;
Rigidbody rb;
void Start(){
rb = GetComponent<Rigidbody>();
jump = new Vector3(0.0f, 2.0f, 0.0f);
}
void OnCollisionStay()
{
isGrounded = true;
}
void OnCollisionExit(){
isGrounded = false;
}
void Update(){
if(Input.GetKeyDown(KeyCode.Space) && isGrounded){
rb.AddForce(jump * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
}
This means that, when you leave the ground, isGrounded is set to false. I would also suggest a layers system, otherwise you can wall-jump.
I'm currently facing this issue with the wall jumping. I looked at the layers documentation but couldn't figure out how it could help me stop the character wall jumping, do you $$anonymous$$d explaining a little bit more?
Answer by Lormax23 · Aug 28, 2020 at 09:29 PM
@Arcane-Potato , I used your script to great success, I was able to get my player to move AND jump. It doesn't double-jump. The height of the jump can be very inconsistent though. I'm extremely new, so I was trying to figure out why. Since 'jump' and 'jumpForce' were set values, I thought it might be the Time.deltaTime, but when I removed that the player would jump sky high and never come back down. Any thoughts as to why the height of the jump is inconsistent?
Answer by adityapatnaik_si · May 24, 2019 at 09:29 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Youjump : MonoBehaviour
{
public float speed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// Jump is an axis predefined in the Unity input manager
// go to edit->Project Settings->Input and Check
transform.Translate(0,speed*Input.GetAxis("Jump")*Time.deltaTime,0);
}
}
Answer by Arcane-Potato · Aug 23, 2020 at 05:55 AM
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public Vector3 jump;
public float jumpForce = 2.0f;
public bool isGrounded;
Rigidbody rb;
void Start(){
rb = GetComponent<Rigidbody>();
jump = new Vector3(0.0f, 2.0f, 0.0f);
}
void OnCollisionStay()
{
isGrounded = true;
}
void OnCollisionExit(){
isGrounded = false;
}
void FixedUpdate(){
if(Input.GetKey("space") && isGrounded){
rb.AddForce(jump * jumpForce *Time.deltaTime, ForceMode.Impulse);
isGrounded = false;
}
}
}
I know this post was made forever ago but just in case anyone else is looking at this in the future I found the above code to be extremely inconsistent on when it would detect my jump and also how high I would end up jumping so this is what I changed it too and it seems to be working for me so far. Just so you know you will have to increase the jump force a lot compared to what you had it set at.
I added a RigidBody, and while I can jump, my cinemachine camera does funky things and bounce everywhere until I glitch into the void :/
This script works. I get the Jump and Jump force options. But I cant jump for some reason? Space isn't working. Can you explain whats wrong please?
Your answer
Follow this Question
Related Questions
Picking up collectables in a correct (custom set) order to open gates? 0 Answers
Platformer Jump Script Without AddForce 0 Answers
Simple AI , glowing effect ?? 0 Answers
I have got bugs when i jump 0 Answers
Can you use a cube to bring up a UI? 1 Answer