cwisbg

GoatCtlr

Oct 2nd, 2018
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class GoatCtrl_v1 : MonoBehaviour {
  6.  
  7. public GameObject GoatObj;
  8. public GameObject GoatFeet;
  9. public GameObject HitPointObj;
  10. private Rigidbody2D GoatRb;
  11. private Collider2D GoatCollider;
  12. public GameObject AimTargetObj;
  13. private Vector2 AimTargetPos;
  14. public GameObject AncorObjPrefab;
  15. private Vector2 MouseForce;
  16. public Camera RayCam;
  17.  
  18. private GameObject ShootStartPoint;
  19. private GameObject ShootEndPoint;
  20. private Vector3 MousePos;
  21.  
  22.  
  23. private Vector3 ShootForce;
  24. private Vector3 GoatDirection;
  25. private Vector3 lastPosition;
  26. public float speed = 5f;
  27. public float ShootForceAmnt = 100f;
  28. public float MaxShootSpeed = 10;
  29.  
  30. public float SearchCollisionDistance = 5f;
  31. public LayerMask SceneCollisionLayer;
  32. private OnGround OnTheGround;
  33. Vector2 RaySearcher(GameObject Goat)
  34. {
  35. Vector2 rayForce = Vector2.zero;
  36. RaycastHit2D hit = Physics2D.Raycast(Goat.transform.position, Goat.transform.up, SearchCollisionDistance, SceneCollisionLayer);
  37. Debug.DrawLine(Goat.transform.position, Goat.transform.position-Goat.transform.up, Color.red);
  38. if (hit.collider != null)
  39. {
  40. rayForce = hit.normal;
  41. Debug.DrawLine(hit.point, hit.point + hit.normal, Color.red);
  42. }
  43. return rayForce;
  44. }
  45. Vector3 MousePosition()
  46. {
  47. Vector3 MousePos = Input.mousePosition;
  48. MousePos = Camera.main.ScreenToWorldPoint(MousePos);
  49. MousePos = new Vector3(MousePos.x, MousePos.y, 0f);
  50. return MousePos;
  51. }
  52. void Start () {
  53. GoatRb = GoatObj.GetComponent<Rigidbody2D>();
  54. GoatCollider = GoatObj.GetComponent<Collider2D>();
  55. OnTheGround = GoatObj.GetComponent<OnGround>();
  56. //Debug.Log(GoatCollider);
  57. }
  58. void OnCollisionEnter2D(Collision2D GoatCollider)
  59. {
  60. //Debug.Log(GoatCollider.transform);
  61. if (GoatCollider.gameObject.name == "Ground")
  62. {
  63. Destroy(GoatCollider.gameObject);
  64. }
  65. }
  66. // Update is called once per frame
  67. void Update() {
  68.  
  69. //GoatRb.AddForce(Vector2.right * speed);
  70.  
  71. Vector2 MousePos = MousePosition();
  72. Vector3 GameObjPos = GoatObj.transform.position;
  73. Vector3 ShootForce = Vector3.zero;
  74.  
  75.  
  76. if (Input.GetMouseButtonDown(0))// Create start and end point targets on mouse click
  77. {
  78. if (ShootStartPoint == null) // if start point not created yet, create
  79. {
  80. var HitPointObj = (GameObject)Instantiate(AncorObjPrefab, MousePos, Quaternion.identity);
  81. ShootStartPoint = HitPointObj;
  82. }
  83. if (ShootEndPoint == null) // if end point not created yet, create
  84. {
  85. var HitPointObj = (GameObject)Instantiate(AncorObjPrefab, MousePos, Quaternion.identity);
  86. ShootEndPoint = HitPointObj;
  87. }
  88. }
  89. if (Input.GetMouseButton(0))// Update End position target while mouse is down
  90. {
  91. if (ShootEndPoint != null)
  92. {
  93. ShootEndPoint.transform.position = MousePos;
  94. }
  95. Debug.DrawLine(ShootStartPoint.transform.position, ShootEndPoint.transform.position);
  96. }
  97. /*
  98. GoatDirection = (GameObjPos - lastPosition);
  99. //GoatDirection.Normalize();
  100. GoatDirection += GameObjPos;
  101.  
  102. Vector2 GoatPos = GoatObj.transform.position;
  103. Vector2 AimTargetPos = GoatDirection;// AimTargetObj.transform.position;
  104. Vector2 diff = AimTargetPos - GoatPos;
  105. float AngleInRadians = Mathf.Atan2(diff.y, diff.x);
  106. float AngleInDegrees = AngleInRadians * Mathf.Rad2Deg ;
  107. GoatObj.transform.rotation = Quaternion.Euler(0.0f, 0.0f, AngleInDegrees);
  108. Vector2 dir = GoatRb.velocity;
  109. float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  110. GoatObj.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
  111. Vector2 dirN = dir.normalized;
  112. //Vector2 RayHit = RaySearcher(GoatFeet);
  113. Quaternion CurrentDirection = Quaternion.Euler(GoatObj.transform.rotation.x, GoatObj.transform.rotation.y, GoatObj.transform.rotation.z);// Quaternion.identity;
  114. */
  115.  
  116.  
  117. if (Input.GetMouseButtonUp(0))// Destroy Targets, clean up icons, shoot goat
  118. {
  119. //GoatRb.simulated = true;
  120.  
  121. Vector3 JumpForce = ShootStartPoint.transform.position - ShootEndPoint.transform.position;
  122. float ShootMag = JumpForce.magnitude;// * ShootForceAmnt;
  123. Vector3 ShootForceMax = Vector3.ClampMagnitude(JumpForce * ShootForceAmnt, MaxShootSpeed);
  124. ShootForce = ShootForceMax - GameObjPos;
  125. //Debug.Log(ShootForce);
  126. //ShootForce -= GameObjPos;
  127. Destroy(ShootStartPoint);
  128. Destroy(ShootEndPoint);
  129. }
  130. /*
  131. bool OnGround = false;
  132. Vector2 dirTest = Vector2.down;
  133. Vector2 RayHitPosition = Vector2.zero;
  134. RaycastHit2D hit = Physics2D.Raycast(GameObjPos, dirTest, 3f, SceneCollisionLayer);
  135. Debug.DrawLine(GoatFeet.transform.position, new Vector2(GoatFeet.transform.position.x, GoatFeet.transform.position.y) + dirTest, Color.yellow);
  136.  
  137. if (hit.collider != null)
  138. {
  139. //float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  140. //GoatObj.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
  141. HitPointObj.transform.position = hit.point;
  142. //GoatRb.AddForce(GameObjPos - ShootForce);
  143. OnGround = true;
  144.  
  145. // Rotation stuff
  146. //float angle = Mathf.Atan2(hit.normal.y, hit.normal.x) * Mathf.Rad2Deg;
  147. //GoatObj.transform.rotation = Quaternion.Lerp(CurrentDirection, Quaternion.AngleAxis(angle, Vector3.forward), 10.0f*Time.deltaTime);
  148. //Debug.DrawLine(GoatFeet.transform.position, hit.point + hit.normal, Color.blue);
  149. }
  150. */
  151. //Debug.Log(OnTheGround.IsOnGround);
  152. if (OnTheGround.IsOnGround)
  153. {
  154.  
  155. GoatRb.AddForce(ShootForce);
  156.  
  157.  
  158. }
  159.  
  160. lastPosition = GoatObj.transform.position;
  161. }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment