- Home /
Removing The Slide From Transform.Translate
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);
}
}
Answer by Lockstep · May 29, 2013 at 01:32 PM
The reason for this is Input.GetAxis. It smoothes out the input from the keys. Try Debug.Log(Input.GetAxis); in Update to see what I mean.
To avoid this you can use Input.GetAxisRaw instead. This will return 1 iff the possitive button is pressed -1 iff the negative button is pressed and 0 iff neither is pressed.
Also you shouldn't handle the input in FixedUpdate if you have the rest of your calculation in Update. Fixed Update is used for physics.
This works perfectly! Thanks :) and I'm also noting the FixedUpdate and Update.
Your answer