Ethan Only Walks When Point And Click Now What Happened

Hey everyone,

I am trying to figure out why my player object is no longer running when i click. The only way i can get it to run or sprint is if i hold down the mouse button and use the arrow keys or WASD.

The Movement Speed and Animation Speed are both set at 2 and he still walks slow.

I do notice that when i have the game running and i take off the Player Movement script and put it back on, i can get it to run again.

Or, if i click “Apply” to the prefab it will allow Ethan to run when i click somewhere on the scene.

Here is a copy of my code:

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

[RequireComponent(typeof(ThirdPersonCharacter))]
public class PlayerMovement : MonoBehaviour
{
    [SerializeField] float walkMoveStopRadius = 0.2f;
    [SerializeField] float attackMoveStopRadius = 5f;
  
    ThirdPersonCharacter thirdPersonCharacter;   //A reference to the ThirdPersonCharacter on the object -> was m_Character
    CameraRaycaster cameraRaycaster;
    Vector3 currentDestination, clickPoint;

   // bool isInDirectMode = false;    

    private void Start()
    {
        cameraRaycaster = Camera.main.GetComponent<CameraRaycaster>();
        thirdPersonCharacter = GetComponent<ThirdPersonCharacter>();
        currentDestination = transform.position;
    }
   
    //TODO: fix issue with click to move and WSAD conflicting and increasing speed --- SOLVED 
    //TODO: get rid of gamepad movement i.e. Controler -> Possibly Consider it for RPG game

    // Fixed update is called in sync with physics
    private void FixedUpdate()
    {
        /* //This is code for allowing gamepad -> Xbox Controler
         if (Input.GetKeyDown(KeyCode.G))    //G is for gamepad -> WSAD /TODO: allow player to map later
         {
             isInDirectMode = !isInDirectMode;   //toggle mode
             currentClickTarget = transform.position;   //Fixes bug when switching back adn forth from gamepad mode
         }                                              //Stops player from going back to spot where player movement was switched on
         if (isInDirectMode)
         {
             //ProcessDirectMovement(); -> Will add later
         }
         else
         {
             ProcessMouseMovement();
         }*/

        ProcessMouseMovement();
    }

    //DirectMovement Function For Controller
   /* private void ProcessDirectMovement()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("vertical");

        // calculate camera relative direction to move:
        
       Vector3 m_CamForward = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized;
       Vector3 m_Move = v * m_CamForward + h * Camera.main.transform.right;

       m_Character.Move(m_Move, false, false);   //Parameter was 

    }*/

    private void ProcessMouseMovement()
    {
        if (Input.GetMouseButton(0))
        {
            clickPoint = cameraRaycaster.hit.point;
            switch (cameraRaycaster.currentLayerHit)
            {
                case Layer.Walkable:
                    currentDestination = ShortDestination(clickPoint, walkMoveStopRadius);          
                    break;

                case Layer.Enemy:
                    currentDestination = ShortDestination(clickPoint, attackMoveStopRadius);
                    break;

                default:
                    print("Shouldn't Get Here");
                    return;
            }
        }

        WalkToDestination();
    }

    private void WalkToDestination()
    {
        var playerToClickPoint = currentDestination - transform.position;
        if (playerToClickPoint.magnitude >= 0) //Keep moving until you get to certain radius/Prevents Thirdperson object from running in circles
        {                                                       //This is the Point-Click go a certain amount of meters in a direction and stop
            thirdPersonCharacter.Move(playerToClickPoint, false, false);
        }
        else
        {
            thirdPersonCharacter.Move(Vector3.zero, false, false);
        }
    }

    Vector3 ShortDestination (Vector3 destination, float shortening)
    {
        Vector3 reductionVector = (destination - transform.position).normalized * shortening;
        return destination - reductionVector;
    }

    void OnDrawGizmos()
    {
        Gizmos.color = Color.black;
        Gizmos.DrawLine(transform.position, currentDestination);
        Gizmos.DrawSphere(currentDestination, 0.15F);
        Gizmos.DrawSphere(clickPoint, 0.1F);
    }
}

Try moving your ProcessMouseMovement() method call to Update() instead of FixedUpdate().

Yeah, that fixed it, but i am still having the WASD or Arrows conflicting
with the mouse point-and-click issue. The run speed is doubled when i use
the mouse and or WASD or arrow keys.

Have any idea why?

I thought that FixedUpdate syncs with the physics engine which prevented
this from happening?

1 Like

Hi,

Glad to hear that resolved the first issue.

I’ve linked to the Unity documentation below for their explanation of the differences between the two, but generally, if you are applying forces to rigidbodies then use FixedUpdate(), as you are moving the object yourself (e.g. physics isn’t) you can use Update().

If you ever find yourself in need of both then you need to consider the structure of your code and break it down into the respective methods/classes and call them appropriately from the relevant method above.

Regarding the conflict between keyboard control and mouse control

My suggestion would be to initially comment out all of the code which supports keyboard control for the movement, so you only have mouse control.

Now check to see if the run speed is still doubled, or whether it seems ok.

The swap it around, e.g. add the keyboard control code back in, and comment out the mouse control (obviously you’ll lose the attack at this point but that doesn’t really matter at this time).

Test the movement speed again, still doubled or ok?

Let me know what the outcome of the above is.


See also;

Privacy & Terms