Advertisement
thegreen9

Projectile prediction 2.5d

Jan 14th, 2019
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.15 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. using System;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace Platformer
  9. {
  10.     public class ProjectilePrediction : MonoBehaviour
  11.     {
  12.         private LineRenderer _line;
  13.         public GameObject Target;
  14.         public Transform FirePointPos;
  15.         public Rigidbody2D Rigit;
  16.         private int steps = 50;
  17.         private Vector3[] points;
  18.         private Vector3 direction
  19.         {
  20.             get { return (Vector2)Target.transform.position - (Vector2)transform.position; }
  21.         }
  22.         CharacterMotor _motor;
  23.         private void Start()
  24.         {
  25.             _motor = GetComponent<CharacterMotor>();
  26.             _line = GetComponent<LineRenderer>();
  27.             _line.positionCount = steps;
  28.         }
  29.         void Update()
  30.         {
  31.             FirePointPos.transform.position = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y + 1.6f,gameObject.transform.position.z);
  32.             CameraToWorldRay();
  33.             points = Plot(Rigit, FirePointPos.position, direction * 25, steps);
  34.             GetPos(points);
  35.             RayCheckCollisions(points);
  36.             SetCharacterDirection();
  37.         }
  38.         //Просчитывам массив точек
  39.         public Vector3[] Plot(Rigidbody2D rigidbody, Vector3 pos, Vector3 velocity, int steps)
  40.         {
  41.             Vector3[] results = new Vector3[steps];
  42.             float timestep = Time.fixedDeltaTime / Physics2D.velocityIterations;
  43.             Vector3 gravityAccel = Physics2D.gravity * (rigidbody.gravityScale * 250) * timestep * timestep;
  44.             float drag = 1f - timestep * rigidbody.drag;
  45.             Vector3 moveStep = velocity * timestep;
  46.  
  47.             for (int i = 0; i < steps; ++i)
  48.             {
  49.                 moveStep += gravityAccel;
  50.                 moveStep *= drag;
  51.                 pos += moveStep;
  52.                 results[i] = pos;
  53.             }
  54.  
  55.             return results;
  56.         }
  57.         public void GetPos(Vector3[] array)
  58.         {
  59.             _line.SetPositions(array);
  60.         }
  61.         //Некая зона поражения
  62.         public GameObject Zone;
  63.         //Пускаем лучи добра из 0 точки массива дабы найти точку попадения луча в землю
  64.         public void RayCheckCollisions(Vector3[] _points)
  65.         {
  66.             RaycastHit hit;
  67.             for (int i = 0; i < _points.Length; i++)
  68.             {
  69.                 if (_points[i] != points.Last())
  70.                     if (Physics.Raycast(_points[i], _points[i + 1], out hit))
  71.                     {
  72.                         //Если попали то перемещаем зону воздействия в точку попадания
  73.                         if (hit.collider != null)
  74.                             Zone.transform.position = _points[i];
  75.                     }
  76.  
  77.             }
  78.         }
  79.         //Пускаем луч в центральную линию (стену) игры
  80.         private void CameraToWorldRay()
  81.         {
  82.             RaycastHit hit;
  83.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  84.             if (Physics.Raycast(ray, out hit))
  85.             {
  86.                 if (hit.collider.isTrigger && hit.collider.tag == "CenterWall" && hit.collider != null)
  87.                     Target.transform.position = hit.point;
  88.             }
  89.         }
  90.         private void OnDisable()
  91.         {
  92.             _line.enabled = false;
  93.             Zone.SetActive(false);
  94.         }
  95.         private void OnEnable()
  96.         {
  97.             _line.enabled = true;
  98.             Zone.SetActive(true);
  99.         }
  100.         // Задаем направление персонажа
  101.          void SetCharacterDirection()
  102.         {
  103.             Vector3 lhs = FirePointPos.transform.TransformDirection(Vector3.forward);
  104.             Vector3 rhs = Target.transform.position - FirePointPos.transform.position;
  105.             _motor.Direction = Vector3.Dot(lhs, rhs) >= 0 ? CharacterDirection.Right : CharacterDirection.Left;
  106.         }
  107.              
  108.  
  109.        
  110.  
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement