cwisbg

GoatCtrl v2

Oct 30th, 2018
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 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 GoatSpriteObj;
  10. private SpriteRenderer GoatSprite;
  11. public GameObject GoatTrails;
  12. private ParticleSystem.EmissionModule GoatTrailsParticlesModule;
  13. private ParticleSystem GoatTrailsParticles;
  14. private Rigidbody2D GoatRb;
  15. public GameObject AncorObjPrefab;
  16. private Vector2 MouseForce;
  17. private GameObject ShootStartPoint;
  18. private GameObject ShootEndPoint;
  19. private Vector3 MousePos;
  20. private Vector3 ShootForce;
  21. public float ShootForceAmnt = 100f;
  22. public float MaxShootSpeed = 10;
  23. //private float MaxShootSpeedSave;
  24. private OnGround OnTheGround;
  25. public Animator GoatAnimator;
  26. private bool FacingRight;
  27. private float AngleInRadians;
  28. private Vector2 OnGroundNormal;
  29. public GameObject TakeOffParticles;
  30. public LayerMask SceneCollisionLayer;
  31. public bool DoRayCast = true;
  32. public float RayLength = 1;
  33. private Vector3 JumpForce;
  34.  
  35. public void Fliper(float horizontal)
  36. {
  37. if (horizontal > 0 && !FacingRight || horizontal < 0 && FacingRight)
  38. {
  39. FacingRight = !FacingRight;
  40. Vector3 theScale = GoatObj.transform.position;
  41. theScale.x *= -1;
  42. GoatObj.transform.localScale = theScale;
  43. }
  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. {
  54. GoatRb = GoatObj.GetComponent<Rigidbody2D>();
  55. OnTheGround = GoatObj.GetComponent<OnGround>();
  56. GoatSprite = GoatSpriteObj.GetComponent<SpriteRenderer>();
  57. GoatTrailsParticles = GoatTrails.GetComponent<ParticleSystem>();
  58. GoatTrailsParticlesModule = GoatTrailsParticles.emission; //= GoatTrailsParticles.GetComponent<ParticleSystem.EmissionModule>();
  59. //MaxShootSpeedSave = MaxShootSpeed;
  60. }
  61. void Update()
  62. {
  63. Vector2 MousePos = MousePosition();
  64. Vector3 GameObjPos = GoatObj.transform.position;
  65. Vector3 ShootForce = Vector3.zero;
  66. JumpForce = Vector3.zero;
  67. if (Input.GetMouseButtonDown(0))// Create start and end point targets on mouse click
  68. {
  69. if (ShootStartPoint == null) // if start point not created yet, create
  70. {
  71. var HitPointObj = (GameObject)Instantiate(AncorObjPrefab, MousePos, Quaternion.identity);
  72. ShootStartPoint = HitPointObj;
  73. }
  74. if (ShootEndPoint == null) // if end point not created yet, create
  75. {
  76. var HitPointObj = (GameObject)Instantiate(AncorObjPrefab, MousePos, Quaternion.identity);
  77. ShootEndPoint = HitPointObj;
  78. }
  79. }
  80. if (Input.GetMouseButton(0))// Update End position target while mouse is down
  81. {
  82. if (ShootEndPoint != null)
  83. {
  84. ShootEndPoint.transform.position = MousePos;
  85. }
  86. Debug.DrawLine(ShootStartPoint.transform.position, ShootEndPoint.transform.position);
  87. }
  88. if (Input.GetMouseButtonUp(0))// Destroy Targets, clean up icons, shoot goat
  89. {
  90. JumpForce = ShootStartPoint.transform.position - ShootEndPoint.transform.position;
  91. float ShootMag = JumpForce.magnitude;// * ShootForceAmnt;
  92. JumpForce.Normalize();
  93. Vector3 ShootForceMax = Vector3.ClampMagnitude(JumpForce * (ShootMag * ShootForceAmnt), MaxShootSpeed);
  94. ShootForce = GameObjPos + ShootForceMax;
  95. Destroy(ShootStartPoint);
  96. Destroy(ShootEndPoint);
  97.  
  98. if (OnTheGround.IsOnGround)
  99. {
  100. var TakeOffExplosion = (GameObject)Instantiate(
  101. TakeOffParticles,
  102. GameObjPos,
  103. GoatObj.transform.rotation);
  104. Destroy(TakeOffExplosion, 1.0f);
  105. }
  106. //OnTheGround.IsOnGround = false;
  107.  
  108. }
  109.  
  110. if (DoRayCast)
  111. {
  112.  
  113. List<Vector3> RayList = new List<Vector3>();
  114. Vector3 dirDown = Vector3.down;
  115. RayList.Add(dirDown * RayLength);
  116. Vector3 dirForward = Vector3.right + Vector3.down;
  117. RayList.Add(dirForward.normalized * RayLength);
  118. Vector3 dirBack = Vector3.left + Vector3.down;
  119. RayList.Add(dirBack.normalized * RayLength);
  120. RayList.Add(Vector3.left * RayLength);
  121. RayList.Add(Vector3.right * RayLength);
  122. for (int i = 0; i < RayList.Count; i++)
  123. {
  124. RaycastHit2D hit = Physics2D.Raycast(GoatFeet.transform.position, RayList[i], 1 * RayLength, SceneCollisionLayer);
  125. Debug.DrawLine(GoatFeet.transform.position, GoatFeet.transform.position + RayList[i], Color.blue);
  126. if (hit.collider != null)
  127. {
  128. //Debug.DrawLine(GoatFeet.transform.position, new Vector2(GoatFeet.transform.position.x, GoatFeet.transform.position.y) + dirTest * 0.3f, Color.red);
  129. Debug.DrawLine(GoatFeet.transform.position, hit.point, Color.red);
  130. OnTheGround.IsOnGround = true;
  131. }
  132. }
  133. }
  134.  
  135. if (OnTheGround.IsOnGround)
  136. {
  137. GoatAnimator.SetBool("Jump", false);
  138. GoatRb.AddForce(ShootForce);
  139. float AngleInRadians = Mathf.Atan2(JumpForce.y, JumpForce.x);
  140. AngleInRadians *= -1;
  141. OnGroundNormal = OnTheGround.GroundNormal;
  142. float AngleInRadians2 = Mathf.Atan2(OnGroundNormal.y, OnGroundNormal.x);
  143. float AngleInDegrees2 = AngleInRadians2 * Mathf.Rad2Deg;
  144. GoatSprite.transform.rotation = Quaternion.Euler(0.0f, 0.0f, AngleInDegrees2 - 90);
  145. GoatTrailsParticlesModule.enabled = false;
  146. }
  147. else
  148. {
  149. GoatAnimator.SetBool("Jump", true);
  150. GoatTrailsParticlesModule.enabled = true;
  151. if (GoatRb.velocity.x >= .1)
  152. {
  153. GoatSprite.flipX = false;
  154. }
  155. if (GoatRb.velocity.x <= .1)
  156. {
  157. GoatSprite.flipX = true;
  158. }
  159. }
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment