Lecture Ref: 5_IN_RPG
I have seen other people talking about how the player stands in the middle of the target, or it does not click on it. I am having both issues - and can’t figure out what I’m doing wrong. I’ve looked at all the coding and fixed it based on the Github Commits up until this point. I thought I heard somewhere where he had this issue too, but can’t find the video where he fixes it… Here is my code for each of the scripts because I don’t know where I’m going wrong. I have also attached a video that shows what is happening…
(Fighter Code)
using UnityEngine;
using RPG.Movement;
using RPG.Core;
namespace RPG.Combat
{
public class Fighter : MonoBehaviour
{
[SerializeField] float weaponRange = 2f;
Transform target;
private void Update()
{
if (target == null) return;
if (GetIsInRange())
{
GetComponent<Mover>().MoveTo(target.position);
}
else
{
GetComponent<Mover>().Stop();
}
}
private bool GetIsInRange()
{
return Vector3.Distance(transform.position, target.position) < weaponRange;
}
public void Attack(CombatTarget combatTarget)
{
GetComponent<ActionScheduler>().StartAction(this);
target = combatTarget.transform;
}
public void Cancel()
{
target = null;
}
}
}
(PlayerControl Code)
using RPG.Movement;
using RPG.Combat;
using UnityEngine;
namespace RPG.Control
{
public class PlayerController : MonoBehaviour
{
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 (Input.GetMouseButtonDown(0))
{
GetComponent<Fighter>().Attack(target);
}
return true;
}
return false;
}
private bool InteractWithMovement()
{
RaycastHit hit;
bool hasHit = Physics.Raycast(GetMouseRay(), out hit);
if (hasHit)
{
if (Input.GetMouseButton(0))
{
GetComponent<Mover>().StartMoveAction(hit.point);
}
return true;
}
return false;
}
private static Ray GetMouseRay()
{
return Camera.main.ScreenPointToRay(Input.mousePosition);
}
}
}
(CombatTarget - I know there is nothing in here, I can’t remember if we changed anything on this yet or not…)
using UnityEngine;
namespace RPG.Combat
{
public class CombatTarget :MonoBehaviour
{
}
}
(Mover Code)
using System.Collections;
using System.Collections.Generic;
using RPG.Combat;
using RPG.Core;
using UnityEngine;
using UnityEngine.AI;
namespace RPG.Movement
{
public class Mover : MonoBehaviour
{
[SerializeField] Transform target;
NavMeshAgent navMeshAgent;
private void Start()
{
navMeshAgent = GetComponent<NavMeshAgent>();
}
void Update()
{
UpdateAnimator();
}
public void StartMoveAction(Vector3 destination)
{
GetComponent<ActionScheduler>().StartAction(this);
GetComponent<Fighter>().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);
}
}
}