public void SetCraftingMode(Crafter crafter) { if (currentInteractionState == interactionState.Normal) { craftingMenu.ToggleCraftingMenu(true, crafter); hud.ToggleHUD(true); Cursor.visible = true; Cursor.lockState = CursorLockMode.None; currentInteractionState = interactionState.CraftingMode; } else if (currentInteractionState == interactionState.CraftingMode) { craftingMenu.ToggleCraftingMenu(false, crafter); hud.ToggleHUD(false); Cursor.visible = false; Cursor.lockState = CursorLockMode.Locked; currentInteractionState = interactionState.Normal; } }
public void SetDialogueMode() { if (currentInteractionState == interactionState.Normal) { hud.ToggleHUD(true); canSpeak = false; Cursor.visible = true; Time.timeScale = 0; Cursor.lockState = CursorLockMode.None; currentInteractionState = interactionState.DialogueMode; } else if (currentInteractionState == interactionState.DialogueMode) { hud.ToggleHUD(false); Cursor.visible = false; Cursor.lockState = CursorLockMode.Locked; Time.timeScale = 1; currentInteractionState = interactionState.Normal; noDialogueTimer = 0.0f; } }
void SetInventoryMode(bool toggleOn) { if (toggleOn) { hud.ToggleHUD(true); inventoryDisplayer.ToggleInventoryMenu(true); Cursor.visible = true; Time.timeScale = 0; Cursor.lockState = CursorLockMode.None; currentInteractionState = interactionState.InventoryMode; } else if (!toggleOn) { hud.ToggleHUD(false); inventoryDisplayer.ToggleInventoryMenu(false); Cursor.visible = false; Cursor.lockState = CursorLockMode.Locked; Time.timeScale = 1; currentInteractionState = interactionState.Normal; } }
// Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.Escape)) { SetCursorState(CursorLockMode.None); } if (Input.GetButtonDown("Crafting")) { SetCraftingMode(playerCrafter); } switch (currentInteractionState) { case interactionState.Normal: if (Input.GetAxisRaw("Aim") == 1) { currentInteractionState = interactionState.Aiming; } if (Input.GetButtonDown("Inventory")) { SetInventoryMode(true); } if (FreeLook) { /* * * // only check on the X-Z plane: * Vector3 cameraDirection = new Vector3(playerCamera.transform.forward.x, 0f, playerCamera.transform.forward.z); * Vector3 playerDirection = new Vector3(transform.forward.x, 0f, transform.forward.z); * if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0) * { * if (Vector3.Angle(cameraDirection, playerDirection) > 15f) * { * Quaternion targetRotation = Quaternion.LookRotation(cameraDirection, transform.up); * * transform.rotation = Quaternion.RotateTowards(transform.rotation,targetRotation, turnSpeed * Time.deltaTime); * * } * } */ } if (FreeMove) { forwardMovement = transform.forward * Input.GetAxis("Vertical"); sidewaysMovement = transform.right * Input.GetAxis("Horizontal"); float cameraFacing = Camera.main.transform.eulerAngles.y; if (forwardMovement != Vector3.zero || sidewaysMovement != Vector3.zero) { float angle = Mathf.Atan2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")) * Mathf.Rad2Deg; animator.SetBool("Run", true); transform.rotation = Quaternion.AngleAxis(angle + cameraFacing, Vector3.up); transform.position += transform.forward * 10.0f * Time.deltaTime; } else { animator.SetBool("Run", false); } } if (Input.GetButtonDown("Dodge")) { combat.Dodge(forwardMovement, sidewaysMovement); } if (Input.GetButton("Block")) { combat.Block(true); } else { combat.Block(false); } if (Input.GetButtonDown("Interact")) { RaycastHit interact; Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 100.0f, Color.yellow); if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out interact, interactRange)) { if (interact.transform.gameObject.tag == "Item") { ItemContainer item = interact.transform.gameObject.GetComponent <ItemContainer>(); if (item.hitsToHarvest == 0) { inventory.AddItem(item.containedItem); Destroy(interact.transform.gameObject); } else { animator.SetTrigger("AxeSwing"); item.hitsToHarvest -= 1; } } if (interact.transform.gameObject.tag == "NPC") { if (canSpeak) { noDialogueTimer = 0.0f; interact.transform.gameObject.GetComponent <DialogueContainer>().Talk(); SetDialogueMode(); return; } } if (interact.transform.gameObject.tag == "Interactable") { Interactable interactable = interact.transform.gameObject.GetComponent <Interactable>(); Debug.Log("hit interact"); interactable.UseInteractable(gameObject); } } } if (!canSpeak) { if (noDialogueTimer == dialogueTimeOut) { dialogueTimeOut = +Time.deltaTime; } else { noDialogueTimer = 0; canSpeak = true; } } break; case interactionState.Aiming: if (Input.GetAxisRaw("Aim") == 0) { bowDrawn = false; aiming = false; FreeMove = true; currentInteractionState = interactionState.Normal; } else { crosshair.transform.LookAt(transform); aiming = true; FreeMove = false; animator.SetBool("Run", false); Vector3 cameraDirection = new Vector3(playerCamera.transform.forward.x, 0f, playerCamera.transform.forward.z); Vector3 playerDirection = new Vector3(transform.forward.x, 0f, transform.forward.z); forwardMovement = transform.forward * Input.GetAxis("Vertical"); sidewaysMovement = transform.right * Input.GetAxis("Horizontal"); Vector3 nextMovePos = (forwardMovement + sidewaysMovement) * Time.deltaTime * combat.currentMovementSpeed; transform.position += nextMovePos; if (Vector3.Angle(cameraDirection, playerDirection) > 0.5f) { Quaternion targetRotation = Quaternion.LookRotation(cameraDirection, transform.up); transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, turnSpeed * Time.deltaTime); } if (Input.GetAxis("PullBack") > 0) { if (bowPowerModifer < maxBowPower) { bowPowerModifer += (Time.deltaTime * bowPullBackRate); } else { bowPowerModifer = maxBowPower; } bowDrawn = true; } if (bowDrawn == true) { if (Input.GetAxisRaw("PullBack") == 0) { if (inventory.RemoveItem(arrow)) { bowDrawn = false; // GameObject arrow = Instantiate(arrowPrefab, bowPosition.position, playerCamera.transform.rotation); GameObject arrow = ObjectPooler.PoolerInstance.GetPooledObject("Arrow"); arrow.SetActive(true); arrow.transform.position = bowPosition.position; arrow.transform.rotation = playerCamera.transform.rotation; // GameObject arrow = Instantiate(arrowPrefab, bowPosition.position, playerCamera.transform.rotation); Rigidbody arrowrb = arrow.GetComponent <Rigidbody>(); arrowrb.gameObject.GetComponent <Arrow>().damage = arrowDamage; arrowrb.isKinematic = false; arrowrb.AddForce(playerCamera.transform.forward * (shotForce + bowPowerModifer), ForceMode.Impulse); arrow.GetComponent <Rigidbody>().AddForce(playerCamera.transform.forward * (shotForce + bowPowerModifer), ForceMode.Impulse); bowPowerTimer = 0; bowPowerModifer = 0; } } } } break; case interactionState.DialogueMode: break; case interactionState.InventoryMode: if (Input.GetButtonDown("Inventory")) { SetInventoryMode(false); } break; case interactionState.CraftingMode: if (Input.GetButtonDown("Inventory")) { SetInventoryMode(false); } break; } }
// Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.Escape)) { SetCursorState(CursorLockMode.None); } if (Input.GetButtonDown("Inventory")) { inventory.ToggleInventoryMenu(); } switch (currentInteractionState) { case interactionState.Normal: if (FreeMove) { if (Input.GetAxis("Vertical") != 0) { transform.position += movepos + transform.forward * Input.GetAxis("Vertical") * MovementSpeed * Time.deltaTime; } if (Input.GetAxis("Horizontal") != 0) { transform.position += movepos + transform.right * Input.GetAxis("Horizontal") * MovementSpeed * Time.deltaTime; } } if (FreeLook) { if (Input.GetAxis("Mouse X") != 0) { Debug.Log("rot"); rot = Mathf.Lerp(StartRotateSpeed, MaxRotateSpeed, Step * Time.deltaTime); } transform.Rotate(0, rot * Input.GetAxis("Mouse X"), 0); } if (Input.GetButtonDown("Attack")) { RaycastHit hit; Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 100.0f, Color.yellow); if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity)) { if (hit.transform.gameObject.tag == "NPC") { receivedDialogue = hit.transform.gameObject.GetComponent <DialogueContainer>().dialogue; currentInteractionState = interactionState.DialogueMode; dialogueIndex = 0; return; } if (hit.transform.gameObject.tag == "Enemy") { if (equipmentHolder.equipedWeapon != null) { hit.transform.gameObject.GetComponent <Stats>().DoDamage(equipmentHolder.equipedWeapon.attackDamage); Debug.Log("ATTACK!"); } else { Debug.Log("No Weapon Equiped"); } } } } break; case interactionState.DialogueMode: dialogueDisplay.DisplayDialogue(receivedDialogue[dialogueIndex]); if (Input.GetButtonDown("Attack")) { if (dialogueIndex + 1 < receivedDialogue.Count) { dialogueIndex++; dialogueDisplay.isDisplayingDialogue = false; } else { dialogueDisplay.FinishDisplayingDialogue(); currentInteractionState = interactionState.Normal; } } break; case interactionState.InventoryMode: break; } }