Add project files.
This commit is contained in:
8
Assets/Scripts/Combat.meta
Normal file
8
Assets/Scripts/Combat.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 693a5171faa224c46a992c9c1ccee87d
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Scripts/Combat/CombatTarget.cs
Normal file
8
Assets/Scripts/Combat/CombatTarget.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using UnityEngine;
|
||||
using RPG.Combat;
|
||||
|
||||
public class CombatTarget : MonoBehaviour
|
||||
{
|
||||
// This class is intentionally left empty.
|
||||
// It serves as a marker to identify combat targets in the game.
|
||||
}
|
||||
2
Assets/Scripts/Combat/CombatTarget.cs.meta
Normal file
2
Assets/Scripts/Combat/CombatTarget.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b8654c3b7c55a404fbedd3d1b66415f2
|
||||
22
Assets/Scripts/Combat/Fighter.cs
Normal file
22
Assets/Scripts/Combat/Fighter.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using UnityEngine;
|
||||
using RPG.Movement;
|
||||
|
||||
namespace RPG.Combat
|
||||
{
|
||||
public class Fighter : MonoBehaviour
|
||||
{
|
||||
Transform target;
|
||||
private void Update()
|
||||
{
|
||||
if(target != null)
|
||||
{
|
||||
GetComponent<Mover>().MoveTo(target.position);
|
||||
target = null;
|
||||
}
|
||||
}
|
||||
public void Attack(CombatTarget combatTarget)
|
||||
{
|
||||
target = combatTarget.transform;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Combat/Fighter.cs.meta
Normal file
2
Assets/Scripts/Combat/Fighter.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0dac85c6432a11746b07d2f6a021e2e4
|
||||
8
Assets/Scripts/Control.meta
Normal file
8
Assets/Scripts/Control.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 04f126232ba345441abb1a3fe3ca806d
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
75
Assets/Scripts/Control/PlayerController.cs
Normal file
75
Assets/Scripts/Control/PlayerController.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using RPG.Movement;
|
||||
using RPG.Combat;
|
||||
using System;
|
||||
|
||||
namespace RPG.Control
|
||||
{
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
private PlayerControls playerControls;
|
||||
private InputAction moveAction;
|
||||
private InputAction interactAction;
|
||||
private Camera mainCamera;
|
||||
private void Awake()
|
||||
{
|
||||
playerControls = new PlayerControls();
|
||||
moveAction = playerControls.Player.Move;
|
||||
interactAction = playerControls.Player.Action;
|
||||
mainCamera = Camera.main;
|
||||
}
|
||||
private void OnEnable()
|
||||
{
|
||||
moveAction.Enable();
|
||||
interactAction.Enable();
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
moveAction.Disable();
|
||||
interactAction.Disable();
|
||||
}
|
||||
private void Update()
|
||||
{
|
||||
if (InteractWithCombat()) return;
|
||||
if (InteractWithMovement()) return;
|
||||
}
|
||||
|
||||
private bool InteractWithCombat()
|
||||
{
|
||||
RaycastHit[] hits = Physics.RaycastAll(GetMouseRay());
|
||||
foreach (RaycastHit hit in hits)
|
||||
{
|
||||
CombatTarget target = hit.transform.GetComponent<CombatTarget>();
|
||||
if (target == null) continue;
|
||||
if (interactAction.WasPressedThisFrame())
|
||||
{
|
||||
//MoveToCursor();
|
||||
GetComponent<Fighter>().Attack(target);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool InteractWithMovement()
|
||||
{
|
||||
RaycastHit hit;
|
||||
bool hasHit = Physics.Raycast(GetMouseRay(), out hit);
|
||||
if (hasHit)
|
||||
{
|
||||
if (moveAction.IsPressed())
|
||||
GetComponent<Mover>().MoveTo(hit.point);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private Ray GetMouseRay()
|
||||
{
|
||||
return mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Control/PlayerController.cs.meta
Normal file
2
Assets/Scripts/Control/PlayerController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a8218d52e9da57343b0e14cd0a1af897
|
||||
8
Assets/Scripts/Core.meta
Normal file
8
Assets/Scripts/Core.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2d6fe9a4109216047b65d5a73e659d70
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
14
Assets/Scripts/Core/FollowCamera.cs
Normal file
14
Assets/Scripts/Core/FollowCamera.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace RPG.Core
|
||||
{
|
||||
public class FollowCamera : MonoBehaviour
|
||||
{
|
||||
[SerializeField] Transform target;
|
||||
|
||||
void LateUpdate()
|
||||
{
|
||||
transform.position = target.position;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Core/FollowCamera.cs.meta
Normal file
2
Assets/Scripts/Core/FollowCamera.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bc35952d43d5ef444a4da8f0fdf4d488
|
||||
8
Assets/Scripts/Movement.meta
Normal file
8
Assets/Scripts/Movement.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 20e5e08225415394989e0b790f460a71
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
42
Assets/Scripts/Movement/Mover.cs
Normal file
42
Assets/Scripts/Movement/Mover.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace RPG.Movement
|
||||
{
|
||||
public class Mover : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Transform target;
|
||||
|
||||
// Cache components for performance
|
||||
private NavMeshAgent navMeshAgent;
|
||||
private Animator animator;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// Get component references once
|
||||
navMeshAgent = GetComponent<NavMeshAgent>();
|
||||
animator = GetComponent<Animator>();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
UpdateAnimator();
|
||||
}
|
||||
|
||||
public void MoveTo(Vector3 destination)
|
||||
{
|
||||
navMeshAgent.destination = destination;
|
||||
}
|
||||
|
||||
private void UpdateAnimator()
|
||||
{
|
||||
// This is your original code, which is correct!
|
||||
// It just uses the cached variables now.
|
||||
Vector3 velocity = navMeshAgent.velocity;
|
||||
Vector3 localVelocity = transform.InverseTransformDirection(velocity);
|
||||
float speed = localVelocity.z;
|
||||
animator.SetFloat("forwardSpeed", speed);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Movement/Mover.cs.meta
Normal file
2
Assets/Scripts/Movement/Mover.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 99048001b4ebc1543911415b71049d4a
|
||||
Reference in New Issue
Block a user