I’m trying to have a simple cube move across the screen vertically and horizontally but at the same time diagonally. I got all of this down with a simple transform.Translate but my problem is that I have a “slide” when I go in one direction then switch to another for example: I got up and decide to go left, I slide up slightly before I fully stop.
using UnityEngine;
using System.Collections;
public class PlayerControls : MonoBehaviour {
public GameObject player;
public float speed = 10.0f;
public float transH;
public float transV;
void Update()
{
PlayerProperties playerProp = GetComponent<PlayerProperties>();
if(transV >= 0.001f && transH == 0)
{
playerProp.playerState = PlayerProperties.PlayerState.Up;
playerProp.changePlayer = true;
}
if (transV <= -0.001f && transH == 0)
{
playerProp.playerState = PlayerProperties.PlayerState.Down;
playerProp.changePlayer = true;
}
if (transV == 0 && transH <= -0.001f)
{
playerProp.playerState = PlayerProperties.PlayerState.Left;
playerProp.changePlayer = true;
}
if (transV == 0 && transH >= 0.001f)
{
playerProp.playerState = PlayerProperties.PlayerState.Right;
playerProp.changePlayer = true;
}
if (transV >= 0.001f && transH >= 0.001f)
{
playerProp.playerState = PlayerProperties.PlayerState.UpRight;
}
}
void FixedUpdate()
{
transV = Input.GetAxis("Vertical") * speed * Time.deltaTime;
transH = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
transform.Translate(transH, transV, 0);
}
}