void Update() { // Check if there is anything to interact with. if (overlappingTriggers.Count == 0) { InteractionText.text = ""; } else { for (int i = overlappingTriggers.Count - 1; i >= 0; --i) { if (overlappingTriggers[i] == null) { overlappingTriggers.RemoveAt(i); } } // Get the closest trigger. Collider closestTrigger = null; float closestDistance = float.MaxValue; foreach (Collider c in overlappingTriggers) { float distance = Vector2.Distance(new Vector2(transform.position.x, transform.position.z), new Vector2(c.transform.position.x, c.transform.position.z)); if (distance < closestDistance || c.gameObject.layer == LayerMask.NameToLayer("Weapon")) { closestTrigger = c; closestDistance = distance; } } if (closestTrigger != null) { // Set the text to the name. InteractionText.text = closestTrigger.transform.root.name; InteractionText.transform.position = closestTrigger.transform.root.position + new Vector3(0.0f, 0.6f + 0.1f * Mathf.Sin(Time.time), 0.0f); InteractionText.transform.LookAt(Camera.main.transform); // Act based on the type of object. if (closestTrigger.gameObject.layer == LayerMask.NameToLayer("Weapon")) { scrWeapon weapon = closestTrigger.transform.root.GetComponent <scrWeapon>(); if (Input.GetButtonDown("Assign Front")) { TrolleyWeapons.EquipFront(weapon); } else if (Input.GetButtonDown("Assign Left")) { TrolleyWeapons.EquipLeft(weapon); } else if (Input.GetButtonDown("Assign Right")) { TrolleyWeapons.EquipRight(weapon); } } } } }
/// <summary> /// Attaches the attachment and resets its position and rotation. /// </summary> /// <param name="attachment">Attachment.</param> public void Attach(scrWeapon attachment) { // Detach existing weapon. if (this.attachment != null) { Detach(); } this.attachment = attachment; attachment.transform.parent = Anchor.transform; attachment.transform.localPosition = new Vector3(0.02f, 0.02f, 0.02f); attachment.transform.localRotation = Quaternion.Euler(0.0f, 0.0f, Random.Range(-15.0f, 15.0f)); attachment.rigidbody.isKinematic = true; attachment.collider.isTrigger = true; attachment.gameObject.layer = Anchor.layer; foreach (Transform t in attachment.gameObject.GetComponentsInChildren <Transform>()) { t.gameObject.layer = Anchor.layer; } }
public void EquipRight(scrWeapon attachment) { if (rightHardPointsUsed == rightHardPointIndexes.Length) { // Replace a random hardpoint's attachment. RightHardPoints[Random.Range(0, rightHardPointIndexes.Length)].Attach(attachment); } else { // Get a random index that hasnt been used. int index = Random.Range(rightHardPointsUsed, rightHardPointIndexes.Length); // Get the hardpoint. HardPoint hp = RightHardPoints[rightHardPointIndexes[index]]; hp.Attach(attachment); // Swap the index with the first available one. int temp = rightHardPointIndexes[index]; rightHardPointIndexes[index] = rightHardPointIndexes[rightHardPointsUsed]; rightHardPointIndexes[rightHardPointsUsed] = temp; ++rightHardPointsUsed; } }
public HardPoint() { Anchor = null; attachment = null; }