Instead of having to click a location to walk towards it, I have to hold mouse button 0, which is not what I’d like to have, as the camera moves along with the character, meaning that I’d have to adjust my pointer as Ethan gets closer to where he should be.
I’ve checked and compared both scripts (mine and yours) multiple times, and it seems both are identical, yet still, whenever I click at a point, Ethan moves a tiny bit and stops. Tried replacing Input.GetMouseButton(0)
to Input.GetMouseButtonDown(0)
, and instead, and instead, Ethan doesn’t even complete a full step.
I assume my issue here is that, instead of setting currentClickTarget
and maintaining it until I click on another location, it turns null when my input is release (as if it had another if statement for Input.GetMouseButtonUp(0)
). Been trying to solve that for at least an hour now, which has lead to me ask for help here, even thought I’m certain the Udemy course is too outdated for you to even bother. If not, thanks in advance.
My PlayerMovement
code for inspection:
> using System;
> using UnityEngine;
> using UnityStandardAssets.Characters.ThirdPerson;
>
> [RequireComponent(typeof (ThirdPersonCharacter))]
> public class PlayerMovement : MonoBehaviour
> {
> ThirdPersonCharacter m_Character; // A reference to the ThirdPersonCharacter on the object
> CameraRaycaster cameraRaycaster;
> Vector3 currentClickTarget;
> [SerializeField] float walkMoveStopRadius = 0.2f;
private void Start()
{
cameraRaycaster = Camera.main.GetComponent<CameraRaycaster>();
m_Character = GetComponent<ThirdPersonCharacter>();
currentClickTarget = transform.position;
}
private void Start()
{
cameraRaycaster = Camera.main.GetComponent<CameraRaycaster>();
m_Character = GetComponent<ThirdPersonCharacter>();
currentClickTarget = transform.position;
}
// Fixed update is called in sync with physics
private void FixedUpdate()
{
if (Input.GetMouseButton(0))
{
print("Cursor raycast hit: " + cameraRaycaster.layerHit);
switch (cameraRaycaster.layerHit)
{
case Layer.Walkable:
currentClickTarget = cameraRaycaster.hit.point; // So not set in default case
m_Character.Move(currentClickTarget - transform.position, false, false);
break;
case Layer.Enemy:
print("Not moving towards " + cameraRaycaster.layerHit.ToString());
break;
default:
print("Unexpected layer found");
break;
}
var playerToClickPoint = currentClickTarget - transform.position;
if (playerToClickPoint.magnitude >= walkMoveStopRadius)
{
m_Character.Move(playerToClickPoint, false, false);
}
else
{
m_Character.Move(Vector3.zero, false, false);
}
}
}
}