- Home /
ground check isnt working...HELP!
hey guys ...i have this script attached to a ragdoll character but when i jump he moves up without changing the is grounded bool to false so he keeps floatin upwards .....how to fix this?
using UnityEngine;
public class Jump : MonoBehaviour
{
bool isgrounded = true;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
GroundCheck();
if (Input.GetButtonDown("Jump") & (isgrounded == true))
GetComponent<ConstantForce>().force = new Vector3(0, 150, 0);
if(isgrounded == false)
GetComponent<ConstantForce>().force = new Vector3(0, 80, 0);
}
void GroundCheck()
{
RaycastHit hit;
float distance = 3f;
Vector3 dir = new Vector3(0, -2);
if (Physics.Raycast(transform.position, dir, out hit, distance))
{
isgrounded = true;
}
else
{
isgrounded = false;
}
}
}
Answer by MadDevil · May 23, 2019 at 11:00 AM
bool isgrounded = true;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
GroundCheck();
if (Input.GetButtonDown("Jump") & (isgrounded == true))
GetComponent<ConstantForce>().force = new Vector3(0, 150, 0);
if (isgrounded == false)
GetComponent<ConstantForce>().force = new Vector3(0, -80, 0);
}
void GroundCheck()
{
RaycastHit hit;
float distance = 3f;
Vector3 dir = new Vector3(0, -2);
if (Physics.Raycast(transform.position, dir, out hit, distance))
{
isgrounded = true;
}
else
{
isgrounded = false;
}
}
You just have to give negative value to your downforce in update when isgrounded is false
yeah but the "is grounded" bool isnt turning false even though my character is in the air
The code that i have posted does make the bool false. $$anonymous$$ake the bool public and watch in the inspector. Whenever you will press the space bar your character will jump and the boolean is false while its in air, For Testing purpose I have taken a cube as character and a plane below it for ground. The script is attached to the cube along with constant force component.
Answer by Captain_Pineapple · May 23, 2019 at 11:02 AM
Hey there,
i guess that if you add a Debug.Log to output what object you actually hit you will see that it's the player object itself.
Add a Layermask to disable your raycast from hitting anything else then the environment.
Is there a specific reason why you do not want to use the builtin gravity? As @MadDevil pointed out your force vector has the wrong direction. You can avoide these things by keeping it simple and sticking to the standart gravity.
@Captain_Pineapple ....i wanna make a physics based controller with an active ragdoll and this force makes the player stand on his feet thats why is positive ...how can i make the raycast hit only the floor or anything with a specific tag and not every collider?
Assign Layers your player objects to a "Player-Layer". (Somewhere in the top right corner there is a layer button. - there is also a ton of tutorials on this)
Then you can create a public Layermask
on your player script where you can select certain layers. This mask can be given to a raycast as parameter to exclude given layers from the raycast. Scripting reference can by found here. This way you can make sure the raycast ignores everything on the playerlayer.
Carefull though: If you add multiple Players they will not be able to stand on each other.