An alternative to G key for direct mode

I decided not to make the game modal in this sense. How?

In the PlayerMovement script, I added a new private field

bool arrived = true;

then in the case statement for Walklable, set it to false, meaning, time to start walking.

 case Layer.Walkable:
                    currentClickTarget = cameraRaycaster.hit.point;
                    arrived = false;
                    break;

Finally, the end of the FixedUpdate is:

 if (!arrived) {
            Vector3 playerToClickPoint = currentClickTarget - transform.position;
            if (playerToClickPoint.magnitude > walkMoveStopRadius) {
                m_Character.Move(currentClickTarget - transform.position, false, false);
            } else {
                arrived = true;
            }
        }

so I don’t even walk unless I have not arrived, but once I get there, set “arrived” to true. Also, I do not disable the Third Person User Control script.

This means WASD works if I have arrived.

My complete PlayerMovement script thus looks like:

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;

    bool arrived = true;
        
    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))
        {
            switch (cameraRaycaster.layerHit) {
                case Layer.Walkable:
                    currentClickTarget = cameraRaycaster.hit.point;
                    arrived = false;
                    break;
                case Layer.Enemy:
                    Debug.Log("Enemy clicked....");
                    break;
                default:
                    Debug.LogError("Unhandled click layer: " + cameraRaycaster.layerHit);
                    break;
            }
            print("Cursor raycast hit" + cameraRaycaster.hit.collider.gameObject.name.ToString());
        }
        if (!arrived) {
            Vector3 playerToClickPoint = currentClickTarget - transform.position;
            if (playerToClickPoint.magnitude > walkMoveStopRadius) {
                m_Character.Move(currentClickTarget - transform.position, false, false);
            } else {
                arrived = true;
            }
        }
    }
}
2 Likes

Privacy & Terms