Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class Player_v3 : MonoBehaviour {
- public float Health = 1.0f;
- public float HealthDecaySpeed = 2f;
- public float MoveSpeed = 0.1f;
- public float GravityForce = 5;
- public float JumpSpeed = 300;
- public GameObject LineObject;
- public GameObject PlanetListParent;
- public GameObject TakeOffEffect;
- public GameObject ThrustTrails;
- public SpriteRenderer ObjectSprite;
- public GameObject DeathPrefab;
- public float LineRenderOffset = 1f;
- public LayerMask groundLayer;
- public float RayLength = .5f;
- public float SurfaceSearchOffset = 2;
- public bool OnPlanet = false;
- private LineRenderer LineRenderObj;
- private GameObject ClickedPlanet;
- private GameObject Planet = null;
- private GameObject PlanetClosest;
- private Vector3 PlayerPosition;
- private Vector3 PlanetPosition;
- private Vector3 PlanetDirection;
- private Vector3 OnPlanetPos;
- private Vector3 OnPlanetDir;
- private Vector3 MouseDirection;
- private float PlayerPlanetAngle = 0.0f;
- private List<Transform> PlanetList = new List<Transform>();
- private Rigidbody2D playerRigidBody;
- private Vector3 LineStart = new Vector3(1000, 0, 0);
- private bool MoveVertical = false;
- public GameObject TestObj;
- private CircleCollider2D PlanetCollider = null;
- private PolygonCollider2D PlanetColliderP = null;
- private float deadTimer = 2f;
- private ParticleSystem.EmissionModule TrailsParticlesModule;
- private ParticleSystem TrailsParticles;
- private bool DoDeath = true;
- private bool IsAlive = true;
- public GameObject FollowCamera;
- private Vector3 CameraTargetPosition;
- public Vector3 CameraFollowOffset;
- GameObject MousedObject(Vector3 mousePos)
- {
- GameObject HitObj = null;
- Vector2 MousePos2D = new Vector2(mousePos.x, mousePos.y);
- RaycastHit2D hit = Physics2D.Raycast(MousePos2D, Vector2.zero, groundLayer);
- if (hit)
- {
- HitObj = hit.transform.gameObject;
- }
- return HitObj;
- }
- Vector3 MousePosition()
- {
- Vector3 MousePos = Input.mousePosition;
- MousePos = Camera.main.ScreenToWorldPoint(MousePos);
- MousePos = new Vector3(MousePos.x, MousePos.y, 0f);
- return MousePos;
- }
- GameObject GetClosest(List<Transform> PlanetList, Vector3 CloseToPos)
- {
- GameObject bestTarget = null;
- float closestDistanceSqr = Mathf.Infinity;
- int PlanetListLen = PlanetList.Count;
- for (int i = 0; i < PlanetListLen; i++)
- {
- Vector3 directionToTarget = PlanetList[i].transform.position - CloseToPos;
- float dSqrToTarget = directionToTarget.sqrMagnitude;
- if (dSqrToTarget < closestDistanceSqr)
- {
- closestDistanceSqr = dSqrToTarget;
- bestTarget = PlanetList[i].gameObject;
- }
- }
- return bestTarget;
- }
- Vector3 PLanetLaunchLine(Vector3 StartPos, Vector3 MousePos)
- {
- Vector3 SavePos = StartPos;
- Vector3 SaveDir = Vector3.zero;
- StartPos.z = LineRenderOffset;
- LineRenderObj.SetPosition(0, StartPos);
- Vector3 NewPosDir = SavePos - MousePos;
- float VectorMag = Vector3.Magnitude(NewPosDir);
- NewPosDir.Normalize();
- NewPosDir *=( VectorMag*0.5f);
- SaveDir = NewPosDir;
- NewPosDir += SavePos;
- NewPosDir.z = LineRenderOffset;
- LineRenderObj.SetPosition(1, NewPosDir);
- return SaveDir;
- }
- Vector3 PST(Vector3 Planet, Vector3 Direction) // Planet Surface Tracker
- {
- Vector3 SurfacePoint = Vector3.zero;
- return SurfacePoint;
- }
- void Start () {
- playerRigidBody = transform.GetComponent<Rigidbody2D>();
- LineRenderObj = LineObject.GetComponent<LineRenderer>();
- TrailsParticles = ThrustTrails.GetComponent<ParticleSystem>();
- TrailsParticlesModule = TrailsParticles.emission;
- //foreach (Transform potentialTarget in PlanetListParent.transform)
- foreach (GameObject child in GameObject.FindGameObjectsWithTag("Planets"))
- {
- Transform potentialTarget = child.transform;
- PlanetList.Add(potentialTarget);
- }
- PlanetClosest = GetClosest(PlanetList, PlayerPosition);
- }
- void Update() {
- PlayerPosition = transform.position;
- PlanetPosition = PlanetClosest.transform.position;
- CameraTargetPosition = PlayerPosition + CameraFollowOffset;
- if (Input.GetMouseButtonUp(0) && IsAlive == true)
- {
- MoveVertical = true;
- }
- Vector3 PlanetDirection = PlanetPosition - PlayerPosition;
- Vector3 PlanetDirectionNorm = PlanetDirection;
- PlanetDirectionNorm.Normalize();
- LineRenderObj.SetPosition(0, LineStart);
- LineRenderObj.SetPosition(1, LineStart);
- if (OnPlanet)
- {
- TrailsParticlesModule.enabled = false;
- Vector3 StartDir = PlayerPosition - PlanetPosition;
- StartDir.Normalize();
- //Debug.DrawRay(PlanetPosition, StartDir * 3, Color.cyan);
- Vector3 EndDir = (PlanetPosition + MouseDirection) - PlanetPosition;
- EndDir.Normalize();
- //Debug.DrawRay(PlanetPosition, EndDir * 3, Color.red);
- Vector3 MiddleDir = (StartDir * 0.8f) + (EndDir * 0.2f);
- MiddleDir.Normalize();
- Vector3 ToSurface = MiddleDir;
- float dist = Vector3.Distance(PlayerPosition, PlanetPosition);
- //MiddleDir *= (PlanetClosest.transform.localScale[0]*0.5f) * SurfaceSearchOffset;
- MiddleDir *= (dist+0.1f);
- MiddleDir += PlanetPosition;
- ToSurface *= -1;
- RaycastHit2D SurfaceHit = Physics2D.Raycast(MiddleDir, ToSurface, RayLength*1.5f, groundLayer);
- Debug.DrawLine(MiddleDir, MiddleDir+(ToSurface * (RayLength * 1.5f)), Color.red);
- if (SurfaceHit.collider != null)
- {
- Debug.DrawLine(MiddleDir, SurfaceHit.point, Color.blue);
- Vector3 NewPos = Vector3.Lerp(transform.position, SurfaceHit.point, MoveSpeed * Time.deltaTime);
- transform.position = NewPos;
- OnPlanetDir = SurfaceHit.normal;
- }
- PlanetDirection = OnPlanetPos - transform.position;
- Vector3 NewDirGravity = Vector3.Lerp(transform.up, OnPlanetDir, 40.0f * Time.deltaTime);
- transform.up = NewDirGravity;
- if (MoveVertical == true)
- {
- //playerRigidBody.AddForce(transform.up * JumpSpeed);
- OnPlanet = false;
- MoveVertical = false;
- PlanetCollider = PlanetClosest.GetComponent<CircleCollider2D>();
- if (PlanetCollider != null)
- {
- PlanetCollider.enabled = false;
- }
- PlanetColliderP = PlanetClosest.GetComponent<PolygonCollider2D>();
- if (PlanetColliderP != null)
- {
- PlanetColliderP.enabled = false;
- }
- var Explosion = (GameObject)Instantiate(TakeOffEffect, PlayerPosition, transform.rotation);
- Destroy(Explosion, 0.5f);
- playerRigidBody.AddForce(MouseDirection * JumpSpeed);
- transform.up = MouseDirection;
- }
- if (Health < 1)
- {
- Health += HealthDecaySpeed * Time.deltaTime;
- }
- }
- // Cast Ray from player in the direction of closest planet
- else
- {
- PlanetClosest = GetClosest(PlanetList, PlayerPosition);
- PlanetPosition = PlanetClosest.transform.position;
- TrailsParticlesModule.enabled = true;
- ObjectSprite.color = Color.Lerp(new Color(1, 0, 0), new Color(1, 1, 1), Health);
- RaycastHit2D hit = Physics2D.Raycast(PlayerPosition, PlanetDirectionNorm, RayLength, groundLayer);
- //Debug.DrawLine(PlayerPosition, PlayerPosition + PlanetDirectionNorm * RayLength, Color.red);
- if (hit.collider != null)
- {
- //Debug.DrawLine(PlayerPosition, hit.point, Color.red);
- OnPlanet = true;
- playerRigidBody.velocity = Vector2.zero;
- playerRigidBody.angularVelocity = 0.0f;
- OnPlanetPos = hit.point;
- OnPlanetDir = hit.normal;
- if (PlanetCollider != null)
- {
- PlanetCollider.enabled = true;
- }
- if (PlanetColliderP != null)
- {
- PlanetColliderP.enabled = true;
- }
- // Hit planet effect
- }
- else
- {
- OnPlanet = false;
- }
- Health -= HealthDecaySpeed * Time.deltaTime;
- }
- if (Health <= 0.0f)
- {
- ObjectSprite.enabled = false;
- if (DoDeath)
- {
- var DeathExplosion = (GameObject)Instantiate(DeathPrefab, PlayerPosition, transform.rotation);
- //DeathExplosion.transform.parent = transform;
- //Destroy(LandingExplosion, 1.5f);
- playerRigidBody.velocity = Vector2.zero;
- playerRigidBody.angularVelocity = 0.0f;
- DoDeath = false;
- IsAlive = false;
- }
- TrailsParticlesModule.enabled = false;
- if (deadTimer <= 0.0f) {
- Scene scene = SceneManager.GetActiveScene();
- SceneManager.LoadScene(scene.buildIndex);
- }
- deadTimer -= 1f * Time.deltaTime;
- }
- if (Input.GetMouseButton(0))
- {
- Vector3 MousePos = MousePosition();
- if (IsAlive)
- {
- MouseDirection = PLanetLaunchLine(transform.position, MousePos);
- MouseDirection.Normalize();
- }
- if (!OnPlanet)
- {
- playerRigidBody.AddForce(MouseDirection * 4f);
- Vector3 OffPlanetDir = Vector3.Lerp(transform.up, MouseDirection, 5f * Time.deltaTime);
- transform.up = OffPlanetDir;
- }
- else
- {
- CameraTargetPosition += MouseDirection * 0.001f;
- }
- }
- FollowCamera.transform.position = CameraTargetPosition; // Set camera positition
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment