Advertisement
Kofthefens

Bouyancy

Jul 26th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Bouyancy : MonoBehaviour {
  5.    
  6.     public float offset;
  7.     public float mass;
  8.     public float density;
  9.     float waterHeight;
  10.     public float gravity;
  11.     public float fluidDensity;
  12.     public float dampingCoefficient;
  13.     Vector3 boatSize;
  14.     public float percentUnderwater;
  15.    
  16.     float yVel;
  17.    
  18.     // Use this for initialization
  19.     void Start () {
  20.         Ray ray = new Ray(transform.position + new Vector3(0, 500, 0), Vector3.down);
  21.         RaycastHit[] hits = Physics.RaycastAll(ray);
  22.         foreach (RaycastHit hit in hits){
  23.             if (hit.transform.gameObject.layer == 4){
  24.                 waterHeight = hit.point.y;
  25.             }
  26.         }
  27.        
  28.         BoxCollider boxcol = this.gameObject.GetComponent("BoxCollider") as BoxCollider;
  29.         boatSize = boxcol.size;
  30.     }
  31.    
  32.     // Update is called once per frame
  33.     void FixedUpdate () {
  34.        
  35.         percentUnderwater = (waterHeight - (transform.position.y - offset - (0.5f * boatSize.y)))/boatSize.y;
  36.         if (percentUnderwater < 0f) percentUnderwater = 0f;
  37.        
  38.         float accelY = 0f;
  39.         accelY = gravity * (fluidDensity/density * percentUnderwater - 1f);
  40.         yVel += (accelY);
  41.         yVel *= dampingCoefficient;
  42.        
  43.         transform.position += new Vector3(0f, yVel, 0f);
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement