- Home /
Ground detection (3D)
So my isGrounded check works but I want to make the groundDis variable change in relation to the ground. For example, if the ground you are standing on is 45 on the Y axis and the character is that high you can jump, but say you go up a hill and it is 57 on Y axis but you can't jump, but I want it to be able to detect the hight of the terrain and change the groundDis variable in real time in relation to the terrain height. Wow this sounded waaaayyyy better in my head.
My script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float GroundDis = 1f;
public float walkSpeed;
public float sprintSpeed;
public Rigidbody rb;
public float jHeight;
void Start () {
walkSpeed = 4f;
sprintSpeed = 10f;
rb = GetComponent<Rigidbody>();
}
void Update () {
//Walking
transform.Translate (walkSpeed*Input.GetAxis("Horizontal")*Time.deltaTime,0f,walkSpeed*Input.GetAxis("Vertical")*Time.deltaTime);
//Sprinting
if (Input.GetKey(KeyCode.LeftShift))
{
walkSpeed = sprintSpeed;
}
else
{
walkSpeed = walkSpeed;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
walkSpeed = 4f;
}
}
void FixedUpdate()
{
//jumping
if (Input.GetKeyDown(KeyCode.Space)&&isGrounded())
{
Vector3 jumpVelocity = new Vector3 (0,jHeight,0);
rb.velocity = rb.velocity + jumpVelocity;
}
}
bool isGrounded()
{
return Physics.Raycast(transform.position,Vector3.down,GroundDis);
}
}
Could you rephrase the question? I would like to help, but I don't understand what you are asking.
Looking at your code, groundDis is the range your ground check uses too find the ground with the ray cast. As the ray cast originates from the player object, it wouldn't make sense to change it based on the trerrain height, as I'm guessing you always want to have the same allowed distance from the ground. I might be misunderstanding what you are trying to do though as your question isn't very clear.
I agree with $$anonymous$$UG806. If you changed the groundDis based on the Y value of the object or even the raycast hit point, then you would be grounded while being way up in the air, or not grounded while right on the ground, depending on how your wrote the code.
If you could rephrase the question I think we could help.
Are you trying to simply make it where a player cannot jump if they are above a certain height? Or if the ground below them is a certain slope?
Answer by Zodiarc · Feb 09, 2018 at 02:43 PM
Three ways basically:
https://docs.unity3d.com/ScriptReference/TerrainData.GetHeight.html or https://docs.unity3d.com/ScriptReference/Terrain.SampleHeight.html or you make a raycast downwards everytime the player attempts to jump.
Your answer
![](https://koobas.hobune.stream/wayback/20220612152251im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
a better movement code C# 3 Answers
Multiple Cars not working 1 Answer
How to move while jumping? 1 Answer
Distribute terrain in zones 3 Answers
Better 2D Sprite Jump in 3D Plane 1 Answer