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;
}
}
}
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
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.