Advertisement
BlueBear

FinalShipControl.cs

Feb 12th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.04 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class FinalShipControl : MonoBehaviour {
  5.  
  6.     public bool queryUserInput;
  7.     public float mass = 3000;
  8.     //public int propulsorCount;
  9.     //public float waveLenght;
  10.     //public float waveHeight;
  11.     //public float jerkingLength;
  12.     //public float jerkingHeight;
  13.     public float waterLevel;
  14.     public float distanceFromWaterLevel;
  15.     public float percentUnderWater;
  16.     public AudioClip motorSound;
  17.     public float motor;
  18.     public float steer;
  19.     public float rudder = 40;
  20.     public float propellerTurningAngle = 20;
  21.     public float rpmPitch;
  22.     public float forwardVelo;
  23.     public Vector3 propellerPos;
  24.     public Vector3 propellerPosGlobal;
  25.     public Vector3 propellerDir;
  26.     public Vector3 drag = new Vector3(6.0f, 4.0f, 0.2f);
  27.     public Vector3 dragForces;
  28.     public Vector3 dragDirection;
  29.     public Vector3 dragAttackPosition;
  30.     public Vector3 buoyancyPos;
  31.     public float depth;
  32.     public float volume = 9000;
  33.     public float steeringAngle;
  34.     public float engineForce = 10000;
  35.     public Vector3 gravity;
  36.     public Vector3 size = new Vector3(3,3,10);
  37.     public float angularDrag = 0.8f;
  38.     public FloatableWater waterSurface = null;
  39.     // Use this for initialization
  40.     void Start () {
  41.         //gravity = new Vector3(0, -9, 0);
  42.         //size = GetComponent<BoxCollider>().size;
  43.         rigidbody.mass = mass;
  44.         rigidbody.angularDrag = angularDrag;
  45.         rigidbody.interpolation = RigidbodyInterpolation.Interpolate;
  46.     }
  47.  
  48.     // Update is called once per frame
  49.     void FixedUpdate () {
  50.  
  51.         /*if(waveLenght <= 0)
  52.         {
  53.             waveLenght = 0.1f;
  54.         }
  55.         else if(jerkingLength <= 0)
  56.         {
  57.             jerkingLength = 0.1f;
  58.         }*/
  59.  
  60.         //if there is no water surface we are colliding with, no boat physics  
  61.         if(waterSurface == null)
  62.         {      
  63.             return;
  64.         }
  65.  
  66.         //query input axes if necessarry
  67.         //motor = 0.0f;
  68.         //steer = 0.0f;
  69.        
  70.         if(queryUserInput)
  71.         {
  72.             motor = Input.GetAxis("Vertical");
  73.             steer = Input.GetAxis("Horizontal");
  74.         }
  75.        
  76.         //get water level and percent under water
  77.         waterLevel=waterSurface.collider.bounds.max.y;
  78.         distanceFromWaterLevel = transform.position.y-waterLevel;
  79.         percentUnderWater = Mathf.Clamp01((-distanceFromWaterLevel + 0.5f*size.y)/size.y);
  80.  
  81.         //Buoyancy (the force which keeps the boat floating above water)
  82.         //--------------------------------------------------------------
  83.         //the point the buoyancy force is applied onto is calculated based
  84.         //on the boat's picth and roll, so it will always tilt upwards:
  85.         buoyancyPos=transform.TransformPoint(-new Vector3(transform.right.y*size.x*0.5f,0,transform.forward.y*size.z*0.5f));
  86.         //then it is shifted arcording to the current waves
  87.         buoyancyPos.x+=waterSurface.waveXMotion1*Mathf.Sin(waterSurface.waveFreq1*Time.time)
  88.             +waterSurface.waveXMotion2*Mathf.Sin(waterSurface.waveFreq2*Time.time)
  89.                 +waterSurface.waveXMotion3*Mathf.Sin(waterSurface.waveFreq3*Time.time);
  90.         buoyancyPos.z+=waterSurface.waveYMotion1*Mathf.Sin(waterSurface.waveFreq1*Time.time)
  91.             +waterSurface.waveYMotion2*Mathf.Sin(waterSurface.waveFreq2*Time.time)
  92.                 +waterSurface.waveYMotion3*Mathf.Sin(waterSurface.waveFreq3*Time.time);
  93.         //apply the force  
  94.         rigidbody.AddForceAtPosition(- volume * percentUnderWater * Physics.gravity , buoyancyPos);
  95.         //Engine   
  96.         //--------------------------------------------------------------
  97.         //calculate propeller position
  98.         propellerPos = new Vector3(0,-size.y*0.5f,-size.z*0.5f);
  99.         propellerPosGlobal=transform.TransformPoint(propellerPos);
  100.         //apply force only if propeller is under water
  101.         if(propellerPosGlobal.y<waterLevel)
  102.         {
  103.             //direction propeller force is pointing to.
  104.             //mostly forward, rotated a bit according to steering angle
  105.             steeringAngle = steer * propellerTurningAngle * Mathf.Deg2Rad;     
  106.             propellerDir = transform.forward*Mathf.Cos(steeringAngle) - transform.right*Mathf.Sin(steeringAngle);
  107.             //apply propeller force
  108.             rigidbody.AddForceAtPosition(propellerDir * engineForce * motor, propellerPosGlobal);
  109.  
  110.         }
  111.        
  112.         //Drag
  113.         //--------------------------------------------------------------
  114.         //calculate drag force
  115.         dragDirection = transform.InverseTransformDirection(rigidbody.velocity);
  116.         dragForces = -Vector3.Scale(dragDirection, drag);
  117.         //depth of the boat under water (used to find attack point for drag force)
  118.         depth = Mathf.Abs(transform.forward.y)*size.z*0.5f+Mathf.Abs(transform.up.y)*size.y*0.5f;
  119.         //apply force
  120.         dragAttackPosition = new Vector3(transform.position.x,waterLevel-depth,transform.position.z);
  121.         rigidbody.AddForceAtPosition(transform.TransformDirection(dragForces) * rigidbody.velocity.magnitude * (1 + percentUnderWater * (waterSurface.waterDragFactor - 1)), dragAttackPosition);
  122.         //linear drag (linear to velocity, for low speed movement)
  123.         rigidbody.AddForce(transform.TransformDirection(dragForces)*500);
  124.         //rudder torque for steering (square to velocity)
  125.         forwardVelo = Vector3.Dot(rigidbody.velocity,transform.forward);
  126.         rigidbody.AddTorque(transform.up*forwardVelo*forwardVelo*rudder*steer);
  127.         //Sound
  128.         //--------------------------------------------------------------
  129.         //audio.volume=0.3f+Mathf.Abs(motor);
  130.         //slowly adjust pitch to power input
  131.         rpmPitch=Mathf.Lerp(rpmPitch,Mathf.Abs(motor),Time.deltaTime*0.4f);
  132.         //audio.pitch=0.3f+0.7f*rpmPitch;
  133.         //reset water surface, so we have to stay in contact for boat physics.
  134.        
  135.         waterSurface = null;
  136.  
  137.         //this.transform.position = elevate(Time.time);
  138.         //this.transform.rotation = jerk(Time.time);
  139.  
  140.  
  141.     }
  142.  
  143.     void OnTriggerStay(Collider col)
  144.     {
  145.         if(col.GetComponent<FloatableWater>() != null)
  146.         {
  147.             waterSurface = col.GetComponent<FloatableWater>();
  148.         }
  149.     }
  150.  
  151.     /*public Vector3 elevate(float t)
  152.     {
  153.         //Vector3 newVec =
  154.  
  155.         return new Vector3(this.transform.position.x, this.transform.position.y + (Mathf.Sin (t / waveLenght) * waveHeight), this.transform.position.z);
  156.     }
  157.  
  158.     public Quaternion jerk(float t)
  159.     {
  160.         float val = (Mathf.Cos (t / jerkingLength) * jerkingHeight);
  161.         return Quaternion.Euler(new Vector3(this.transform.rotation.eulerAngles.x + val, this.transform.rotation.eulerAngles.y, this.transform.rotation.eulerAngles.z + Random.Range(0, val)));
  162.     }*/
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement