When I move my character, he immediately almost snaps to look in a different direction to where I’ve moved him to. While this doesnt necessarily affect the game this far, I want to address this problem. It doesn’t seem to happen in the videos, and maybe the problem was fixed and I missed it?
Here’s my player movement script, if that helps:
{
using System;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
[RequireComponent(typeof(ThirdPersonCharacter))]
public class PlayerMovement : MonoBehaviour
{
[SerializeField] float walkMoveStopRadius = .2f;
ThirdPersonCharacter m_Character; // A reference to the ThirdPersonCharacter on the object
CameraRaycaster cameraRaycaster;
Vector3 currentClickTarget;
bool isInDirectMode = false; // TODO consider making static later
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.GetKeyDown(KeyCode.G)) // G for gamepad TODO allow player to map later
{
isInDirectMode = !isInDirectMode;
currentClickTarget = transform.position;
}
if (isInDirectMode)
{
ProcessDirectMovement();
}
else
{
ProcessMouseMovement();
}
}
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);
}
private void ProcessMouseMovement()
{
if (Input.GetMouseButton(0))
{
switch (cameraRaycaster.layerHit)
{
case Layer.Walkable:
currentClickTarget = cameraRaycaster.hit.point; // So not set in default case
break;
case Layer.Enemy:
print("Not going towards enemy");
break;
default:
print("shouldn't be here!");
return;
}
}
var playerToClickPoint = currentClickTarget - transform.position;
if (playerToClickPoint.magnitude >= walkMoveStopRadius)
{
m_Character.Move(playerToClickPoint, false, false);
}
else
{
m_Character.Move(Vector3.zero, false, false);
}
}
}