Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Mirror;
  5.  
  6.  
  7. public class PlayerMovement : NetworkBehaviour
  8. {
  9.     public float speed;
  10.     private Rigidbody2D rb;
  11.     private Vector2 mv;
  12.  
  13.     public Camera viewCamera;
  14.  
  15.  
  16.     public float viewRadius;
  17.     [Range(0, 360)]
  18.     public float viewAngle;
  19.  
  20.     public LayerMask targetMask;
  21.     public LayerMask obstacleMask;
  22.  
  23.     [HideInInspector]
  24.     public List<Transform> visibleTargets = new List<Transform>();
  25.  
  26.     public float meshResolution;
  27.  
  28.  
  29.  
  30.  
  31.  
  32.     // Start is called before the first frame update
  33.     void Start()
  34.     {
  35.        
  36.  
  37.         if (isLocalPlayer)
  38.         {
  39.             //StartCoroutine("FindTargetsWithDelay", .2f);
  40.             viewCamera = Camera.main;
  41.             rb = GetComponent<Rigidbody2D>();
  42.  
  43.         }
  44.     }
  45.  
  46.    
  47.  
  48.     // Update is called once per frame
  49.     void Update()
  50.     {
  51.         if (isLocalPlayer)
  52.         {
  53.             //DrawFieldOfView();
  54.             Vector2 mi = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
  55.             mv = mi.normalized * speed;
  56.  
  57.  
  58.             var dir = Input.mousePosition - viewCamera.WorldToScreenPoint(transform.position);
  59.             var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  60.  
  61.             transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
  62.  
  63.             /*anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
  64.             anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));*/
  65.  
  66.         }
  67.     }
  68.  
  69.     private void FixedUpdate()
  70.     {
  71.         if (isLocalPlayer)
  72.         {
  73.             rb.MovePosition(rb.position + mv * Time.fixedDeltaTime);
  74.         }
  75.        
  76.     }
  77.  
  78.  
  79. //DONT LOOK DONW HERE
  80.  
  81.  
  82.    /* void FindVisibleTargets()
  83.     {
  84.         visibleTargets.Clear();
  85.         Collider[] targetsInViewRadius = Physics.OverlapSphere(transform.position, viewRadius, targetMask);
  86.  
  87.         for (int i = 0; i < targetsInViewRadius.Length; i++)
  88.         {
  89.             Transform target = targetsInViewRadius[i].transform;
  90.             Vector3 dirToTarget = (target.position - transform.position).normalized;
  91.             if (Vector3.Angle(transform.forward, dirToTarget) < viewAngle / 2)
  92.             {
  93.                 float dstToTarget = Vector3.Distance(transform.position, target.position);
  94.  
  95.                 if (!Physics.Raycast(transform.position, dirToTarget, dstToTarget, obstacleMask))
  96.                 {
  97.                     visibleTargets.Add(target);
  98.                 }
  99.             }
  100.         }
  101.     }
  102.  
  103.     void DrawFieldOfView()
  104.     {
  105.         int stepCount = Mathf.RoundToInt(viewAngle * meshResolution);
  106.         float stepAngleSize = viewAngle / stepCount;
  107.  
  108.         for (int i = 0; i < stepCount; i++)
  109.         {
  110.             float angle = transform.eulerAngles.y - viewAngle / 2 + stepAngleSize * i;
  111.             Debug.DrawLine(transform.position, transform.position + DirFromAngle(angle, true) * viewRadius, Color.red);
  112.         }
  113.     }
  114.  
  115.     public Vector3 DirFromAngle(float angleInDegrees, bool angleIsGlobal)
  116.     {
  117.         if (!angleIsGlobal)
  118.         {
  119.             angleInDegrees += transform.eulerAngles.y;
  120.         }
  121.         return new Vector3(Mathf.Sin(angleInDegrees * Mathf.Deg2Rad), 0, Mathf.Cos(angleInDegrees * Mathf.Deg2Rad));
  122.     }
  123.  
  124.     IEnumerator FindTargetsWithDelay(float delay)
  125.     {
  126.         while (true)
  127.         {
  128.             yield return new WaitForSeconds(delay);
  129.             FindVisibleTargets();
  130.         }
  131.     }*/
  132.  
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement