V_SMC_Camera.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Visyde{
  5. public class V_SMC_Camera : MonoBehaviour {
  6. // The handler of crosshair sprites:
  7. public V_SMC_Handler crosshairHandler;
  8. // For the FPS mouse look:
  9. float rotationX = 0F;
  10. float rotationY = 0F;
  11. // For the raycasting function:
  12. Vector3 fireDirection;
  13. Vector3 firePoint;
  14. // Use this for initialization
  15. void Start () {
  16. // Lock and hide the cursor:
  17. Cursor.lockState = CursorLockMode.Locked;
  18. Cursor.visible = false;
  19. }
  20. // Update is called once per frame
  21. void Update () {
  22. // FPS mouse look:
  23. rotationX += Input.GetAxis("Mouse X") * 2;
  24. rotationY -= Input.GetAxis("Mouse Y") * 2;
  25. Quaternion rotation = Quaternion.Euler(rotationY, rotationX, 0);
  26. transform.rotation = rotation;
  27. // Call raycast method:
  28. Hit ();
  29. }
  30. void Hit(){
  31. // Raycasting variables:
  32. RaycastHit hit;
  33. fireDirection = transform.TransformDirection(Vector3.forward) * 10;
  34. firePoint = transform.position;
  35. // Do raycasting:
  36. if (Physics.Raycast (firePoint, (fireDirection), out hit, Mathf.Infinity)) {
  37. // Change the color based on what object is under the crosshair:
  38. if (hit.transform.name == "Friendly") {
  39. crosshairHandler.ChangeColor (Color.green);
  40. } else if (hit.transform.name == "Enemy") {
  41. crosshairHandler.ChangeColor (Color.red);
  42. } else {
  43. crosshairHandler.ChangeColor (Color.white);
  44. }
  45. } else {
  46. crosshairHandler.ChangeColor (Color.white);
  47. }
  48. // Debug the ray out in the editor:
  49. Debug.DrawRay(firePoint, fireDirection, Color.green);
  50. }
  51. }
  52. }