Holding Click instead of Clicking

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);
            }
        }
    }
}

It took me a moment to realize this was from the old version of the Core Combat course. I’m going to have to call in the big guns on this, although I strongly recommend heading to the newer Core Combat course, as we have a much more logical way of handling movement.

1 Like

Seems like you just got some brackets wrong. You only call m_Character.move within the if statement. So it will only move the character when you are holding the mouse button. Move it out of the statement and you should be fine.

1 Like

God, do I feel stupid now. Thank you for the quick answer, Sam!

Not stupid, these sorts of things happen every day, even with the most experienced of programmers. I looked at that bit of code several times and didn’t catch the error.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms