cwisbg

Playerv_3.1

Mar 13th, 2019
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.22 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5.  
  6. public class Player_v3 : MonoBehaviour {
  7. public float Health = 1.0f;
  8. public float HealthMax = 10;
  9. public int LifeCount = 3;
  10. public float HealthTrack = 0f;
  11. public bool IsDoHurt = false;
  12. public float HealthDecaySpeed = 2f;
  13. public float MoveSpeed = 40f;
  14. public float RotationSpeed = 40;
  15. public float GravityForce = 5;
  16. public float JumpSpeed = 300;
  17. public GameObject LineObject;
  18. //public GameObject PlanetListParent;
  19. public GameObject TakeOffEffect;
  20. public GameObject ThrustTrails;
  21. public SpriteRenderer ObjectSprite;
  22. public GameObject DeathPrefab;
  23. public float LineRenderOffset = 1f;
  24. public LayerMask groundLayer;
  25. public float RayLength = .5f;
  26. public float SurfaceSearchOffset = 2;
  27. public bool OnPlanet = false;
  28. private LineRenderer LineRenderObj;
  29. private GameObject ClickedPlanet;
  30. private GameObject Planet = null;
  31. private Planets PlanetClosest;
  32. private Vector3 PlayerPosition;
  33. private Vector3 PlanetPosition;
  34. private Vector3 PlanetDirection;
  35. private Vector3 OnPlanetPos;
  36. private Vector3 OnPLanetHitPos;
  37. private Vector3 OnPlanetDir;
  38. private Vector3 MouseDirection;
  39. private float PlayerPlanetAngle = 0.0f;
  40. private List<Planets> PlanetList = new List<Planets>();
  41. private Rigidbody2D playerRigidBody;
  42. private Vector3 LineStart = new Vector3(1000, 0, 0);
  43. private bool MoveVertical = false;
  44. public GameObject TestObj;
  45. private CircleCollider2D PlanetCollider = null;
  46. private PolygonCollider2D PlanetColliderP = null;
  47. private BoxCollider2D PlanetColliderBC = null;
  48. private float deadTimer = 2f;
  49. private ParticleSystem.EmissionModule TrailsParticlesModule;
  50. private ParticleSystem TrailsParticles;
  51. private bool DoDeath = true;
  52. public bool IsAlive = true;
  53. private float MouseDownTime = 0.0f;
  54. public SceneLoad_Restart SceneRestarter;
  55. public bool MouseInvert = false;
  56. private int MouseInvertSave;
  57. private Planets Saveplanets = new Planets();
  58. public float OffPlanetForce = 4f;
  59. //private Vector3 PlanetDirection;
  60. private Vector3 PlanetDirectionNorm;
  61. //public GameObject FollowCamera;
  62. //private Vector3 CameraTargetPosition;
  63. //public Vector3 CameraFollowOffset;
  64. class Planets
  65. {
  66. public GameObject PlanetGameObj;
  67. public bool DoSearch = true;
  68. public Vector3 RayHitPosition;
  69. }
  70. GameObject MousedObject(Vector3 mousePos)
  71. {
  72. GameObject HitObj = null;
  73. Vector2 MousePos2D = new Vector2(mousePos.x, mousePos.y);
  74. RaycastHit2D hit = Physics2D.Raycast(MousePos2D, Vector2.zero, groundLayer);
  75. if (hit)
  76. {
  77. HitObj = hit.transform.gameObject;
  78. }
  79. return HitObj;
  80. }
  81. Vector3 MousePosition()
  82. {
  83. Vector3 MousePos = Input.mousePosition;
  84. MousePos = Camera.main.ScreenToWorldPoint(MousePos);
  85. MousePos = new Vector3(MousePos.x, MousePos.y, 0f);
  86. return MousePos;
  87. }
  88.  
  89. Planets GetClosest(List<Planets> PlanetList, Vector3 CloseToPos)
  90. {
  91. Planets bestTarget = null;
  92. float closestDistanceSqr = Mathf.Infinity;
  93. int PlanetListLen = PlanetList.Count;
  94. Vector3 HitPointV3 = Vector3.zero;
  95. for (int i = 0; i < PlanetListLen; i++)
  96. {
  97. if (PlanetList[i].DoSearch)
  98. {
  99. Vector3 directionToTarget = PlanetList[i].PlanetGameObj.transform.position - CloseToPos;
  100. Vector3 directionToTargetN = directionToTarget;
  101. directionToTargetN.Normalize();
  102. RaycastHit2D hitSearch = Physics2D.Raycast(transform.position, directionToTargetN, 10, groundLayer);
  103. //Debug.DrawLine(transform.position, transform.position + directionToTargetN * 10, Color.green);
  104. if (hitSearch.collider != null)
  105. {
  106. Debug.DrawLine(transform.position, hitSearch.point, Color.red);
  107. HitPointV3.x = hitSearch.point.x;
  108. HitPointV3.y = hitSearch.point.y;
  109. directionToTarget = HitPointV3 - CloseToPos;
  110. PlanetList[i].RayHitPosition = HitPointV3;
  111. }
  112.  
  113. float dSqrToTarget = directionToTarget.sqrMagnitude;
  114. if (dSqrToTarget < closestDistanceSqr)
  115. {
  116. closestDistanceSqr = dSqrToTarget;
  117. bestTarget = PlanetList[i];
  118. }
  119. }
  120. else
  121. {
  122. //PlanetList[i].DoSearch = true;
  123. }
  124. }
  125. return bestTarget;
  126. }
  127.  
  128. Vector3 GetClosestRaycast()
  129. {
  130. Vector3 bestTarget = Vector3.zero;
  131.  
  132. List<Vector3> RayList = new List<Vector3>();
  133. Vector3 dirUp = transform.up;
  134. RayList.Add(dirUp * RayLength);
  135. Vector3 dirLeft = transform.up - transform.right;
  136. dirLeft.Normalize();
  137. RayList.Add(dirLeft * RayLength);
  138. //Vector3 dirDown = Vector3.down;
  139. //RayList.Add(dirDown * RayLength);
  140. //Vector3 dirForward = Vector3.right + Vector3.down;
  141. //RayList.Add(dirForward.normalized * RayLength);
  142. //Vector3 dirBack = Vector3.left + Vector3.down;
  143. //RayList.Add(dirBack.normalized * RayLength);
  144. //RayList.Add(Vector3.left * RayLength);
  145. //RayList.Add(Vector3.right * RayLength);
  146.  
  147. for (int i = 0; i < RayList.Count; i++)
  148. {
  149. RaycastHit2D hit = Physics2D.Raycast(transform.position, RayList[i], 1 * RayLength);
  150. Debug.DrawLine(transform.position, transform.position + RayList[i], Color.blue);
  151. if (hit.collider != null)
  152. {
  153. Debug.DrawLine(transform.position, hit.point, Color.red);
  154. bestTarget = hit.point;
  155. }
  156. }
  157. return bestTarget;
  158. }
  159.  
  160.  
  161. Vector3 PLanetLaunchLine(Vector3 StartPos, Vector3 MousePos)
  162. {
  163. Vector3 SavePos = StartPos;
  164. Vector3 SaveDir = Vector3.zero;
  165. Vector3 NewPosDir = Vector3.zero;
  166. StartPos.z = LineRenderOffset;
  167. LineRenderObj.SetPosition(0, StartPos);
  168.  
  169. if (MouseInvert)
  170. {
  171. NewPosDir = MousePos - SavePos;
  172. }
  173. else
  174. {
  175. NewPosDir = SavePos - MousePos;
  176. }
  177.  
  178. float VectorMag = Vector3.Magnitude(NewPosDir);
  179. NewPosDir.Normalize();
  180. NewPosDir *= Mathf.Clamp(VectorMag,0.1f, 2f);
  181. SaveDir = NewPosDir;
  182. NewPosDir += SavePos;
  183. NewPosDir.z = LineRenderOffset;
  184. LineRenderObj.SetPosition(1, NewPosDir);
  185. return SaveDir;
  186. }
  187. Vector3 PST(Vector3 Planet, Vector3 Direction) // Planet Surface Tracker
  188. {
  189. Vector3 SurfacePoint = Vector3.zero;
  190. return SurfacePoint;
  191. }
  192.  
  193.  
  194. void Start () {
  195. LifeCount = PlayerPrefs.GetInt("Player_Lives");
  196. //MouseInvertSave = PlayerPrefs.GetInt("Player_ControlInvert");
  197. //if (MouseInvertSave == 0)
  198. //{
  199. // MouseInvert = true;
  200. //}
  201. //if (MouseInvertSave == 1)
  202. //{
  203. // MouseInvert = false;
  204. //}
  205. playerRigidBody = transform.GetComponent<Rigidbody2D>();
  206. LineRenderObj = LineObject.GetComponent<LineRenderer>();
  207. TrailsParticles = ThrustTrails.GetComponent<ParticleSystem>();
  208. TrailsParticlesModule = TrailsParticles.emission;
  209. //foreach (Transform potentialTarget in PlanetListParent.transform)
  210.  
  211. foreach (GameObject child in GameObject.FindGameObjectsWithTag("Planets"))
  212. {
  213. Planets potentialTarget = new Planets();
  214. potentialTarget.PlanetGameObj = child;
  215. PlanetList.Add(potentialTarget);
  216. }
  217.  
  218. //PlanetClosest = GetClosest(PlanetList, PlayerPosition);
  219. //if (PlanetClosest != null)
  220. //{
  221. // PlanetClosest.PlanetGameObj.layer = 8;
  222. // Saveplanets = PlanetClosest;
  223. //}
  224. }
  225.  
  226.  
  227. void Update() {
  228. if (Health > 0)
  229. {
  230. if (HealthTrack < Health)
  231. {
  232. //IsDoHurt = true;
  233. }
  234. PlayerPosition = transform.position;
  235. //if (PlanetClosest != null)
  236. //{
  237. // //PlanetPosition = PlanetClosest.PlanetGameObj.transform.position;
  238. // PlanetPosition = PlanetClosest.RayHitPosition;
  239. // OnPLanetHitPos = PlanetPosition;
  240. //}
  241. //CameraTargetPosition = PlayerPosition + CameraFollowOffset;
  242. if (Input.GetMouseButtonUp(0) && IsAlive == true && OnPlanet == true)
  243. {
  244. if (MouseDownTime >= 0.3f)
  245. {
  246. MoveVertical = true;
  247. }
  248. MouseDownTime = 0;
  249. }
  250.  
  251. //Debug.Log(MouseDownTime);
  252.  
  253.  
  254. LineRenderObj.SetPosition(0, LineStart);
  255. LineRenderObj.SetPosition(1, LineStart);
  256.  
  257. if (OnPlanet)
  258. {
  259.  
  260. TrailsParticlesModule.enabled = false;
  261. Vector3 StartDir = PlayerPosition - PlanetPosition;
  262.  
  263. StartDir.Normalize();
  264. //Debug.DrawRay(PlanetPosition, StartDir * 3, Color.cyan);
  265.  
  266. Vector3 EndDir = (PlanetPosition + MouseDirection) - PlanetPosition;
  267. //Vector3 EndDir = (OnPLanetHitPos + MouseDirection) - PlanetPosition;
  268. EndDir.Normalize();
  269. //Debug.DrawRay(PlanetPosition, EndDir * 3, Color.red);
  270. Vector3 MiddleDir = (StartDir * 0.8f) + (EndDir * 0.2f);
  271. MiddleDir.Normalize();
  272. Vector3 ToSurface = MiddleDir;
  273. float dist = Vector3.Distance(PlayerPosition, PlanetPosition);
  274. //MiddleDir *= (PlanetClosest.transform.localScale[0]*0.5f) * SurfaceSearchOffset;
  275. MiddleDir *= (dist + 0.1f);
  276. MiddleDir += PlanetPosition;
  277. ToSurface *= -1;
  278. RaycastHit2D SurfaceHit = Physics2D.Raycast(MiddleDir, ToSurface, RayLength * 1.5f, groundLayer);
  279. //Debug.DrawLine(MiddleDir, MiddleDir + (ToSurface * (RayLength * 1.5f)), Color.red);
  280.  
  281. if (SurfaceHit.collider != null)
  282. {
  283. Debug.DrawLine(SurfaceHit.point, SurfaceHit.point + SurfaceHit.normal, Color.blue);
  284. OnPLanetHitPos = SurfaceHit.point;
  285. Vector3 NewPos = Vector3.Lerp(transform.position, OnPLanetHitPos, MoveSpeed * Time.deltaTime);
  286. transform.position = NewPos;
  287. OnPlanetDir = SurfaceHit.normal;
  288. }
  289. else
  290. {
  291. OnPlanet = false;
  292. }
  293. OnPlanetDir += (MouseDirection * 0.5f);
  294. PlanetDirection = OnPlanetPos - transform.position;
  295. Vector3 NewDirGravity = Vector3.Lerp(transform.up, OnPlanetDir, RotationSpeed * Time.deltaTime);
  296. transform.up = NewDirGravity;
  297. if (MoveVertical == true)
  298.  
  299. {
  300.  
  301. PlanetClosest.DoSearch = false;
  302. SpriteRenderer planetSprite = PlanetClosest.PlanetGameObj.GetComponent<SpriteRenderer>();
  303. planetSprite.enabled = false;
  304. Saveplanets = PlanetClosest;
  305. PlanetClosest.PlanetGameObj.layer = 0;
  306.  
  307. OnPlanet = false;
  308. MoveVertical = false;
  309. //PlanetCollider = PlanetClosest.PlanetGameObj.GetComponent<CircleCollider2D>();
  310.  
  311.  
  312. var Explosion = (GameObject)Instantiate(TakeOffEffect, PlayerPosition, transform.rotation);
  313. Destroy(Explosion, 0.5f);
  314. MouseDirection.Normalize();
  315. playerRigidBody.AddForce(MouseDirection * JumpSpeed);
  316. transform.up = MouseDirection;
  317. }
  318. if (Health < HealthMax)
  319. {
  320. //Health += HealthDecaySpeed * Time.deltaTime;
  321.  
  322. //IsDoHurt = false;
  323. }
  324.  
  325. }
  326. // Cast Ray from player in the direction of closest planet
  327. else
  328. {
  329. TrailsParticlesModule.enabled = true;
  330. ObjectSprite.color = Color.Lerp(new Color(1, 0, 0), new Color(1, 1, 1), Health);
  331.  
  332. PlanetClosest = GetClosest(PlanetList, PlayerPosition);
  333. Debug.DrawLine(transform.position, PlanetClosest.RayHitPosition, Color.green);
  334. if (PlanetClosest != null)
  335. {
  336. PlanetPosition = PlanetClosest.PlanetGameObj.transform.position;
  337. //PlanetPosition = PlanetClosest.RayHitPosition;
  338. //
  339. //PlanetClosest.DoSearch = false;
  340.  
  341. }
  342.  
  343. PlanetDirection = PlanetPosition - PlayerPosition;
  344. PlanetDirectionNorm = PlanetDirection;
  345. PlanetDirectionNorm.Normalize();
  346.  
  347. RaycastHit2D hit = Physics2D.Raycast(PlayerPosition, PlanetDirectionNorm, RayLength, groundLayer);
  348. Debug.DrawLine(PlayerPosition, PlayerPosition + PlanetDirectionNorm * RayLength, Color.blue);
  349.  
  350. if (hit.collider != null)
  351. {
  352. if (Saveplanets.PlanetGameObj != null)
  353. {
  354. Saveplanets.DoSearch = true;
  355. SpriteRenderer planetSprite = Saveplanets.PlanetGameObj.GetComponent<SpriteRenderer>();
  356. planetSprite.enabled = true;
  357. Saveplanets.PlanetGameObj.layer = 8;
  358. }
  359.  
  360. //Debug.DrawLine(PlayerPosition, hit.point, Color.red);
  361. OnPlanet = true;
  362. playerRigidBody.velocity = Vector2.zero;
  363. playerRigidBody.angularVelocity = 0.0f;
  364. OnPlanetPos = hit.point;
  365. OnPlanetDir = hit.normal;
  366. //if (PlanetCollider != null)
  367. //{
  368. // PlanetCollider.enabled = true;
  369. //}
  370. //if (PlanetColliderP != null)
  371. //{
  372. // PlanetColliderP.enabled = true;
  373. //}
  374.  
  375. }
  376.  
  377. else
  378. {
  379. OnPlanet = false;
  380. //PlanetClosest.layer = 0;
  381. }
  382. Health -= HealthDecaySpeed * Time.deltaTime;
  383. }
  384.  
  385.  
  386. if (Input.GetMouseButton(0))
  387. {
  388. Vector3 MousePos = MousePosition();
  389. if (IsAlive)
  390. {
  391.  
  392. MouseDirection = PLanetLaunchLine(transform.position, MousePos);
  393. //MouseDirection = PLanetLaunchLine(MousePos,transform.position);
  394.  
  395.  
  396.  
  397. //MouseDirection.Normalize();
  398. }
  399.  
  400.  
  401. if (!OnPlanet)
  402. {
  403. MouseDirection.Normalize();
  404. playerRigidBody.AddForce(MouseDirection * OffPlanetForce);
  405. Vector3 OffPlanetDir = Vector3.Lerp(transform.up, MouseDirection, 5f * Time.deltaTime);
  406. transform.up = OffPlanetDir;
  407. }
  408. else
  409. {
  410. //CameraTargetPosition += MouseDirection * (10f*Time.deltaTime);
  411. }
  412. MouseDownTime += 1 * Time.deltaTime;
  413. }
  414.  
  415. //Vector3 NewCamPos = Vector3.Slerp(FollowCamera.transform.position, CameraTargetPosition, 1f);
  416. //FollowCamera.transform.position = NewCamPos; // Set camera positition
  417. }
  418. if (Health <= 0.0f)
  419. {
  420.  
  421. ObjectSprite.enabled = false;
  422. if (DoDeath)
  423. {
  424. Debug.Log("Is Dead");
  425.  
  426. LifeCount -= 1;
  427. PlayerPrefs.SetInt("Player_Lives", LifeCount);
  428. PlayerPrefs.Save();
  429. Debug.Log("Killed");
  430.  
  431. var DeathExplosion = (GameObject)Instantiate(DeathPrefab, PlayerPosition, transform.rotation);
  432. //DeathExplosion.transform.parent = transform;
  433. //Destroy(LandingExplosion, 1.5f);
  434. playerRigidBody.velocity = Vector2.zero;
  435. playerRigidBody.angularVelocity = 0.0f;
  436. DoDeath = false;
  437. IsAlive = false;
  438. LineRenderObj.SetPosition(0, LineStart);
  439. LineRenderObj.SetPosition(1, LineStart);
  440. }
  441.  
  442. TrailsParticlesModule.enabled = false;
  443. if (deadTimer <= 0.0f)
  444. {
  445. SceneRestarter.LoadScene();
  446. //Scene scene = SceneManager.GetActiveScene();
  447. //SceneManager.LoadScene(scene.buildIndex);
  448. }
  449. deadTimer -= 1f * Time.deltaTime;
  450. }
  451. HealthTrack = Health;
  452. }
  453. }
Advertisement
Add Comment
Please, Sign In to add comment