Weird interaction with WASD and mouse movement

I’ve got the code working as per the video, but wanted to make the two systems run concurrently, without needing to toggle between modes. I’ve got code pinched from the standard asset that allows both controls to work simultaneously- using the keyboard controls overrides the mouse command, but for some reason when I have this set up, mouse movement will only make Ethan walk, not run anywhere. Anyone got any ideas? This is my movement script:

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

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

ThirdPersonCharacter m_Character;   // A reference to the ThirdPersonCharacter on the object
CameraRaycaster cameraRaycaster;
Vector3 currentClickTarget;
Layer layerClicked;
bool isWalkTarget = false;

private Transform m_Cam;                  // A reference to the main camera in the scenes transform
private Vector3 m_CamForward;             // The current forward direction of the camera
private Vector3 m_Move;
private bool m_Jump;                      // the world-relative desired move direction, calculated from the camForward and user input.
    
private void Start()
{
    cameraRaycaster = Camera.main.GetComponent<CameraRaycaster>();
	m_Character = GetComponent<ThirdPersonCharacter>(); // get the third person character ( this should never be null due to require component )
    currentClickTarget = transform.position;

	// get the transform of the main camera
        if (Camera.main != null)
        {
            m_Cam = Camera.main.transform;
        }
        else
        {
            Debug.LogWarning(
                "Warning: no main camera found. Third person character needs a Camera tagged \"MainCamera\", for camera-relative controls.", gameObject);
            // we use self-relative controls in this case, which probably isn't what the user wants, but hey, we warned them!
        }
}

private void Update()
    {
        if (!m_Jump)
        {
            m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
        }
    }


// Fixed update is called in sync with physics
private void FixedUpdate()
{
    ProcessIndirectMovement ();
    WASDControls();
}

void ProcessIndirectMovement ()
{
	if (Input.GetMouseButton (1)) {
		isWalkTarget = true;
		print ("Cursor raycast hit" + cameraRaycaster.layerHit);
		switch (cameraRaycaster.layerHit) {
		case (Layer.Walkable):
			currentClickTarget = cameraRaycaster.hit.point;
			break;
		case (Layer.Enemy):
			print ("Not moving to enemy");
			break;
		default:
			print ("unexpected layer found");
			return;
		}
	}

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

 
void WASDControls()
    {
        // read inputs
        float h = CrossPlatformInputManager.GetAxis("Horizontal");
        float v = CrossPlatformInputManager.GetAxis("Vertical");
        bool crouch = Input.GetKey(KeyCode.C);

        if (h !=0 || v != 0){
        	isWalkTarget = false;
        }

        // calculate move direction to pass to character
        if (m_Cam != null)
        {
            // calculate camera relative direction to move:
            m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized;
            m_Move = v*m_CamForward + h*m_Cam.right;
		}
        else
        {
            // we use world-relative directions in the case of no main camera
            m_Move = v*Vector3.forward + h*Vector3.right;
           
        }

#if !MOBILE_INPUT
// walk speed multiplier
if (Input.GetKey(KeyCode.LeftShift)) m_Move *= 0.5f;
#endif

        // pass all parameters to the character control script
        m_Character.Move(m_Move, crouch, m_Jump);
        m_Jump = false;
    }

}

Having pasted my code in it’s separating the last chunk out after the #if !MOBILE_INPUT statement. Could it be something to do with that?

Aaaand I’ve solved the problem. I was polling for when the keyboard took over, and thus cancelled mouse control too late in the cycle, so in effect I was calling Ethan to both move with the mouse control and also move 0 because the keyboard told him to stay still, meaning I guess he averaged it out?!

Functioning code below, in case anyone has the same issue:

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

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

ThirdPersonCharacter m_Character;   // A reference to the ThirdPersonCharacter on the object
CameraRaycaster cameraRaycaster;
Vector3 currentClickTarget;
Layer layerClicked;

private Transform m_Cam;                  // A reference to the main camera in the scenes transform
private Vector3 m_CamForward;             // The current forward direction of the camera
private Vector3 m_Move;
private bool m_Jump;                      // the world-relative desired move direction, calculated from the camForward and user input.
bool isInDirectMode = false;
      
private void Start()
{
    cameraRaycaster = Camera.main.GetComponent<CameraRaycaster>();
	m_Character = GetComponent<ThirdPersonCharacter>(); // get the third person character ( this should never be null due to require component )
    currentClickTarget = transform.position;

	// get the transform of the main camera
        if (Camera.main != null)
        {
            m_Cam = Camera.main.transform;
        }
        else
        {
            Debug.LogWarning(
                "Warning: no main camera found. Third person character needs a Camera tagged \"MainCamera\", for camera-relative controls.", gameObject);
            // we use self-relative controls in this case, which probably isn't what the user wants, but hey, we warned them!
        }
}

private void Update()
    {
        if (!m_Jump)
        {
            m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
        }
    }


// Fixed update is called in sync with physics
private void FixedUpdate()
{

	// read inputs
        float h = CrossPlatformInputManager.GetAxis("Horizontal");
       	float v = CrossPlatformInputManager.GetAxis("Vertical");
        bool crouch = Input.GetKey(KeyCode.C);

	if(h != 0 || v != 0) //G for gamepad  TODO allow player to set in menus
	{ isInDirectMode = true;}
	else {isInDirectMode = false;}

	if (isInDirectMode){
		currentClickTarget = transform.position;
		WASDControls(h, v, crouch);
	}
	else {
    ProcessMouseMovement ();
    }

}

void ProcessMouseMovement ()
{
	if (Input.GetMouseButton (1)) {
		print ("Cursor raycast hit" + cameraRaycaster.layerHit);
		switch (cameraRaycaster.layerHit) {
		case (Layer.Walkable):
			currentClickTarget = cameraRaycaster.hit.point;
			break;
		case (Layer.Enemy):
			print ("Not moving to enemy");
			break;
		default:
			print ("unexpected layer found");
			return;
		}
	}

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

 
void WASDControls(float h, float v, bool crouch)
    {         
        // calculate move direction to pass to character
        if (m_Cam != null)
        {
            // calculate camera relative direction to move:
            m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized;
            m_Move = v*m_CamForward + h*m_Cam.right;
		}

        if (Input.GetKey(KeyCode.LeftShift)) m_Move *= 0.5f;

        // pass all parameters to the character control script
        m_Character.Move(m_Move, crouch, m_Jump);
        m_Jump = false;
    }

}

Privacy & Terms