Why my buoyacy script doesen't work?
I am coding a navigation game but im stuck with buoyancy, i've coded this script for a floating object with a convex collider and a rigidbody attached, but when play the game nothing happens. this is the code:
using UnityEngine;
public class FloatingObj : MonoBehaviour { [SerializeField] float RD; [SerializeField] float WaterLevel; [SerializeField] float Density; [SerializeField] float objWidth = 10, objLength = 5; [SerializeField] float BPrecision = 50; void Start() { } private void FixedUpdate() {
for (float iz = -objLength; iz < objLength; iz += objLength / BPrecision)
{
for (float ix = -objWidth; ix < objWidth; ix +=objWidth/BPrecision)
{
RaycastHit hit;
Ray ray = new Ray(transform.position + new Vector3(ix, -RD, iz), Vector3.up);
float maxDistance = transform.position.y - RD - WaterLevel;
if (Physics.Raycast(ray, out hit, maxDistance,0))
{
Debug.DrawRay(ray.origin, ray.direction,Color.magenta);
if (hit.collider.gameObject == gameObject && hit.point.y <= WaterLevel)
{
gameObject.GetComponent<Rigidbody>().AddForceAtPosition(Vector3.up * (maxDistance - hit.distance) * Density / BPrecision, hit.point);
Debug.DrawLine(hit.point + new Vector3 (0, 10), hit.point + (Vector3.up * (maxDistance - hit.distance) * Density / BPrecision)+ new Vector3(0, 10), Color.red);
}
}
}
}
}
}
Comment