- Home /
Hey Guys I have a problem with a single jump script.
I'm working on a adventure rpg and trying to set up a simple single jump action. For some reason each time I test there is a double jump or even a way to cheese the timing to mini jump forever. Can someone look over my noob code and let me know if I goofed somewhere?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float speed;
public float jumpForce;
public float turningSpeed;
private bool canJump;
void Start()
{
}
void Update()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.down, out hit, 1.01f))
{
canJump = true;
}
ProcessInput();
}
void ProcessInput ()
{
if (Input.GetKey("right") || Input.GetKey("d"))
{
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey("left") || Input.GetKey("a"))
{
transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetKey("up") || Input.GetKey("w"))
{
transform.position += Vector3.forward * speed * Time.deltaTime;
}
if (Input.GetKey("down") || Input.GetKey("s"))
{
transform.position += Vector3.back * speed * Time.deltaTime;
}
if ( Input.GetKeyDown("space") && canJump)
{
GetComponent<Rigidbody>().AddForce(0, jumpForce, 0);
canJump = false;
}
}
}
Answer by Ege10 · Apr 08, 2020 at 05:04 PM
Try this: Add a empty GameObject to character(It has to be at the ground level). Then add this code to your code. After that attach the GameObject to controller.Then add layer to your ground object.And set the ground_layer varilable to ground. And bada bing bada bam. It has to be work .
public Transform controller;
public bool canJump;
public LayerMask ground_layer;
const float grounded_time= 0.2f;
void Update()
{
canJump=Physics.OverlapSphere(controller.position, ground_time, ground_layer);
if ( Input.GetKeyDown("space") && canJump)
{
GetComponent<Rigidbody>().AddForce(0, jumpForce, 0);
}
}
Thanks for responding! Problem is I already have my player inside an empty though I have it slightly off the ground as I wanted to keep checking for gravity as well. How necessary is the layer$$anonymous$$ask? is far as I understand I would only need to check the collisions between the player and the ground itself?
oh sorry I was wondering how needed is the layer$$anonymous$$ask since it's the first time I've heard of it? At this phase of the game I'm trying to check against the ability to jump and along with raycasting, Lay$$anonymous$$ask is a whole new term to me.
You can use layermask when you want to assign a object type. For example if you want to jump only on the ground you can assign the ground as ground layer. For another example if you want to do something only in the water. You can assign the water as water layer.
Sry about that I was trying to ask if Layer$$anonymous$$ask was really needed since it was my understanding that using the raycast system checks against any collisions while Layer$$anonymous$$ask would be asigned only to objects that would need to be checked.
Layer$$anonymous$$ask just defines what layers you want to check. Raycast by default checks against all collisions, but you can give the raycast a Layer$$anonymous$$ask to tell it to only look for collisions on certain layers. We are suggesting using a layer mask because in most games there are objects that have colliders that you don't want the character to be able to stand on or jump off of, so you can put them in layers that you havn't selected in your Layer$$anonymous$$ask variable. You don't have to use a Layer$$anonymous$$ask, we are just trying to anticipate your future needs
Answer by unity_ek98vnTRplGj8Q · Apr 08, 2020 at 04:08 PM
I'm guessing it has to do with your raycast. Likely there is a few frames after the jump key is pressed where the raycast still intersects the ground and sets canJump to true again as the character is traveling up. A quick fix would be to add a small delay before canJump can be set back to true after you've jumped.
I noticed that too though I thought GetKeyDown would cover that as it should act as the delay I need. Also I dont fully understand the Raycast as looking through the documentation gave me more questions then answers. I set the max range to a float thinking that depending on terrain incline/decline I could still be able to move normally but that appears to be not the case.
GetKeyDown will only activate for 1 frame, which is the first frame that the key press is detected. So on that frame, your character will jump. The next frame, your raycast will still detect ground because you haven't really started to move yet, and it will set canJump back to true. canJump will stay true until your next jump key press is detected, even if you are still in the air.
What raycast does is it basically starts at the point you give and then looks in a direction a certain distance to see if there are any colliders. This may or may not work depending on your game, generally people use an overlap sphere like the other answer here suggests because there may not be ground directly below the character, but perhaps slightly to the left or right of center of the character. What method you use will depend on what kind of collider your character has as well as exactly how you want your jump to behave.
Ok I see your point. It's weird though now i'm confused about the how the checked distance is measured. It's my understanding that the max distance would be from the main object and the "ground" which is at y1.
I'm using a box collider so i thought overlap sphere wouldn't apply but why not give it a shot. Oh an this is a simple rpg concept game so if I might try to scrap raycast for jumps and maybe use it for traps or something
Your answer
Follow this Question
Related Questions
Revise one conflicting code line? 1 Answer
Super Ghouls 'n Ghosts style jumps & double jumps? 1 Answer
Character Fails To Jump Sometimes 1 Answer
Jumping mechanism 1 Answer
Jump higher when holding button 5 Answers