Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerController : MonoBehaviour {
- private float xDir = 0;
- private float yDir = 0;
- private new Rigidbody rigidbody;
- private bool controlsLocked = true;
- public LineRenderer teleArc;
- [RangeAttribute(1, 10)]
- public float sensitivity;
- void Start() {
- rigidbody = GetComponent<Rigidbody>();
- rigidbody.freezeRotation = true;
- Cursor.lockState = CursorLockMode.None;
- Cursor.visible = true;
- FreeControls();
- }
- bool inAir() {
- RaycastHit hit;
- Physics.Raycast(rigidbody.transform.position, Vector3.down, out hit);
- if (hit.distance > .5) {
- return true;
- }
- return false;
- }
- void drawLine(RaycastHit hit) {
- if (hit.point == Vector3.zero) {
- teleArc.enabled = false;
- } else {
- Vector3[] arcPos = {
- rigidbody.position + transform.right * (float).2 + transform.up * (float)-.2,
- hit.point,
- };
- teleArc.SetPositions(arcPos);
- teleArc.enabled = true;
- }
- }
- RaycastHit getGunArc(float velocity) {
- Vector3[] arcPos = new Vector3[25];
- RaycastHit hit;
- Vector3 pos = rigidbody.transform.position;
- Vector3 dir = rigidbody.transform.forward;
- Debug.Log("Initial Pos:");
- Debug.Log(pos);
- Debug.Log("Initial Dir:");
- Debug.Log(dir);
- //dir.Normalize(); //unneccesary, as rigidbody.transform.forward is always normalized
- Vector3 initialVelocity = dir * velocity;
- const double t = .1;
- const double a = -9.81;
- const int MAX_SEGMENTS = 20;
- arcPos[0] = pos;
- int count = 1;
- while (true) {
- float Dy = (float)(initialVelocity.y * t + 1 / 2 * a * t * t);
- dir.y -= (1 - Dy) / initialVelocity.y;
- dir.Normalize();
- Physics.Raycast(pos, dir, out hit, velocity);
- pos += new Vector3(initialVelocity.x, Dy, initialVelocity.z);
- initialVelocity.y = (float)(initialVelocity.y + a * t);
- Debug.Log(pos);
- arcPos[count] = pos;
- count++;
- if (hit.point != Vector3.zero || count >= MAX_SEGMENTS) {
- break;
- }
- }
- teleArc.SetPositions(arcPos);
- teleArc.enabled = true;
- return hit;
- }
- public float gunVelocity;
- void FixedUpdate() {
- if (!controlsLocked) {
- if (Input.GetMouseButtonUp(0)) {
- //rigidbody.position = hit.point + new Vector3(0, (float).5, 0);
- RaycastHit hit = getGunArc(gunVelocity);
- Debug.Log("Final point:");
- Debug.Log(hit.point);
- drawLine(hit);
- }
- xDir += Input.GetAxis("Mouse X") * sensitivity;
- yDir -= Input.GetAxis("Mouse Y") * sensitivity;
- if (yDir > 90) {
- yDir = 90;
- } else if (yDir < -90) {
- yDir = -90;
- }
- rigidbody.rotation = Quaternion.Euler(yDir, xDir, 0.0f);
- }
- }
- void LockControls() {
- controlsLocked = true;
- Cursor.lockState = CursorLockMode.None;
- Cursor.visible = true;
- }
- void FreeControls() {
- controlsLocked = false;
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- }
- void ResetPosition() {
- rigidbody.position = new Vector3(-2, -2, -2);
- xDir = 0;
- yDir = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement