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);
}
}
}
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);
}
}
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.
@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?