I have two objects, one is the parent and one is the child, the child object has a raycast to check if the object is hitting the ground, when the raycast is whithing the right distance, it changes the isGrounded variable on the parent which stops it from moving. On the child object inside the Start function I add the parents script into a variable, then when the Raycast detects the right range its changes the isGrounded variable on the parent script. This works but the problem is that the delay between setting the isGrounded to true the object has moved further and is then slight embedded into the ground. So is there a faster method for changing a variable on another gameobject?
To affect a value to a variable is effective instantly, especially if you don’t need to look for the component each time. Maybe your character takes time to use that variable. You could try to test if ground with a higher approximation, ?
Perhaps instead of using a ray cast you may want to use OnCollisionEnter, or OnTriggerEnter.
Put your isGrounded into a public method so that other objects can use it.
public void IsGrounded(){
//changes isGrounded to it's opposite
isGrounded = !isGrounded;
}
void OnCollisionEnter(Collision collision){
collision.gameObject.GetComponent().IsGrounded();
}
or if you want to use Triggers:
void OnTriggerEnter(Collider trigger){
trigger.gameObject.GetComponent<script>().IsGrounded();
}
This is simple take on something I would do.