daily pastebin goal
81%
SHARE
TWEET

Untitled

a guest Jun 24th, 2014 890 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ,/*DESCRIPTION:
  2.  
  3. Controls all transformations for all fish. Serves as the base class for npcMovement and playerMovement.
  4.  
  5. moveFish(): creates a vector based off this idea (http://natureofcode.com/book/chapter-6-autonomous-agents/ , section 6.3),
  6. then translates the fish by adding a force
  7.  
  8. rotateFish(): rotates the to face the input direction (taking horizontal bias into account).  After determining horizontal
  9. bias, the vector direction is converted into an angle in degrees.  The player is then rotated to face this angle
  10. by using the Quaternion.Slerp() function.
  11.  
  12. */
  13. using UnityEngine;
  14. using System.Collections;
  15.  
  16. public class Movement : MonoBehaviour
  17. {
  18.         #region variables
  19.  
  20.     public enum Zplane
  21.     {
  22.         Normal = 0,
  23.         Back = 5
  24.     }
  25.  
  26.     //movement
  27.     private float stopAreaRadius = 0.15f;       // only used if isMouseControlled = true
  28.         [HideInNormalInspector] public Vector3 InputDirection;
  29.         public bool isMouseControlled = false;
  30.         public float drag = 4;
  31.         public float AppliedForce;
  32.         public float AbilityForceMod = 1;
  33.         public float AcquiredSpeedMultiplier = 1.0f;
  34.         private float _appForceTarget;
  35.     private float _appForceSmoothDv;
  36.         [HideInNormalInspector] public bool Moving = true;
  37.         public float FixedZ = 0.0f;
  38.         public float AngularDrag = 40f;         // I couldn't get it to work in the prefab so put it here! -PJ
  39.         public bool LockInputMagnitude = false;
  40.        
  41.         [HideInNormalInspector] public float turningValue;              // float used to indicate which direction the fish is turning
  42.        
  43.         //rotation
  44.         float horizontalBiasMod = 0.2f;
  45.         float rotationSpeed = 0.08f;   
  46.        
  47.         private Fish _thisFish;
  48.        
  49.         #endregion
  50.        
  51.         #region functions
  52.  
  53.         //initialization
  54.         public Movement Start()
  55.         {
  56.         // switch z movment plane
  57.            
  58.  
  59.         transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z * 0.95f + 0.05f * FixedZ);    // so the player / fish doesn't move away from the plane accidentally.
  60.  
  61.                 //set values for variables
  62.                 _appForceTarget = AppliedForce;
  63.                 rigidbody.useGravity = false;
  64.                 _thisFish = Helper.GetFish(transform);
  65.                 if (_thisFish == null)
  66.                 {
  67.                         Debug.LogError("Can't find FISH for Movement!");
  68.                 }
  69.                 return this;
  70.         }
  71.  
  72.         void Update()
  73.         {
  74.                 if (isMouseControlled) // then read the mouse and set the inputdirection
  75.                 {
  76.                         //movement
  77.                         Moving = true;
  78.                         InputDirection = new Vector3(Input.mousePosition.x,Input.mousePosition.y,0) - new Vector3(Screen.width/2,Screen.height/2,0);//assign inputDirection
  79.                         InputDirection = InputDirection * (1/((float)Screen.height/2));//normalise inputDirection
  80.                         if(InputDirection.magnitude>1){InputDirection=InputDirection.normalized;}//makes sure inputDireciton does not exceed 1
  81.                         else if(InputDirection.magnitude<stopAreaRadius){Moving = false;}//enables player to sotp
  82.                 }
  83.         }
  84.         //movement
  85.         void FixedUpdate ()
  86.         {
  87.                
  88.                 if (_thisFish.IsBeingEaten)
  89.                 {
  90.                         Transform newpos = _thisFish.BeingEatenTransform;
  91.                        
  92.                         Transform x = _thisFish.FishEatingMe.GetComponentInChildren<DamageGobble>().transform.Find("EatPos");
  93.                        
  94.                         gameObject.transform.position = newpos.position;
  95.                         gameObject.transform.rotation = newpos.rotation;
  96.                        
  97.                         gameObject.rigidbody.velocity = Vector3.zero;           // Need these to stop the fish moving from the spot we want!
  98.                         gameObject.rigidbody.angularVelocity = Vector3.zero;
  99.                         return;
  100.                 }
  101.                 if (!_thisFish.isAlive()) { return;}    // disable all movement
  102.                 if(gameObject.tag == "Player")
  103.                 {
  104.                         //move player
  105.                         if(transform.position.y < 10) //underwater
  106.                         {
  107.                                 //used to ensure a smooth change of velocity when entering water
  108.                                 if(Moving)AppliedForce = Mathf.SmoothDamp(AppliedForce, _appForceTarget*AbilityForceMod, ref _appForceSmoothDv,0.2f);
  109.                                 else AppliedForce = Mathf.SmoothDamp(AppliedForce, 0, ref _appForceSmoothDv, 0.2f);
  110.                                
  111.                                 rigidbody.useGravity = false;
  112.                                 rigidbody.drag = drag;
  113.                                 MoveFish();
  114.                                 RotateFish(InputDirection);
  115.                         }
  116.                         else //breaching
  117.                         {
  118.                                 //used to ensure a smooth change of velocity when entering water
  119.                                 AppliedForce = Mathf.SmoothDamp(AppliedForce,0,ref _appForceSmoothDv,0.2f);
  120.                                
  121.                                 rigidbody.useGravity = true;
  122.                                 rigidbody.drag = 0;
  123.                                 RotateFish(rigidbody.velocity);
  124.                         }
  125.                 }
  126.                 else // Not the player - AI
  127.                 {
  128.                         if(transform.position.y < 10) //underwater
  129.                         {
  130.                                 rigidbody.useGravity = false;
  131.                                 rigidbody.drag = drag;
  132.                                 MoveFish();
  133.                                 RotateFish(InputDirection);
  134.                         }
  135.                         else // breaching
  136.                         {
  137.                                 rigidbody.useGravity = true;
  138.                                 rigidbody.drag = 0;
  139.                                 RotateFish(rigidbody.velocity);
  140.                         }
  141.                 }
  142.                 rigidbody.angularDrag = AngularDrag;
  143.                 transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z *0.95f + 0.05f *FixedZ);      // so the player / fish doesn't move away from the plane accidentally.
  144.         }
  145.  
  146.         public float getAppliedForce()
  147.         {
  148.                 float f = AppliedForce;
  149.                 f *= Mathf.Sqrt(1.0f + _thisFish.AcquiredSpeedBoost * AcquiredSpeedMultiplier);
  150.                 return f;
  151.         }
  152.         void MoveFish()
  153.         {
  154.                 float f= getAppliedForce();
  155.                 //Debug.Log("f = "+f+"  AppliedForce = "+AppliedForce);
  156.                 rigidbody.AddForce( f * ((LockInputMagnitude)?InputDirection.normalized:InputDirection) - rigidbody.velocity );
  157.                
  158.         }
  159.  
  160.         private void RotateFish(Vector3 direction)
  161.         {
  162.                 //horizontal bias
  163.                 direction = direction + (1/rigidbody.velocity.magnitude)*horizontalBiasMod*((direction.x>0) ? new Vector3(1,0,0) : new Vector3(-1,0,0)).normalized;
  164.                
  165.                 //convert vector to angle
  166.                 float angle = -1 * ((Mathf.Atan2(direction.x,direction.y) * Mathf.Rad2Deg) -90);
  167.                 if (float.IsNaN (angle) )
  168.                 {      
  169.                         angle = 0f;     // hack
  170.                 }
  171.                 if(angle > 180) angle -= 360;
  172.                
  173.                 //rotate player
  174.                
  175.                 if(angle > -90 && angle < 90) //facing right
  176.                 {
  177.                         transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.Euler(new Vector3(0, 0, angle)), rotationSpeed);
  178.                         turningValue = -transform.rotation.eulerAngles.y;
  179.                 }
  180.                 else //facing left
  181.                 {
  182.                         transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.Euler(new Vector3(0, 180, 180-angle)), rotationSpeed);
  183.                         turningValue = 180 - transform.rotation.eulerAngles.y;
  184.                 }
  185.                 if (Mathf.Abs(turningValue +360) < Mathf.Abs(turningValue) )
  186.                 {
  187.                         turningValue = turningValue +360;
  188.                 }
  189.         }
  190.         #endregion
  191. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top