Advertisement
CaptainSpaceCat

Apparition

Mar 3rd, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.72 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour {
  6.  
  7.     private float xDir = 0;
  8.     private float yDir = 0;
  9.     private new Rigidbody rigidbody;
  10.     private bool controlsLocked = true;
  11.  
  12.     public LineRenderer teleArc;
  13.  
  14.     [RangeAttribute(1, 10)]
  15.     public float sensitivity;
  16.  
  17.     void Start() {
  18.         rigidbody = GetComponent<Rigidbody>();
  19.         rigidbody.freezeRotation = true;
  20.         Cursor.lockState = CursorLockMode.None;
  21.         Cursor.visible = true;
  22.         FreeControls();
  23.     }
  24.  
  25.     bool inAir() {
  26.         RaycastHit hit;
  27.         Physics.Raycast(rigidbody.transform.position, Vector3.down, out hit);
  28.         if (hit.distance > .5) {
  29.             return true;
  30.         }
  31.         return false;
  32.     }
  33.  
  34.     void drawLine(RaycastHit hit) {
  35.         if (hit.point == Vector3.zero) {
  36.             teleArc.enabled = false;
  37.         } else {
  38.             Vector3[] arcPos = {
  39.                 rigidbody.position + transform.right * (float).2 + transform.up * (float)-.2,
  40.                 hit.point,
  41.             };
  42.             teleArc.SetPositions(arcPos);
  43.             teleArc.enabled = true;
  44.         }
  45.     }
  46.  
  47.     RaycastHit getGunArc(float velocity) {
  48.         Vector3[] arcPos = new Vector3[25];
  49.  
  50.         RaycastHit hit;
  51.  
  52.         Vector3 pos = rigidbody.transform.position;
  53.         Vector3 dir = rigidbody.transform.forward;
  54.         Debug.Log("Initial Pos:");
  55.         Debug.Log(pos);
  56.         Debug.Log("Initial Dir:");
  57.         Debug.Log(dir);
  58.         //dir.Normalize();  //unneccesary, as rigidbody.transform.forward is always normalized
  59.         Vector3 initialVelocity = dir * velocity;
  60.  
  61.         const double t = .1;
  62.         const double a = -9.81;
  63.         const int MAX_SEGMENTS = 20;
  64.         arcPos[0] = pos;
  65.         int count = 1;
  66.         while (true) {
  67.             float Dy = (float)(initialVelocity.y * t + 1 / 2 * a * t * t);
  68.             dir.y -= (1 - Dy) / initialVelocity.y;
  69.             dir.Normalize();
  70.             Physics.Raycast(pos, dir, out hit, velocity);
  71.            
  72.             pos += new Vector3(initialVelocity.x, Dy, initialVelocity.z);
  73.             initialVelocity.y = (float)(initialVelocity.y + a * t);
  74.             Debug.Log(pos);
  75.             arcPos[count] = pos;
  76.             count++;
  77.             if (hit.point != Vector3.zero || count >= MAX_SEGMENTS) {
  78.                 break;
  79.             }
  80.         }
  81.  
  82.         teleArc.SetPositions(arcPos);
  83.         teleArc.enabled = true;
  84.  
  85.         return hit;
  86.     }
  87.  
  88.     public float gunVelocity;
  89.  
  90.     void FixedUpdate() {
  91.        
  92.        
  93.  
  94.         if (!controlsLocked) {
  95.  
  96.             if (Input.GetMouseButtonUp(0)) {
  97.                 //rigidbody.position = hit.point + new Vector3(0, (float).5, 0);
  98.                 RaycastHit hit = getGunArc(gunVelocity);
  99.                 Debug.Log("Final point:");
  100.                 Debug.Log(hit.point);
  101.                 drawLine(hit);
  102.             }
  103.  
  104.             xDir += Input.GetAxis("Mouse X") * sensitivity;
  105.             yDir -= Input.GetAxis("Mouse Y") * sensitivity;
  106.  
  107.             if (yDir > 90) {
  108.                 yDir = 90;
  109.             } else if (yDir < -90) {
  110.                 yDir = -90;
  111.             }
  112.  
  113.             rigidbody.rotation = Quaternion.Euler(yDir, xDir, 0.0f);
  114.         }
  115.     }
  116.  
  117.     void LockControls() {
  118.         controlsLocked = true;
  119.         Cursor.lockState = CursorLockMode.None;
  120.         Cursor.visible = true;
  121.     }
  122.  
  123.     void FreeControls() {
  124.         controlsLocked = false;
  125.         Cursor.lockState = CursorLockMode.Locked;
  126.         Cursor.visible = false;
  127.     }
  128.  
  129.     void ResetPosition() {
  130.         rigidbody.position = new Vector3(-2, -2, -2);
  131.         xDir = 0;
  132.         yDir = 0;
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement