Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Bouyancy : MonoBehaviour {
- public float offset;
- public float mass;
- public float density;
- float waterHeight;
- public float gravity;
- public float fluidDensity;
- public float dampingCoefficient;
- Vector3 boatSize;
- public float percentUnderwater;
- float yVel;
- // Use this for initialization
- void Start () {
- Ray ray = new Ray(transform.position + new Vector3(0, 500, 0), Vector3.down);
- RaycastHit[] hits = Physics.RaycastAll(ray);
- foreach (RaycastHit hit in hits){
- if (hit.transform.gameObject.layer == 4){
- waterHeight = hit.point.y;
- }
- }
- BoxCollider boxcol = this.gameObject.GetComponent("BoxCollider") as BoxCollider;
- boatSize = boxcol.size;
- }
- // Update is called once per frame
- void FixedUpdate () {
- percentUnderwater = (waterHeight - (transform.position.y - offset - (0.5f * boatSize.y)))/boatSize.y;
- if (percentUnderwater < 0f) percentUnderwater = 0f;
- float accelY = 0f;
- accelY = gravity * (fluidDensity/density * percentUnderwater - 1f);
- yVel += (accelY);
- yVel *= dampingCoefficient;
- transform.position += new Vector3(0f, yVel, 0f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement