public AttractorBehaviour GetClosest(Ray r, out float dist) { dist = 99999; AttractorBehaviour bestAttr = null; foreach (var a in _attractors) { float d = Vector3.Cross(r.direction, a.transform.position - r.origin).magnitude; if (d < dist) { dist = d; bestAttr = a; } } return(bestAttr); }
public void RemoveAttractor(AttractorBehaviour a) { _attractors.Remove(a); }
public void AddAttractor(AttractorBehaviour a) { _attractors.Add(a); }
// Update is called once per frame void Update() { if (!IsActive) { return; } hpos = Camera.main.ScreenPointToRay(new Vector3((Hand.point.x * 0.5f + 0.5f) * Screen.width, (Hand.point.y * 0.5f + 0.5f) * Screen.height, 4)); float dist = 0; var c = GetClosest(hpos, out dist); if (c != Closest && Closest != null) { Closest.selected = false; } Closest = c; if (Closest != null) { Closest.selected = true; } if (Hand.IsPinched && Hand.PinchTime > 2) { if (!_isRemoved && (Time.time - _time) > 2.0f) { _time = Time.time; _isRemoved = true; if (Closest != null) { Env.TriggerSpace(0.2f); Closest.Destroy(); _attractors.Remove(Closest); } } } else { _isRemoved = false; } if (Hand.IsPinchedUp) { if (!_isPinched && (Time.time - _time) > 2.0f) { _time = Time.time; _isPinched = true; Env.TriggerSpace(0.1f); var obj = Instantiate(Prefab.gameObject, Vector3.zero, Quaternion.identity, root) as GameObject; obj.transform.position = hpos.origin + hpos.direction * 3; AddAttractor(obj.GetComponent <AttractorBehaviour> ()); } } else { _isPinched = false; } }