I’m using an asset which has a locomotion script to move the player. Unfortunately this script works the way that the player is walking in the direction where the mouse is pointing. I would like to change it so that I can use it without the mouse. The left and right arrow keys shall give the direction instead. But for some reason I don’t manage that.
This is the original script
using System.Collections;
using System.Collections.Generic;
[RequireComponent(typeof(Animator))]
//Name of class must be name of file as well
public class LocomotionPlayer : MonoBehaviour
{
private Transform mountPoint;
private Vector3 moveDirection = Vector3.zero;//player's move direction
protected Animator _animator;
private CharacterController _charCtrl;
private bool _jump;//jump button press detection
public bool _run;
public bool canRotate = true;
public bool holding;
public bool alive = true;
public float _speed;
public float _strafe;
public float gravity;//gravity force
public float health;
private float _mouseX;
private float tapTimeWindow;
public int tapCount;
// Use this for initialization
void Start ()
{
_animator = GetComponent<Animator>();
_charCtrl = GetComponent<CharacterController>();
m_AudioSource = GetComponent<AudioSource>();
}
void Update ()
{
_mouseX = Input.GetAxis("Mouse X");
_speed = Input.GetAxis("Vertical");//reading vertical axis input
_strafe = Input.GetAxis("Horizontal");//reading horizontal axis input
_run = Input.GetKey(KeyCode.LeftShift) ? true : false;//check if run button was pressed
//PROCESSING ROTATION
Vector3 aimPoint = Camera.main.transform.forward * 10f;
if (canRotate)
{
Quaternion targetRotation = Quaternion.LookRotation(aimPoint);
this.transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 4 * Time.deltaTime);
this.transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
}
if ((_charCtrl != null) && (!swimming))
{ //is character controller enabled
if (_charCtrl.isGrounded)
{
moveDirection.y = 0;
}
}
}
void FixedUpdate ()
{
_animator.SetFloat("mouseX", _mouseX, 0.3f, Time.deltaTime);
_animator.SetFloat("Speed", _speed);
_animator.SetFloat("Strafe", _strafe);
_animator.SetBool("Run", _run);
_animator.SetBool("JumpOverObstcle", _jumpOverObstcle);
_animator.SetBool("holding", holding);
_animator.SetInteger("SwingCount", tapCount);
}
}
I changed it to:
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
[RequireComponent(typeof(Animator))]
//Name of class must be name of file as well
public class LocomotionPlayer : MonoBehaviour
{
private Transform mountPoint;
private Vector3 moveDirection = Vector3.zero;
protected Animator _animator;
private CharacterController _charCtrl;
private bool _jump;
public bool _run;
public bool canRotate = true;
public bool holding;
public bool alive = true;
public float _speed;
public float _strafe;
public float gravity;
public float health;
private float _mouseX;
private float tapTimeWindow;
public int tapCount;
// Use this for initialization
void Start ()
{
_animator = GetComponent<Animator>();
_charCtrl = GetComponent<CharacterController>();
m_AudioSource = GetComponent<AudioSource>();
}
void Update ()
{
_mouseX = Input.GetAxis("Horizontal"); // -->> Changed here
_speed = Input.GetAxis("Vertical");
_strafe = Input.GetAxis("Horizontal");
_run = Input.GetKey(KeyCode.LeftShift) ? true : false;
//PROCESSING ROTATION
Vector3 aimPoint = transform.forward * 10f; // -->> Changed here and removed if statement
Quaternion targetRotation = Quaternion.LookRotation(aimPoint);
this.transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 4 * Time.deltaTime);
this.transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
if ((_charCtrl != null) && (!swimming))
{ //is character controller enabled
if (_charCtrl.isGrounded)
{
moveDirection.y = 0;
}
}
}
void FixedUpdate ()
{
_animator.SetFloat("mouseX", _mouseX, 0.3f, Time.deltaTime);
_animator.SetFloat("Speed", _speed);
_animator.SetFloat("Strafe", _strafe);
_animator.SetBool("Run", _run);
_animator.SetBool("JumpOverObstcle", _jumpOverObstcle);
_animator.SetBool("holding", holding);
_animator.SetInteger("SwingCount", tapCount);
}
}
Now my player is walking left and right when I press the left and right arrow keys but he isn’t doing it while he is walking forward, he only does it while he stands still. What do I do wrong?