Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4.  
  5.  
  6. public class PlayerController : MonoBehaviour {
  7.  
  8.  
  9. //Public Vars
  10. [SerializeField]
  11. public Rigidbody2D arrowpoint;
  12. [SerializeField]
  13. public Camera camera;
  14. [SerializeField]
  15. public Rigidbody2D arrow;
  16. public static bool shoot;
  17. public Vector3 mousePosition;
  18. public PlayerController play;
  19.  
  20. //Private Vars
  21.  
  22. private Vector3 direction;
  23. private float distanceFromObject;
  24. private float angle;
  25. private Rigidbody2D rigidbody;
  26. private bool isAiming;
  27. private bool rotating;
  28. private Animator anim;
  29.  
  30.  
  31. public Vector3 dir;
  32. public bool test;
  33.  
  34.  
  35. void Start() {
  36. rigidbody = GetComponent<Rigidbody2D> ();
  37. anim = GetComponent<Animator> ();
  38. shoot = false;
  39. test = true;
  40. rotating = false;
  41. }
  42.  
  43. void FixedUpdate() {
  44.  
  45. HandleRotation ();
  46.  
  47.  
  48. }
  49.  
  50. void Update(){
  51. HandleShoot();
  52. AddPower ();
  53. HandleController ();
  54. ArrowFlight();
  55. }
  56.  
  57. void HandleRotation(){
  58.  
  59. if (Input.GetMouseButton (0)) {
  60.  
  61.  
  62. if (ArrowControl.thrust < 1000f) {
  63. ArrowControl.thrust++;
  64. ArrowControl.thrust++;
  65. } else {
  66. ArrowControl.thrust = Random.Range (900f, 1000f);
  67. }
  68.  
  69.  
  70.  
  71. isAiming = true;
  72.  
  73. Vector3 pos = Camera.main.WorldToScreenPoint (transform.position);
  74. Vector3 dir = Input.mousePosition - pos;
  75. angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg;
  76. transform.rotation = Quaternion.AngleAxis (Mathf.Clamp(angle, -6, 53), Vector3.forward);
  77. if (test) {
  78. Instantiate (arrow, arrowpoint.position, Quaternion.AngleAxis (angle, Vector3.forward));
  79.  
  80. test = false;
  81. }
  82. if (ArrowControl.arrow_instantiated)
  83. ArrowControl.arrowrb.transform.position = arrowpoint.position;
  84.  
  85. }
  86.  
  87. }
  88.  
  89.  
  90. void HandleShoot()
  91. {
  92.  
  93. if (isAiming) {
  94. anim.SetBool("Aiming", true);
  95.  
  96. }
  97. if (Input.GetMouseButtonUp (0)) {
  98. shoot = true;
  99.  
  100. isAiming = false;
  101. anim.SetBool ("Aiming", false);
  102.  
  103.  
  104. }
  105.  
  106. }
  107. void AddPower(){
  108.  
  109. if (shoot) {
  110. ArrowControl.arrow_instantiated = false;
  111.  
  112. rotating = true;
  113. Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
  114. Vector3 dir = Input.mousePosition - pos;
  115. angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  116. ArrowControl.arrowrb.AddForce(dir.normalized * ArrowControl.thrust);
  117. ArrowControl.arrowrb.gravityScale = 1f;
  118.  
  119. test = true;
  120. }
  121. }
  122. void HandleController(){
  123. shoot = false;
  124. }
  125. void ArrowFlight(){
  126. float t = ArrowControl.thrust * Mathf.Sin (angle*Mathf.Deg2Rad);
  127. Quaternion temp = ArrowControl.arrowrb.transform.rotation;
  128. if (temp.z > angle && temp.z < 360 - angle) {
  129. temp.z -= 4 * angle * Time.deltaTime / t;
  130. } else {
  131. temp.z -= 2 * angle * Mathf.Sin (Mathf.PI / 2) * Mathf.Sin (Mathf.PI * Time.deltaTime / (2 * t));
  132. }
  133. ArrowControl.arrowrb.transform.rotation = temp;
  134. }
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement