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