- Home /
Minecraft Style Auto Jumping
Does anyone know how the "Auto Jumping" is achieved in Minecraft?
For those who don't know "Auto Jump" will allow the player to climb blocks without having to press the space bar.
I was thinking a system like that could be good for walking up stairs, but unsure.
Let me know your thoughts, thanks!=
Answer by HellsHand · Apr 14, 2021 at 03:43 PM
You could use a raycast, when it hits an object you dub as climbable, the character jumps. As far as how it's done in Minecraft, I haven't coded since Java edition was only available.
I guess another less taxing way than a constant raycast could be to add a collider a little bigger than the object to jump up that acts as a trigger and when the player enters the trigger, they jump.
Answer by Nistroy · Apr 15, 2021 at 03:59 PM
I tried since yesterday ways to make the automatic jump and tell me if it suits you @Razputin
using UnityEngine;
using UnityEngine.Events;
public class AutoJumpScript : MonoBehaviour
{
RaycastHit[] hits;
// Capture all the GameObjects you have to jump
float distance;
// The distance at which it detects a GameObject.
[SerializeField] float maxDistance = 20f;
// The value not to be exceeded for the maximum distance at which it detects a GameObject.
[SerializeField] float minDistance = -10f;
// The value not to be exceeded for the minimum distance at which it detects a GameObject.
[SerializeField] LayerMask groundMask;
// The LayerMask that detects.
//[SerializeField] --- (if you want to reference it from the inspector, then remove the double slashes)
PlayerMovement movement;
// Your player's movement script reference.
[SerializeField] UnityEvent jumpScript;
// The jump event to Invoke.
void Start()
{
movement = GetComponentInParent<PlayerMovement>();
}
// Update is called once per frame
void Update()
{
distance = Mathf.Clamp(GetMax(Mathf.Abs(movement.hozizontalmovement), Mathf.Abs(movement.verticalmovement)), minDistance, maxDistance);
// The "distance" value clamped between the maximum and minimum value
// Horizontal movement and vertical movement are the values when the player is moving (Input.GetAxis ("Horizontal") for example)
hits = Physics.RaycastAll(transform.position, new Vector3(movement.hozizontalmovement, 0, movement.verticalmovement), distance, groundMask);
// Get all the GameObjects detected with the LayerMask defined in "groundMask" with the distance at which it can detect a game object and the direction the raycast goes.
foreach (RaycastHit hit in hits)
{
Debug.Log(hit.transform.name);
jumpScript.Invoke(); // Invoke the jump function
break; // Stop the loop to avoid calling it more than once, although normally you can jump again only if you hit the ground.
}
}
public static float GetMax(float first, float second)
{
return first > second ? first : second;
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawRay(transform.position, new Vector3(movement.hozizontalmovement, 0, movement.verticalmovement));
}
}
Edit: you need to place the GameObject with this component in the parent Player object. Adjust the Y axis position to jump according to the desired height.
Your answer
Follow this Question
Related Questions
Can't get character to Jump on the Y Axis (C#) 2 Answers
jump not working,Code for jump isn't working, help! 1 Answer
Jump in multiplayer,How can I Jump Multiplayer 2 Answers
how can I make the character jump more by holding the jump button in this script? 1 Answer
(New input system) How to queue a jump before you touch the ground? 0 Answers