Advertisement
Guest User

Untitled

a guest
Sep 10th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.71 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. using TMPro;
  7.  
  8. public class PlayerTargeting : MonoBehaviour {
  9.     // Projectile Stuff
  10.     private float ballForce;
  11.     private float finalForce;
  12.     public float forceMultiplier = 1.0f;
  13.     public Transform ballSpawn;
  14.     public Rigidbody ball;
  15.     GameObject prefab;
  16.     public GameObject discoBall;
  17.  
  18.     // Raycast Stuff
  19.  
  20.  
  21.     // Targeting Stuff for UI Crosshair
  22.     public GameObject cursor; //crosshair
  23.     public Canvas canvas; //parent canvas for crosshair
  24.     float pixelX; //possible deprecated
  25.     float pixelY;
  26.     Vector2 cursorPos;
  27.  
  28.     public float posX;
  29.     public float posY;
  30.  
  31.  
  32.     // UI Stuff
  33.     public TextMeshProUGUI scouter;
  34.     private bool isDown;
  35.     private bool charging = true;
  36.     private bool draining = false;
  37.     // Joystick
  38.     private Vector3 rightJoystickInput;
  39.     public float cursorMoveSpeed = 150f;
  40.     public bool flipY = false;
  41.     public bool flipX = false;
  42.  
  43.    
  44.  
  45.     // Use this for initialization
  46.     void Start()
  47.     {
  48.  
  49.     }
  50.  
  51.     // Update is called once per frame
  52.     void Update()
  53.     {
  54.         // RightJoyStick - Move UI Crosshair
  55.        
  56.         rightJoystickInput = RightJoystick.Instance.GetInputDirection();
  57.  
  58.         float xMovementRightJoystick = rightJoystickInput.x; // The horizontal movement from joystick 02
  59.         float yMovementRightJoystick = rightJoystickInput.y; // The vertical movement from joystick 02
  60.  
  61.         float inputX = xMovementRightJoystick * (flipX ? -1 : 1);
  62.         float inputY = yMovementRightJoystick * (flipY ? -1 : 1);
  63.  
  64.         float deltaX = inputX * cursorMoveSpeed;
  65.         float deltaY = inputY * cursorMoveSpeed;
  66.  
  67.         cursorPos.x += deltaX * Time.deltaTime;
  68.         cursorPos.y += deltaY * Time.deltaTime;
  69.  
  70.         if( cursorPos.x < 0 ) cursorPos.x = 0;
  71.         if( cursorPos.x > Screen.width ) cursorPos.x = Screen.width;
  72.  
  73.         if( cursorPos.y < 0 ) cursorPos.y = 0;
  74.         if( cursorPos.y > Screen.height ) cursorPos.y = Screen.height;
  75.  
  76.         cursor.transform.position = cursorPos;
  77.        
  78.  
  79.         // Raycast Stuff
  80.         Ray ray = Camera.main.ScreenPointToRay( cursorPos );
  81.         RaycastHit hit;
  82.         Debug.DrawRay(ray.origin, ray.direction * 100, Color.yellow);
  83.  
  84.         if (Physics.Raycast(transform.position, -Vector3.up, out hit))
  85.         {
  86.             print("Found an object - distance: " + hit.distance);
  87.         }
  88.  
  89.  
  90.  
  91.         // Firing Mechanism - Power Calculations
  92.  
  93.         if (FireButton.Instance.isDown)
  94.         {
  95.             if (charging)
  96.             {
  97.                 ballForce += 1f;
  98.                 if (ballForce >= 100f)
  99.                 {
  100.                     ballForce = 100f;
  101.                     charging = false;
  102.                     draining = true;
  103.                 }
  104.             }
  105.             if (draining)
  106.             {
  107.                 ballForce -= 0.7f;
  108.                 if (ballForce <= 0f)
  109.                 {
  110.                     ballForce = 0f;
  111.                     draining = false;
  112.                     charging = true;
  113.                 }
  114.             }
  115.  
  116.             // Debugging ballForce
  117.             print(ballForce);
  118.  
  119.             // Need to change this to a graphical power bar
  120.             int powerlvl = (int)ballForce;
  121.             scouter.text = "Power: " + powerlvl.ToString();
  122.         }
  123.         if (FireButton.Instance.isDown == false)
  124.         {
  125.             if (ballForce > 0f)
  126.             {
  127.                 if (ballForce >= 10f)
  128.                 {
  129.                     finalForce = 30000f;
  130.                 }
  131.                 if (ballForce <= 11f && ballForce >= 20f)
  132.                 {
  133.                     finalForce = 35000f;
  134.                 }
  135.                 if (ballForce <= 21f && ballForce >= 30f)
  136.                 {
  137.                     finalForce = 40000f;
  138.                 }
  139.                 if (ballForce <= 31f && ballForce >= 40f)
  140.                 {
  141.                     finalForce = 50000f;
  142.                 }
  143.                 if (ballForce <= 41f && ballForce >= 50f)
  144.                 {
  145.                     finalForce = 60000f;
  146.                 }
  147.                 if (ballForce <= 51f && ballForce >= 60f)
  148.                 {
  149.                     finalForce = 70000f;
  150.                 }
  151.                 if (ballForce <= 61f && ballForce >= 70f)
  152.                 {
  153.                     finalForce = 80000f;
  154.                 }
  155.                 if (ballForce <= 71f && ballForce >= 80f)
  156.                 {
  157.                     finalForce = 90000f;
  158.                 }
  159.                 if (ballForce <= 81f && ballForce >= 90f)
  160.                 {
  161.                     finalForce = 100000f;
  162.                 }
  163.                 if (ballForce <= 91f && ballForce >= 95f)
  164.                 {
  165.                     finalForce = 150000f;
  166.                 }
  167.                 if (ballForce <= 96f && ballForce >= 97f)
  168.                 {
  169.                     finalForce = 200000f;
  170.                 }
  171.                 if (ballForce <= 98f && ballForce >= 99f)
  172.                 {
  173.                     finalForce = 300000f;
  174.                 }
  175.                 if (ballForce == 100f)
  176.                 {
  177.                     finalForce = 400000f;
  178.                 }
  179.  
  180.                 // Launch Ball
  181.                 Rigidbody ballRigidbody;
  182.                 // Ball Spawns at Camera presently.
  183.                 ballRigidbody = Instantiate(ball, ballSpawn.position, ballSpawn.rotation) as Rigidbody;
  184.                 ballRigidbody.AddForce(ray.direction * finalForce * forceMultiplier);
  185.                 // Actually getting the ball to align to face raytrace direction to fire on target.
  186.                 ballForce = 0f;
  187.             }
  188.  
  189.         }
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement