Quantcast
Channel: Questions in topic: "loops"
Viewing all articles
Browse latest Browse all 184

Argument if out of range: loops and arrays

$
0
0
I keep getting this error. Argument is out of range. What does it mean? Here is the code: using System.Linq; using System.Collections.Generic; using UnityEngine; namespace Assets.Scripts.Player { public class CameraScript : MonoBehaviour { Bounds b; private new Transform transform; private Vector3 DesiredPos; public List Players; public float camSpeed; private Camera cam; public List UpdatePlayers; void Awake () { transform = GetComponent(); cam = GetComponent (); } public void Start () { b = new Bounds(new Vector2(0f, 0f), new Vector2(12f, 8f)); var p = GameObject.FindGameObjectsWithTag ("Player"); Players = new List (); for (int i = 0; i < p.Length; i++) { Players.Add(p[i].GetComponent()); } } void Update () { if (Players.Count <= 0) { return; } DesiredPos = Vector3.zero; float distance = 0f; var hSort = Players.OrderByDescending (p => p.position.y); var wSort = Players.OrderByDescending (p => p.position.x); var mHeight = hSort.First ().position.y - hSort.Last ().position.y; var mWidth = wSort.First ().position.x - wSort.Last ().position.x; var distanceH = -(mHeight + 5f) * 0.5f / Mathf.Tan (cam.fieldOfView * 0.5f * Mathf.Deg2Rad); var distanceW = -(mWidth / cam.aspect + 5f) * 0.5f / Mathf.Tan (cam.fieldOfView * 0.5f * Mathf.Deg2Rad); distance = distanceH < distanceW ? distanceH : distanceW; for (int i = 0; i < Players.Count; i++) { DesiredPos += Players [i].position; } if (distance > -10f) distance = -10f; DesiredPos /= Players.Count; DesiredPos.z = distance; var k = GameObject.FindGameObjectsWithTag ("Player"); for (int l = 0;l < k.Length; l++) { if (b.Contains (k [l].transform.position)) { Players.Add (k [l].GetComponent()); } else { Players.RemoveAt(l); } } } void LateUpdate () { transform.position = Vector3.MoveTowards (transform.position, DesiredPos, camSpeed); } } } Copied it from the internet and tried to adapt it to my game.

Viewing all articles
Browse latest Browse all 184

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>