cwisbg

Playerv_3

Jan 29th, 2019
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.69 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 HealthDecaySpeed = 2f;
  9.     public float MoveSpeed = 0.1f;
  10.     public float GravityForce = 5;
  11.     public float JumpSpeed = 300;
  12.     public GameObject LineObject;
  13.     public GameObject PlanetListParent;
  14.     public GameObject TakeOffEffect;
  15.     public GameObject ThrustTrails;
  16.     public SpriteRenderer ObjectSprite;
  17.     public GameObject DeathPrefab;
  18.     public float LineRenderOffset = 1f;
  19.     public LayerMask groundLayer;
  20.     public float RayLength = .5f;
  21.     public float SurfaceSearchOffset = 2;
  22.     public bool OnPlanet = false;
  23.     private LineRenderer LineRenderObj;
  24.     private GameObject ClickedPlanet;
  25.     private GameObject Planet = null;
  26.     private GameObject PlanetClosest;
  27.     private Vector3 PlayerPosition;
  28.     private Vector3 PlanetPosition;
  29.     private Vector3 PlanetDirection;
  30.     private Vector3 OnPlanetPos;
  31.     private Vector3 OnPlanetDir;
  32.     private Vector3 MouseDirection;
  33.     private float PlayerPlanetAngle = 0.0f;
  34.     private List<Transform> PlanetList = new List<Transform>();
  35.     private Rigidbody2D playerRigidBody;
  36.     private Vector3 LineStart = new Vector3(1000, 0, 0);
  37.     private bool MoveVertical = false;
  38.     public GameObject TestObj;
  39.     private CircleCollider2D PlanetCollider = null;
  40.     private PolygonCollider2D PlanetColliderP = null;
  41.     private float deadTimer = 2f;
  42.     private ParticleSystem.EmissionModule TrailsParticlesModule;
  43.     private ParticleSystem TrailsParticles;
  44.     private bool DoDeath = true;
  45.     private bool IsAlive = true;
  46.     public GameObject FollowCamera;
  47.     private Vector3 CameraTargetPosition;
  48.     public Vector3 CameraFollowOffset;
  49.  
  50.     GameObject MousedObject(Vector3 mousePos)
  51.     {
  52.         GameObject HitObj = null;
  53.         Vector2 MousePos2D = new Vector2(mousePos.x, mousePos.y);
  54.         RaycastHit2D hit = Physics2D.Raycast(MousePos2D, Vector2.zero, groundLayer);
  55.         if (hit)
  56.         {
  57.             HitObj = hit.transform.gameObject;
  58.         }
  59.         return HitObj;
  60.     }
  61.     Vector3 MousePosition()
  62.     {
  63.         Vector3 MousePos = Input.mousePosition;
  64.         MousePos = Camera.main.ScreenToWorldPoint(MousePos);
  65.         MousePos = new Vector3(MousePos.x, MousePos.y, 0f);
  66.         return MousePos;
  67.     }
  68.  
  69.     GameObject GetClosest(List<Transform> PlanetList, Vector3 CloseToPos)
  70.     {
  71.         GameObject bestTarget = null;
  72.         float closestDistanceSqr = Mathf.Infinity;
  73.         int PlanetListLen = PlanetList.Count;
  74.         for (int i = 0; i < PlanetListLen; i++)
  75.         {
  76.             Vector3 directionToTarget = PlanetList[i].transform.position - CloseToPos;
  77.             float dSqrToTarget = directionToTarget.sqrMagnitude;
  78.             if (dSqrToTarget < closestDistanceSqr)
  79.             {
  80.                 closestDistanceSqr = dSqrToTarget;
  81.                 bestTarget = PlanetList[i].gameObject;
  82.             }
  83.         }
  84.         return bestTarget;
  85.     }
  86.  
  87.     Vector3 PLanetLaunchLine(Vector3 StartPos, Vector3 MousePos)
  88.     {
  89.         Vector3 SavePos = StartPos;
  90.         Vector3 SaveDir = Vector3.zero;
  91.         StartPos.z = LineRenderOffset;
  92.         LineRenderObj.SetPosition(0, StartPos);
  93.         Vector3 NewPosDir = SavePos - MousePos;
  94.         float VectorMag = Vector3.Magnitude(NewPosDir);
  95.         NewPosDir.Normalize();
  96.         NewPosDir *=( VectorMag*0.5f);
  97.         SaveDir = NewPosDir;
  98.         NewPosDir += SavePos;
  99.         NewPosDir.z = LineRenderOffset;
  100.         LineRenderObj.SetPosition(1, NewPosDir);
  101.         return SaveDir;
  102.     }
  103.     Vector3 PST(Vector3 Planet, Vector3 Direction) // Planet Surface Tracker
  104.     {
  105.         Vector3 SurfacePoint = Vector3.zero;
  106.         return SurfacePoint;
  107.     }
  108.  
  109.  
  110.     void Start () {
  111.         playerRigidBody = transform.GetComponent<Rigidbody2D>();
  112.         LineRenderObj = LineObject.GetComponent<LineRenderer>();
  113.         TrailsParticles = ThrustTrails.GetComponent<ParticleSystem>();
  114.         TrailsParticlesModule = TrailsParticles.emission;
  115.         //foreach (Transform potentialTarget in PlanetListParent.transform)
  116.         foreach (GameObject child in GameObject.FindGameObjectsWithTag("Planets"))
  117.         {
  118.             Transform potentialTarget = child.transform;
  119.             PlanetList.Add(potentialTarget);
  120.         }
  121.         PlanetClosest = GetClosest(PlanetList, PlayerPosition);
  122.        
  123.     }
  124.  
  125.  
  126.     void Update() {
  127.         PlayerPosition = transform.position;
  128.         PlanetPosition = PlanetClosest.transform.position;
  129.         CameraTargetPosition = PlayerPosition + CameraFollowOffset;
  130.         if (Input.GetMouseButtonUp(0) && IsAlive == true)
  131.         {
  132.             MoveVertical = true;
  133.         }
  134.  
  135.  
  136.        
  137.  
  138.         Vector3 PlanetDirection = PlanetPosition - PlayerPosition;
  139.         Vector3 PlanetDirectionNorm = PlanetDirection;
  140.         PlanetDirectionNorm.Normalize();
  141.  
  142.         LineRenderObj.SetPosition(0, LineStart);
  143.         LineRenderObj.SetPosition(1, LineStart);
  144.  
  145.         if (OnPlanet)
  146.         {
  147.  
  148.  
  149.             TrailsParticlesModule.enabled = false;
  150.             Vector3 StartDir = PlayerPosition - PlanetPosition;
  151.             StartDir.Normalize();
  152.             //Debug.DrawRay(PlanetPosition, StartDir * 3, Color.cyan);
  153.             Vector3 EndDir = (PlanetPosition + MouseDirection) - PlanetPosition;
  154.             EndDir.Normalize();
  155.             //Debug.DrawRay(PlanetPosition, EndDir * 3, Color.red);
  156.             Vector3 MiddleDir = (StartDir * 0.8f) + (EndDir * 0.2f);
  157.             MiddleDir.Normalize();
  158.             Vector3 ToSurface = MiddleDir;
  159.             float dist = Vector3.Distance(PlayerPosition, PlanetPosition);
  160.             //MiddleDir *= (PlanetClosest.transform.localScale[0]*0.5f) * SurfaceSearchOffset;
  161.             MiddleDir *= (dist+0.1f);
  162.             MiddleDir += PlanetPosition;
  163.             ToSurface *= -1;
  164.             RaycastHit2D SurfaceHit = Physics2D.Raycast(MiddleDir, ToSurface, RayLength*1.5f, groundLayer);
  165.             Debug.DrawLine(MiddleDir, MiddleDir+(ToSurface * (RayLength * 1.5f)), Color.red);
  166.             if (SurfaceHit.collider != null)
  167.             {
  168.                 Debug.DrawLine(MiddleDir, SurfaceHit.point, Color.blue);
  169.                 Vector3 NewPos = Vector3.Lerp(transform.position, SurfaceHit.point, MoveSpeed * Time.deltaTime);
  170.                 transform.position = NewPos;
  171.                 OnPlanetDir = SurfaceHit.normal;
  172.             }
  173.             PlanetDirection = OnPlanetPos - transform.position;
  174.             Vector3 NewDirGravity = Vector3.Lerp(transform.up, OnPlanetDir, 40.0f * Time.deltaTime);
  175.             transform.up = NewDirGravity;
  176.             if (MoveVertical == true)
  177.  
  178.             {
  179.                 //playerRigidBody.AddForce(transform.up * JumpSpeed);
  180.                 OnPlanet = false;
  181.                 MoveVertical = false;
  182.                 PlanetCollider = PlanetClosest.GetComponent<CircleCollider2D>();
  183.                 if (PlanetCollider != null)
  184.                 {
  185.                     PlanetCollider.enabled = false;
  186.                 }
  187.                 PlanetColliderP = PlanetClosest.GetComponent<PolygonCollider2D>();
  188.                 if (PlanetColliderP != null)
  189.                 {
  190.                     PlanetColliderP.enabled = false;
  191.                 }
  192.                 var Explosion = (GameObject)Instantiate(TakeOffEffect, PlayerPosition, transform.rotation);
  193.                 Destroy(Explosion, 0.5f);
  194.                 playerRigidBody.AddForce(MouseDirection * JumpSpeed);
  195.                 transform.up = MouseDirection;
  196.             }
  197.             if (Health < 1)
  198.             {
  199.                 Health += HealthDecaySpeed * Time.deltaTime;
  200.             }
  201.  
  202.         }
  203.         // Cast Ray from player in the direction of closest planet
  204.         else
  205.         {
  206.             PlanetClosest = GetClosest(PlanetList, PlayerPosition);
  207.             PlanetPosition = PlanetClosest.transform.position;
  208.             TrailsParticlesModule.enabled = true;
  209.            
  210.  
  211.             ObjectSprite.color = Color.Lerp(new Color(1, 0, 0), new Color(1, 1, 1), Health);
  212.             RaycastHit2D hit = Physics2D.Raycast(PlayerPosition, PlanetDirectionNorm, RayLength, groundLayer);
  213.             //Debug.DrawLine(PlayerPosition, PlayerPosition + PlanetDirectionNorm * RayLength, Color.red);
  214.             if (hit.collider != null)
  215.             {
  216.                 //Debug.DrawLine(PlayerPosition, hit.point, Color.red);
  217.                 OnPlanet = true;
  218.                 playerRigidBody.velocity = Vector2.zero;
  219.                 playerRigidBody.angularVelocity = 0.0f;
  220.                 OnPlanetPos = hit.point;
  221.                 OnPlanetDir = hit.normal;
  222.                 if (PlanetCollider != null)
  223.                 {
  224.                     PlanetCollider.enabled = true;
  225.                 }
  226.                 if (PlanetColliderP != null)
  227.                 {
  228.                     PlanetColliderP.enabled = true;
  229.                 }
  230.                 // Hit planet effect
  231.  
  232.             }
  233.             else
  234.             {
  235.                 OnPlanet = false;
  236.             }
  237.             Health -= HealthDecaySpeed * Time.deltaTime;
  238.         }
  239.  
  240.         if (Health <= 0.0f)
  241.         {
  242.             ObjectSprite.enabled = false;
  243.             if (DoDeath)
  244.             {
  245.                 var DeathExplosion = (GameObject)Instantiate(DeathPrefab, PlayerPosition, transform.rotation);
  246.                 //DeathExplosion.transform.parent = transform;
  247.                 //Destroy(LandingExplosion, 1.5f);
  248.                 playerRigidBody.velocity = Vector2.zero;
  249.                 playerRigidBody.angularVelocity = 0.0f;
  250.                 DoDeath = false;
  251.                 IsAlive = false;
  252.             }
  253.  
  254.             TrailsParticlesModule.enabled = false;
  255.             if (deadTimer <= 0.0f) {
  256.                 Scene scene = SceneManager.GetActiveScene();
  257.                 SceneManager.LoadScene(scene.buildIndex);
  258.             }
  259.             deadTimer -= 1f * Time.deltaTime;
  260.         }
  261.         if (Input.GetMouseButton(0))
  262.         {
  263.             Vector3 MousePos = MousePosition();
  264.             if (IsAlive)
  265.             {
  266.                 MouseDirection = PLanetLaunchLine(transform.position, MousePos);
  267.                 MouseDirection.Normalize();
  268.             }
  269.  
  270.  
  271.             if (!OnPlanet)
  272.             {
  273.                 playerRigidBody.AddForce(MouseDirection * 4f);
  274.                 Vector3 OffPlanetDir = Vector3.Lerp(transform.up, MouseDirection, 5f * Time.deltaTime);
  275.                 transform.up = OffPlanetDir;
  276.             }
  277.             else
  278.             {
  279.                 CameraTargetPosition += MouseDirection * 0.001f;
  280.  
  281.             }
  282.         }
  283.         FollowCamera.transform.position = CameraTargetPosition; // Set camera positition
  284.     }
  285. }
Advertisement
Add Comment
Please, Sign In to add comment