Issue with character keeps walking after reaching location

Hi everyone,

I’ve been pulling my hair out for the last day trying to figure out why. I’ve got it to output the coordinates and the relative vector3 and they both look correct but the character just keeps walking past that point.

My code is largely unaltered from the example so I’m at a loss as to why the behaviour is misbehaving.

    using System;
    using UnityEngine;
    using UnityStandardAssets.Characters.ThirdPerson;

    [RequireComponent(typeof (ThirdPersonCharacter))]
    public class PlayerMovement : MonoBehaviour
    {
    [SerializeField]
    float walkMoveStopRadius = 0.2f;

    ThirdPersonCharacter m_Character;   // A reference to the ThirdPersonCharacter on the object
    CameraRaycaster cameraRaycaster;
    Vector3 currentClickTarget;
        
    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.hit.collider.gameObject.name.ToString());
            switch (cameraRaycaster.layerHit) {
                case Layer.Walkable:
                    currentClickTarget = cameraRaycaster.hit.point;  // So not set in default case
                    break;
                case Layer.Enemy:
                    break;
                default:
                    return;

            }

            var playerToClickPoint = currentClickTarget - transform.position;
            Debug.Log(playerToClickPoint);
            if (playerToClickPoint.magnitude >= walkMoveStopRadius)
            {
                m_Character.Move(playerToClickPoint, false, false);
            }
            else
            {
                m_Character.Move(Vector3.zero,false,false);
            }
        }
    }
    }

Hi,

You will find as you progress through the lectures you end up re-writing some of this code, and the ThirdPersonController, I would stick with it for the time being. :slight_smile:

Ah okay, thanks Rob!
It’s probably the wrong way to approach it but if I notice a bug I try and fix it immediately. If I’m still having issues later in the course I’ll report back.

Thanks for the reply!

1 Like

I wouldn’t say that’s the wrong approach at all, and I almost copy/pasted you a fix for a couple of the classes which would prevent some problems. If I did that though, your code now would be a bit different to that which you work through in the course and I would be taking a little something away from your journey.

I would suggest doing what you’ve already stated, e.g. stick with it for now, maybe make a note of the issue so that you don’t lose track/sight of it, but then see if anything improves this later in the course. If you are still experiencing any oddities later, pop up another post :slight_smile:

Hi Chris,

I just responded to someone else with a similar issue and reverted back to your post above. I note that you have the “move / stop” code within the if statement for the mouse click detection, where-as in the course it is outside of this if statement.

Try the following change;

    // Fixed update is called in sync with physics
    private void FixedUpdate()
    {
        if (Input.GetMouseButton(0))
        {
            print("Cursor raycast hit" + cameraRaycaster.hit.collider.gameObject.name.ToString());
            switch (cameraRaycaster.layerHit) {
                case Layer.Walkable:
                    currentClickTarget = cameraRaycaster.hit.point;  // So not set in default case
                    break;
                case Layer.Enemy:
                    break;
                default:
                    return;

            }
		}

		var playerToClickPoint = currentClickTarget - transform.position;
		Debug.Log(playerToClickPoint);
		if (playerToClickPoint.magnitude >= walkMoveStopRadius)
		{
			m_Character.Move(playerToClickPoint, false, false);
		}
		else
		{
			m_Character.Move(Vector3.zero,false,false);
		}
    }
1 Like

Privacy & Terms