I’m trying to setup my Player Controller to only move in 4 directions instead of 8. I’ve looked for hours unable to find a solid solution for this. Is there an easy solution or is it going to involve a convoluted amount of boolean switches?
My current control script looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float playerSpeed;
private Animator anim;
private void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
DoMovement(); // Player Movement
}
void DoMovement()
{
if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
{
transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * playerSpeed * Time.deltaTime, 0f, 0f));
}
if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
{
transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * playerSpeed * Time.deltaTime, 0f));
}
//Tell The Animator What Direction The Player is Moving
anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
}
}
There area couple of ways to do this, one is to pick a “primary axis” if this axis is being used, input on the other is simply ignored, and if the primary axis has no input then the secondary axis can be used. This can be done with the following code. (NOTE: all code here assumes that you have your axis defined in InputManager with your desired dead zones and other settings).
public void Update()
{
Vector3 lMovementVector = Vector3.zero;
float lInput = Input.GetAxis("Horizontal");
if (lInput != 0)
{
// We have horizontal movement, set our movement vector.
lMovementVector = Vector3.right * lInput;
}
else
{
// No horizontal movement, check for verticalfloat lInput = Input.GetAxis("Horizontal");
lInput = Input.GetAxis("Vertical");
if (lInput != 0)
{
lMovementVector = Vector3.up * lInput;
}
}
// After all is said andone, apply the movement.
transform.Translate(lMovementVector * Time.deltaTime * playerSpeed);
}
An alternative approach is to consider whichever axis has the greater input magnitude as the primary axis (NOTE: due to the nature of having to deal with the case where both axis are perfectly equal, this does technically mean you still have a strictly defined primary axis, but in general works a bit more nicely I find than the previous approach where a tiny input on your primary axis can cut off a significant input on the secondary). Here is an example of this approach.
public void Update()
{
Vector3 lMovementVector = Vector3.zero;
float lInputHorizontal = Input.GetAxis("Horizontal");
float lInputVertical = Input.GetAxis("Vertical");
if (Mathf.Abs(lInputHorizontal) > Mathf.Abs(lInputVertical))
{
lMovementVector = Vector3.right * lInputHorizontal;
}
else
{
lMovementVector = Vector3.up * lInputVertical;
}
// After all is said andone, apply the movement.
transform.Translate(lMovementVector * Time.deltaTime * playerSpeed);
}
Hope this is useful and shows you enough to work out a solution that best suits your needs.