- Home /
How to float a boat in a deformed mesh?
I have a script which deforms a mesh. This script also updates the mesh collider so that the waves will properly collide with things and shove them around. This part works super awesome great.
I've gone this route to attempt to add buoyancy and I think the 'fake' hovering object is good because you can definitely tell how close to the water the object is but I'm having trouble nailing force modes and when to apply the force.
#pragma strict
var raycaster : GameObject;
function Start()
{
//create the primitive
raycaster = GameObject.CreatePrimitive(PrimitiveType.Sphere);
//turn off the renderer on it
raycaster.gameObject.renderer.enabled = false;
//parent the primitive
raycaster.gameObject.transform.parent = gameObject.transform;
//place the primitive
raycaster.transform.position = Vector3(transform.position.x, transform.position.y+100, transform.position.z);
//name the primitive
raycaster.name = "raycaster";
}
function Update ()
{
var hitall : RaycastHit[] = Physics.RaycastAll(raycaster.transform.position, -Vector3.up, 1000) ;
print(hitall[0].collider.gameObject.name);
print(hitall[0].distance);
if(hitall[0].collider.gameObject.name == "Water")
{
if(hitall[0].distance < 100)
{
print("adding force");
rigidbody.AddForce( Vector3(0,100-hitall[0].distance,0), ForceMode.Force);
}
else
{
rigidbody.AddForce( Vector3(0,-100,0), ForceMode.Force);
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613161003im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Getting Y position at a specific point 0 Answers
Buoyancy using shaders 2 Answers
Md5mesh uvs and vertices bugging 0 Answers
Weapon system 0 Answers
Normal vector from collision? 1 Answer