I try to float a rigidbody object in a distance of ground, with Raycast + "AddForce" or "MovePosition" but It's not working well, Code (CSharp): public float Distancevalue; public LayerMask Mymask; void FixedUpdate() { if (Physics.Raycast(transform.position, Vector3.down, Distancevalue)) { RaycastHit hitInfo; Physics.Raycast(transform.position, Vector3.down, out hitInfo, Distancevalue, Mymask.value); float correction = Distancevalue - hitInfo.distance; // transform.position = new Vector3(transform.position.x, transform.position.y + correction, transform.position.z); // GetComponent<Rigidbody>().AddForce(transform.position.x, transform.position.y + correction, transform.position.z); GetComponent<Rigidbody>().MovePosition(new Vector3(transform.position.x, transform.position.y + correction, transform.position.z)); } } is there something wrong ? thanks
You might need to set the rigidbody "Is kinematic" when the object needs to hover and put it back once it no longer needs to hover. Code (CSharp): private Rigidbody rigidBody; private void Start() { rigidBody = GetComponent<Rigidbody>(); } private void FixedUpdate() { rigidBody.isKinematic = true; } Code (CSharp): private Rigidbody rigidBody; private void Start() { rigidBody = GetComponent<Rigidbody>(); } private void FixedUpdate() { rigidBody.isKinematic = false; }
I need the rigidbody object to float all the time with a specific distance to the ground, but I need to mask some objects on the ground to just fly over them and keep the object distance to the ground. did you mean, I should make "isKinematic = false" once the rigidbody detect an object on the bottom?