I’m currently taking the RPG core combat course, and since I implemented the logic we were taught in lesson 29, my player no longer constantly follows the mouse, it casts a ray to the mouse location and starts rotating toward it but doesn’t move there until I release the mouse button. I am currently stuck and have no idea what to do so any help at all will be greatly appreciated. Here is my code below:
PlayerController;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Movement;
using RPG.Combat;
namespace RPG.Control
{
public class PlayerController : MonoBehaviour
{
void Start()
{
}
void Update()
{
if(ApplyCombat()) return;
if (ApplyMovement()) return;
print("Nowhere to go");
}
private bool ApplyCombat()
{
RaycastHit[] hits = Physics.RaycastAll(GetMouseRay());
foreach (RaycastHit hit in hits)
{
AttackTarget target = hit.transform.GetComponent<AttackTarget>();
if (target == null) continue;
if (Input.GetMouseButtonDown(0))
{
GetComponent<Fighting>().Attack(target);
}
return true;
}
return false;
}
private bool ApplyMovement()
{
RaycastHit hitInfo;
bool hasHit = Physics.Raycast(GetMouseRay(), out hitInfo);
if (hasHit)
{
if (Input.GetMouseButton(0))
{
GetComponent<PlayerMovement>().StartMovementAction(hitInfo.point);
}
return true;
}
return false;
}
public static Ray GetMouseRay()
{
return Camera.main.ScreenPointToRay(Input.mousePosition);
}
}
}
Mover;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using RPG.Combat;
using RPG.Core;
namespace RPG.Movement
{
public class PlayerMovement : MonoBehaviour
{
NavMeshAgent _navMeshAgent;
void Start()
{
_navMeshAgent = GetComponent<NavMeshAgent>();
}
void Update()
{
UpdateAnimator();
}
public void StartMovementAction(Vector3 destination)
{
GetComponent<ActionScheduler>().StartMovementAction(this);
GetComponent<Fighting>().Cancel();
MoveTo(destination);
}
public void MoveTo(Vector3 destination)
{
_navMeshAgent.destination = destination;
_navMeshAgent.isStopped = false;
}
public void Stop()
{
_navMeshAgent.isStopped = true;
}
private void UpdateAnimator()
{
Vector3 velocity = _navMeshAgent.velocity;
Vector3 localVelocity = transform.InverseTransformDirection(velocity);
float speed = localVelocity.z;
GetComponent<Animator>().SetFloat("forwardSpeed", speed);
}
}
}
Fighter;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Movement;
using RPG.Core;
namespace RPG.Combat
{
public class Fighting : MonoBehaviour
{
Transform target;
private PlayerMovement playerMovement;
void Update()
{
if(target == null) return;
playerMovement = GetComponent<PlayerMovement>();
if (!GetIsInRange())
{
playerMovement.MoveTo(target.position);
}
else
{
playerMovement.Stop();
}
}
public void Attack(AttackTarget attackTarget)
{
GetComponent<ActionScheduler>().StartMovementAction(this);
target = attackTarget.transform;
}
public void Cancel()
{
target = null;
}
private bool GetIsInRange()
{
return Vector3.Distance(transform.position, target.position) <= 2f;
}
}
}
Thanks in advance