Advertisement
Guest User

PlatformInputController_NPC.js

a guest
Aug 26th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This makes the character turn to face the current movement speed per default.
  2. var autoRotate : boolean = true;
  3. var maxRotationSpeed : float = 360;
  4. var character : SmoothMoves.BoneAnimation;
  5. //var wayPoints : Transform[];
  6. //var alertRange : int;
  7. //var attackRange : int;
  8. //var target : Transform;
  9. //var facingRight : boolean;
  10. //var idleDuration : float;
  11. //
  12. private var motor : NPCMotor;
  13. private var moveLeft : boolean;
  14. private var moveRight : boolean;
  15. //private var reachPoint : boolean;
  16. //
  17. //private var currentWayPoint : int;
  18. //private var lastArrive : float;
  19. //private var isFacingRight : boolean;
  20. //private var idleing : boolean;
  21. //private var idleStart : float;
  22. //
  23. //private var goAttack : boolean;
  24.  
  25. // Use this for initialization
  26. function Awake () {
  27. //  secondStage = false;
  28. //  moveLeft = false;
  29. //  moveRight = false;
  30.     motor = GetComponent(NPCMotor);
  31.     NoMove();
  32. //  currentWayPoint = 0;
  33. //  lastArrive = Time.time;
  34. //  isFacingRight = facingRight;
  35. //  idleing = false;
  36. //  goAttack = false;
  37. }
  38.  
  39. //public function Attack()
  40. //{
  41. //  goAttack = true;
  42. //}
  43.  
  44. //function AI()
  45. //{
  46. //  //if player is in NPC's watching range
  47. //  if(Vector3.Distance(target.position, transform.position) <= alertRange || goAttack)
  48. //  {
  49. //      idleing = false;
  50. //      if(Mathf.Abs(target.position.x - transform.position.x) <= attackRange)
  51. //      {
  52. //          Debug.Log("tell: " + Mathf.Abs(target.position.x - transform.position.x));
  53. //          character.Play("IDLE_side");
  54. //          NoMove();
  55. //      }
  56. //      else if(target.position.x < transform.position.x)
  57. //      {
  58. //          character.Play("Walk_side");
  59. //          if(isFacingRight)
  60. //              transform.localScale.x *= -1;
  61. //          isFacingRight = false;
  62. //          LeftMove();
  63. //      }
  64. //      else if(target.position.x > transform.position.x)
  65. //      {
  66. //          character.Play("Walk_side");
  67. //          if(!isFacingRight)
  68. //              transform.localScale.x *= -1;
  69. //          isFacingRight = true;
  70. //          RightMove();
  71. //      }      
  72. //  }
  73. //  else
  74. //  {
  75. //      if(idleing)
  76. //      {
  77. //          if(Time.time - idleStart >= idleDuration)
  78. //          {
  79. //              idleing = false;
  80. //          }
  81. //      }
  82. //      else if(Mathf.Abs(wayPoints[currentWayPoint].position.x - transform.position.x) <= 10.0f &&
  83. //          Time.time - lastArrive > 1.0f)
  84. //      {
  85. //          character.Play("IDLE_side");
  86. //          lastArrive = Time.time;
  87. //          idleing = true;
  88. //          idleStart = Time.time;
  89. //          NoMove();
  90. //          if(currentWayPoint == 0)
  91. //              currentWayPoint = 1;
  92. //          else
  93. //              currentWayPoint = 0;
  94. //      }
  95. //      else if(wayPoints[currentWayPoint].position.x < transform.position.x)
  96. //      {
  97. //          character.Play("Walk_side");
  98. //          if(isFacingRight)
  99. //              transform.localScale.x *= -1;
  100. //          isFacingRight = false;
  101. //          LeftMove();
  102. //      }
  103. //      else if(wayPoints[currentWayPoint].position.x > transform.position.x)
  104. //      {
  105. //          character.Play("Walk_side");
  106. //          if(!isFacingRight)
  107. //              transform.localScale.x *= -1;
  108. //          isFacingRight = true;
  109. //          RightMove();
  110. //      }
  111. //  }
  112. //}
  113.  
  114. //Call by playMaker
  115. function ManuallyAttack()
  116. {
  117.  
  118. }
  119.  
  120. function LeftMove (speed : float)
  121. {
  122.     motor.LeftMove (speed);
  123.     moveLeft = true;
  124.     moveRight = false;
  125. }
  126.  
  127. function RightMove (speed : float)
  128. {
  129.     motor.RightMove (speed);
  130.     moveLeft = false;
  131.     moveRight = true;
  132. }
  133.  
  134. function NoMove ()
  135. {
  136.     motor.NoMove ();
  137.     moveLeft = false;
  138.     moveRight = false;
  139.     reachPoint = false;
  140. }
  141.  
  142. function StopMove()
  143. {
  144.     motor.NoMove ();
  145.     moveLeft = false;
  146.     moveRight = false;
  147.     reachPoint = true;
  148. }
  149.  
  150.  
  151. // Update is called once per frame
  152. function Update () {
  153.     // Get the input vector from kayboard or analog stick
  154.     var moveRate : float;
  155.  
  156. //  AI();
  157.    
  158.    
  159. //  if(moveRight)
  160. //  {
  161. //      moveRate = 1.0f;
  162. //  }
  163. //  else if(moveLeft)
  164. //  {
  165. //      moveRate = -1.0f;
  166. //  }
  167. //  else
  168. //  {
  169. //      moveRate = 0.0f;
  170. //  }
  171.  
  172.    
  173.     var directionVector = new Vector3(moveRate, Input.GetAxis("Vertical"), 0);
  174.    
  175.     if (directionVector != Vector3.zero) {
  176.         // Get the length of the directon vector and then normalize it
  177.         // Dividing by the length is cheaper than normalizing when we already have the length anyway
  178.         var directionLength = directionVector.magnitude;
  179.         directionVector = directionVector / directionLength;
  180.        
  181.         // Make sure the length is no bigger than 1
  182.         directionLength = Mathf.Min(1, directionLength);
  183.        
  184.         // Make the input vector more sensitive towards the extremes and less sensitive in the middle
  185.         // This makes it easier to control slow speeds when using analog sticks
  186.         directionLength = directionLength * directionLength;
  187.        
  188.         // Multiply the normalized direction vector by the modified length
  189.         directionVector = directionVector * directionLength;
  190.     }
  191.    
  192.     // Rotate the input vector into camera space so up is camera's up and right is camera's right
  193.     directionVector = Camera.main.transform.rotation * directionVector;
  194.    
  195.     // Rotate input vector to be perpendicular to character's up vector
  196.     var camToCharacterSpace = Quaternion.FromToRotation(-Camera.main.transform.forward, transform.up);
  197.     directionVector = (camToCharacterSpace * directionVector);
  198.    
  199.     // Apply the direction to the CharacterMotor
  200.     motor.inputMoveDirection = directionVector;
  201.     motor.inputJump = Input.GetButton("Jump");
  202.    
  203.     // Set rotation to the move direction  
  204.     if (autoRotate && directionVector.sqrMagnitude > 0.01) {
  205.         var newForward : Vector3 = ConstantSlerp(
  206.             transform.forward,
  207.             directionVector,
  208.             maxRotationSpeed * Time.deltaTime
  209.         );
  210.         newForward = ProjectOntoPlane(newForward, transform.up);
  211.         transform.rotation = Quaternion.LookRotation(newForward, transform.up);
  212.     }
  213. }
  214.  
  215. function ProjectOntoPlane (v : Vector3, normal : Vector3) {
  216.     return v - Vector3.Project(v, normal);
  217. }
  218.  
  219. function ConstantSlerp (from : Vector3, to : Vector3, angle : float) {
  220.     var value : float = Mathf.Min(1, angle / Vector3.Angle(from, to));
  221.     return Vector3.Slerp(from, to, value);
  222. }
  223.  
  224. // Require a character controller to be attached to the same game object
  225. @script RequireComponent (NPCMotor)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement