Draft Dialogue System and Started Quest System

This commit is contained in:
SHOUTING_PIRATE
2025-05-07 14:43:43 +01:00
parent 8fb8da882d
commit 4e0bc0a573
503 changed files with 13767 additions and 281708 deletions

View File

@@ -1,7 +1,9 @@
 using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
using Cinemachine;
using TMPro;
/* Note: animations are called via the controller for both the character and capsule using animator null checks
*/
@@ -14,6 +16,10 @@ namespace StarterAssets
#endif
public class ThirdPersonController : MonoBehaviour
{
[Header("Popup")]
[SerializeField] private TMP_Text _popUpText;
private Transform _currentTarget;
private Transform _textTransform;
[Header("Animation")]
[Tooltip("Animator component reference")]
@@ -96,6 +102,13 @@ namespace StarterAssets
private float _jumpTimeoutDelta;
private float _fallTimeoutDelta;
// character height
private float _characterHeight;
// camera rotation
private float CurrentTargetRotation;
private float CurrentCameraRotation; // Added declaration for CurrentCameraRotation
// animation IDs
private int _animIDSpeed;
private int _animIDGrounded;
@@ -103,8 +116,6 @@ namespace StarterAssets
private int _animIDFreeFall;
private int _animIDMotionSpeed;
private Transform _currentTarget; // Track the currently targeted object
#if ENABLE_INPUT_SYSTEM
private PlayerInput _playerInput;
#endif
@@ -132,11 +143,54 @@ namespace StarterAssets
private void Awake()
{
// get a reference to our main camera
_controller = GetComponent<CharacterController>();
// Get the main camera reference
_mainCamera = Camera.main?.gameObject;
if (_mainCamera == null)
{
_mainCamera = GameObject.FindGameObjectWithTag("MainCamera");
Debug.LogError("ThirdPersonController: Main camera not found!");
return;
}
// Initialize camera rotation values
_cinemachineTargetYaw = _mainCamera.transform.rotation.eulerAngles.y;
_cinemachineTargetPitch = _mainCamera.transform.localRotation.eulerAngles.x;
_cinemachineTargetYaw = _cinemachineTargetYaw % 360f;
_cinemachineTargetPitch = _cinemachineTargetPitch % 360f;
_cinemachineTargetYaw = (_cinemachineTargetYaw > 180f) ? _cinemachineTargetYaw - 360f : _cinemachineTargetYaw;
_cinemachineTargetPitch = (_cinemachineTargetPitch > 180f) ? _cinemachineTargetPitch - 360f : _cinemachineTargetPitch;
_cinemachineTargetYaw = Mathf.Clamp(_cinemachineTargetYaw, BottomClamp, TopClamp);
_cinemachineTargetPitch = Mathf.Clamp(_cinemachineTargetPitch, BottomClamp, TopClamp);
_cinemachineTargetYaw = Mathf.Abs(_cinemachineTargetYaw) > 90f ? 90f : _cinemachineTargetYaw;
_cinemachineTargetPitch = Mathf.Abs(_cinemachineTargetPitch) > 90f ? 90f : _cinemachineTargetPitch;
// Initialize current rotation values
CurrentTargetRotation = _cinemachineTargetYaw;
CurrentCameraRotation = _cinemachineTargetPitch;
// Set initial target rotation
_targetRotation = transform.eulerAngles.y;
// Initialize animator if present
if (_hasAnimator)
{
_animator = GetComponent<Animator>();
}
_targetRotation = transform.eulerAngles.y;
// Initialize character state
_animationBlend = 0f;
_verticalVelocity = 0f;
_characterHeight = _controller.height;
// Initialize popup fields
if (_popUpText == null)
{
Debug.LogError("ThirdPersonController: TMP_Text component not assigned!");
return;
}
_textTransform = _popUpText.transform;
}
private void Start()
@@ -144,7 +198,6 @@ namespace StarterAssets
_cinemachineTargetYaw = CinemachineCameraTarget.transform.rotation.eulerAngles.y;
_hasAnimator = TryGetComponent(out _animator);
_controller = GetComponent<CharacterController>();
_input = GetComponent<StarterAssetsInputs>();
#if ENABLE_INPUT_SYSTEM
_playerInput = GetComponent<PlayerInput>();
@@ -189,6 +242,11 @@ namespace StarterAssets
PlayerKick(); // Call the kick function when the F button is pressed
}
if (Input.GetKeyUp(KeyCode.E))
{
PlayerInteract();
}
}
private void LateUpdate()
@@ -244,24 +302,47 @@ namespace StarterAssets
private void CheckForObjectInformation()
{
// Define the sphere cast starting point and direction
Vector3 origin = transform.position;
Vector3 direction = transform.forward;
float sphereRadius = 0.5f; // Adjust the radius as needed
float maxDistance = 4.0f; // Adjust the distance as needed
RaycastHit hit;
var origin = transform.position;
var direction = transform.forward;
const float sphereRadius = 1f;
const float maxDistance = 4.0f;
var hit = new RaycastHit();
// Perform the sphere cast
if (Physics.SphereCast(origin, sphereRadius, direction, out hit, maxDistance))
{
// Check if the hit object has the ObjectInformation component
ObjectInformation objectInfo = hit.collider.GetComponent<ObjectInformation>();
if (objectInfo != null)
var objectInfo = hit.collider.GetComponent<ObjectInformation>();
var npc = hit.collider.GetComponent<NPC>();
if (objectInfo != null || npc != null)
{
// If the player is looking at a new object, update the pop-up
if (_currentTarget != hit.transform)
{
_currentTarget = hit.transform; // Update the current target
PopUpManager.instance.ShowPopUp(objectInfo.objectName, objectInfo.objectDescription, hit.transform);
_currentTarget = hit.transform;
_popUpText.text = objectInfo != null
? $"{objectInfo.objectName}\n{objectInfo.objectDescription}"
: "[E] Talk to " + npc.npcName;
_popUpText.gameObject.SetActive(true);
// Update popup position
Vector3 tempPosition = _currentTarget.position;
tempPosition.y += 1f;
tempPosition.z += 0.2f;
_textTransform.position = tempPosition;
// Make the pop-up face the camera
var mainCamera = _mainCamera;
if (mainCamera != null)
{
// Get the direction from the target to the camera
Vector3 directionToCamera = mainCamera.transform.position - _currentTarget.position;
// Create rotation that faces the camera
Quaternion targetRotation = Quaternion.LookRotation(directionToCamera, Vector3.up);
// Adjust the rotation to face the camera properly
targetRotation = Quaternion.Euler(0, targetRotation.eulerAngles.y + 180f, 0);
_textTransform.rotation = targetRotation;
}
}
}
}
@@ -270,8 +351,8 @@ namespace StarterAssets
// If the sphere cast doesn't hit anything, hide the pop-up
if (_currentTarget != null)
{
_currentTarget = null; // Clear the current target
PopUpManager.instance.gameObject.SetActive(false); // Hide the pop-up
_currentTarget = null;
_popUpText.gameObject.SetActive(false);
}
}
}
@@ -467,5 +548,37 @@ namespace StarterAssets
{
Animator.SetTrigger("Kick");
}
public void PlayerInteract()
{
// Create a RaySphere from the player's position
Vector3 sphereCenter = transform.position + Vector3.up * 1f; // Slightly above the player
float sphereRadius = 2f;
// Find all NPCs within the sphere
Collider[] hits = Physics.OverlapSphere(sphereCenter, sphereRadius);
NPC closestNPC = null;
float closestDistance = float.MaxValue;
foreach (Collider hit in hits)
{
NPC npc = hit.GetComponent<NPC>();
if (npc != null && npc.canTalk && npc.isInteractable)
{
float distance = Vector3.Distance(transform.position, npc.transform.position);
if (distance < closestDistance)
{
closestDistance = distance;
closestNPC = npc;
}
}
}
// If we found an NPC, interact with it
if (closestNPC != null)
{
closestNPC.Interact();
}
}
}
}