/// <summary> /// Player uses item. /// </summary> void PlayerGive() { RaycastHit hit; //Checks if ray collided with an object if (Input.GetButtonDown("Fire2") && Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, interactDistance)) //Mouse2 button { Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.forward, Color.red); Debug.Log("Detected " + hit.transform.name); if (heldItem != null && IsFoodHeld() && hit.transform.tag == "Creature") { if (hit.transform.GetComponent <EnemyAI>().GetAllegiance() == EnemyAI.CreatureAllegiance.Neutral) { if (heldItemType == HeldItemType.BlueFood || heldItemType == HeldItemType.GreenFood) { Debug.Log("Fed " + hit.transform.name); blueFoodHeld.gameObject.SetActive(false); greenFoodHeld.gameObject.SetActive(false); heldItem.gameObject.SetActive(true); Destroy(heldItem.gameObject); heldItemType = HeldItemType.Empty; heldItem = null; hit.transform.GetComponent <EnemyAI>().Eat(1); } if (heldItemType == HeldItemType.RedFood) { Debug.Log("Fed " + hit.transform.name); redFoodHeld.gameObject.SetActive(false); heldItem.gameObject.SetActive(true); Destroy(heldItem.gameObject); heldItemType = HeldItemType.Empty; heldItem = null; hit.transform.GetComponent <EnemyAI>().Eat(2); } } else if (hit.transform.GetComponent <EnemyAI>().GetAllegiance() == EnemyAI.CreatureAllegiance.Hostile) { if (heldItemType == HeldItemType.GreenFood) { Debug.Log("Fed " + hit.transform.name); greenFoodHeld.gameObject.SetActive(false); heldItem.gameObject.SetActive(true); Destroy(heldItem.gameObject); heldItemType = HeldItemType.Empty; heldItem = null; hit.transform.GetComponent <EnemyAI>().Eat(3); } } } } }
/// <summary> /// Drops item if one is being held /// </summary> void PlayerReleaseItem() { if (Input.GetKeyDown(KeyCode.Q)) //Q key { if (heldItemType == HeldItemType.Ball) { ballHeld.gameObject.SetActive(false); heldItem.gameObject.SetActive(true); heldItem.transform.position = transform.position + transform.forward; heldItemType = HeldItemType.Empty; heldItem = null; } if (heldItemType == HeldItemType.Cube) { cubeHeld.gameObject.SetActive(false); heldItem.gameObject.SetActive(true); heldItem.transform.position = transform.position + transform.forward; heldItemType = HeldItemType.Empty; heldItem = null; } if (heldItemType == HeldItemType.Weapon1) { weapon1.gameObject.SetActive(false); Instantiate(gun1, transform.position + transform.forward, transform.rotation); heldItemType = HeldItemType.Empty; Destroy(heldItem.gameObject); } if (heldItemType == HeldItemType.BlueFood) { blueFoodHeld.gameObject.SetActive(false); heldItem.gameObject.SetActive(true); heldItem.transform.position = transform.position + transform.forward; heldItemType = HeldItemType.Empty; heldItem = null; } if (heldItemType == HeldItemType.GreenFood) { greenFoodHeld.gameObject.SetActive(false); heldItem.gameObject.SetActive(true); heldItem.transform.position = transform.position + transform.forward; heldItemType = HeldItemType.Empty; heldItem = null; } if (heldItemType == HeldItemType.RedFood) { redFoodHeld.gameObject.SetActive(false); heldItem.gameObject.SetActive(true); heldItem.transform.position = transform.position + transform.forward; heldItemType = HeldItemType.Empty; heldItem = null; } } }
// Start is called before the first frame update void Start() { rb = GetComponent <Rigidbody>(); ballHeld.gameObject.SetActive(false); cubeHeld.gameObject.SetActive(false); weapon1.gameObject.SetActive(false); blueFoodHeld.gameObject.SetActive(false); greenFoodHeld.gameObject.SetActive(false); redFoodHeld.gameObject.SetActive(false); heldItemType = HeldItemType.Empty; speed = defaultSpeed; Cursor.visible = false; Cursor.lockState = CursorLockMode.Locked; }
void SetHeldItemType() { switch (heldItem) { case "BOMB": heldItemType = HeldItemType.THROW; break; case "PT_R": heldItemType = HeldItemType.POTION; break; case "PT_O": heldItemType = HeldItemType.POTION; break; case "PT_V": heldItemType = HeldItemType.THROW_POTION; break; case "PT_G": heldItemType = HeldItemType.POTION; break; case "PT_B": heldItemType = HeldItemType.POTION; break; case "AR_R": heldItemType = HeldItemType.BOW; break; case "AR_H": heldItemType = HeldItemType.BOW; break; case "AR_F": heldItemType = HeldItemType.BOW; break; case "AR_P": heldItemType = HeldItemType.BOW; break; case "AR_M": heldItemType = HeldItemType.BOW; break; case "AF_H": heldItemType = HeldItemType.THROW_POTION; break; case "AF_F": heldItemType = HeldItemType.THROW_POTION; break; case "AF_A": heldItemType = HeldItemType.THROW_POTION; break; case "AF_P": heldItemType = HeldItemType.THROW; break; case "AF_M": heldItemType = HeldItemType.THROW; break; default: heldItemType = HeldItemType.NONE; break; } }
/// <summary> /// Detects and picks up items. /// </summary> void PlayerPickUp() { RaycastHit hit; //Checks if ray collided with an object if (Input.GetButtonDown("Fire2") && Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, interactDistance)) //Mouse2 button { if (hit.transform.tag == "Pickup" && heldItem == null) //Checks if object hit has tag "Pickup" and no object is currently being held by player { //Checks what item the player is picking up if (hit.transform.name == "Ball(Clone)") { heldItemType = HeldItemType.Ball; heldItem = hit.transform; hit.transform.gameObject.SetActive(false); ballHeld.GetComponent <Renderer>().material.color = heldItem.transform.GetComponent <Renderer>().material.color; ballHeld.gameObject.SetActive(true); } if (hit.transform.name == "Cube(Clone)") { heldItemType = HeldItemType.Cube; heldItem = hit.transform; hit.transform.gameObject.SetActive(false); cubeHeld.GetComponent <Renderer>().material.color = heldItem.transform.GetComponent <Renderer>().material.color; cubeHeld.gameObject.SetActive(true); } if (hit.transform.name == "BlueFood(Clone)") { heldItemType = HeldItemType.BlueFood; heldItem = hit.transform; hit.transform.gameObject.SetActive(false); blueFoodHeld.GetComponent <Renderer>().material.color = heldItem.transform.GetComponent <Renderer>().material.color; blueFoodHeld.gameObject.SetActive(true); } if (hit.transform.name == "GreenFood(Clone)") { heldItemType = HeldItemType.GreenFood; heldItem = hit.transform; hit.transform.gameObject.SetActive(false); greenFoodHeld.GetComponent <Renderer>().material.color = heldItem.transform.GetComponent <Renderer>().material.color; greenFoodHeld.gameObject.SetActive(true); } if (hit.transform.name == "RedFood(Clone)") { heldItemType = HeldItemType.RedFood; heldItem = hit.transform; hit.transform.gameObject.SetActive(false); redFoodHeld.GetComponent <Renderer>().material.color = heldItem.transform.GetComponent <Renderer>().material.color; redFoodHeld.gameObject.SetActive(true); } } if (hit.transform.tag == "Weapon" && heldItem == null) //Checks if object hit has tag "Pickup" and no object is currently being held by player { heldItemType = HeldItemType.Weapon1; heldItem = hit.transform; hit.transform.gameObject.SetActive(false); weapon1.gameObject.SetActive(true); } } }