d3ullist

Untitled

Jan 29th, 2015
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public class Buoyancy : MonoBehaviour
  5. {
  6.     private Ocean ocean;
  7.     private float mag = 1f;
  8.     private float ypos = 0.0f;
  9.     private List<Vector3> blobs;
  10.     private float ax = 2.0f;
  11.     private float ay = 2.0f;
  12.     private float dampCoeff = .2f;
  13.     public bool sink = false;
  14.     public float sinkForce = 3;
  15.     private List<float> sinkForces;
  16.  
  17.     void Start()
  18.     {
  19.  
  20.         rigidbody.centerOfMass = new Vector3(0.0f, -0.5f, 0.0f);
  21.  
  22.         Vector3 bounds = GetComponent<BoxCollider>().size;
  23.         float length = bounds.z;
  24.         float width = bounds.x;
  25.  
  26.         blobs = new List<Vector3>();
  27.  
  28.         int i = 0;
  29.         float xstep = 1.0f / (ax - 1f);
  30.         float ystep = 1.0f / (ay - 1f);
  31.  
  32.         sinkForces = new List<float>();
  33.  
  34.         float totalSink = 0;
  35.  
  36.         for (int x = 0; x < ax; x++)
  37.         {
  38.             for (int y = 0; y < ay; y++)
  39.             {
  40.                 blobs.Add(new Vector3((-0.5f + x * xstep) * width, 0.0f, (-0.5f + y * ystep) * length) + Vector3.up * ypos);
  41.  
  42.                 float force = Random.Range(0f, 1f);
  43.  
  44.                 force = force * force;
  45.  
  46.                 totalSink += force;
  47.  
  48.                 sinkForces.Add(force);
  49.                 i++;
  50.             }
  51.         }
  52.  
  53.         // normalize the sink forces
  54.         for (int j = 0; j < sinkForces.Count; j++)
  55.         {
  56.             sinkForces[j] = sinkForces[j] / totalSink * sinkForce;
  57.         }
  58.  
  59.     }
  60.  
  61.     void OnEnable()
  62.     {
  63.         if (ocean == null)
  64.             ocean = GameObject.FindGameObjectWithTag("Ocean").GetComponent<Ocean>();
  65.     }
  66.  
  67.     void FixedUpdate()
  68.     {
  69.         int index = 0;
  70.         Debug.Log(this.rigidbody.velocity);
  71.         foreach (Vector3 blob in blobs)
  72.         {
  73.  
  74.             Vector3 wpos = transform.TransformPoint(blob);
  75.             float damp = rigidbody.GetPointVelocity(wpos).y;
  76.  
  77.             float buyancy = mag * (wpos.y);
  78.             if (ocean.enabled && !sink)
  79.                 buyancy = mag * (wpos.y - ocean.GetWaterHeightAtLocation(wpos.x, wpos.z));
  80.  
  81.             if (sink)
  82.             {
  83.                 buyancy = Mathf.Max(buyancy, -3) + sinkForces[index++];
  84.             }
  85.             rigidbody.AddRelativeForce(-Vector3.up * (buyancy + dampCoeff * damp), ForceMode.Impulse);
  86.             //rigidbody.AddForceAtPosition(-Vector3.up * (buyancy + dampCoeff * damp), wpos);
  87.         }
  88.     }
  89.  
  90.     // Call this void when you want your boat or any object to sink more naturally
  91.     public void Sink(bool isActive)
  92.     {
  93.         sink = isActive;
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment