kuba4g8
kuba4g8

Reputation: 1

Why my object is moving left and up without me clicking anythink?

my character goes left and up without my interference and when i click d and s it goes left and down. I saw this is not mine fault couse i debug.log all my buttons clicked.

public float moveSpeed = 0.1f;

    public void Update()
    {
        //not inportant
        foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
        {
            if (Input.GetKey(vKey))
            {

                Debug.Log(vKey.ToString());

            }
        }

        if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)
        {
            float horizontalMovement = Input.GetAxisRaw("Horizontal") * moveSpeed;
            float verticalMovement = Input.GetAxisRaw("Vertical") * moveSpeed;

            Vector3 directionOfMovement = new Vector3(horizontalMovement, verticalMovement, 0);

            gameObject.transform.Translate(directionOfMovement);
        }

Upvotes: 0

Views: 49

Answers (1)

Ian Pilipski
Ian Pilipski

Reputation: 535

If using a controller, or a controller is plugged into your device. You should use a dead zone for the RawInput, because the controller may not center to zero, and could have some small value for the RawAxis.

try changing your conditional to have something like:

float rawX = Input.GetAxisRaw("Horizontal");
float rawY = Input.GetAxisRaw("Vertical");

if(Mathf.Abs(rawX) > 0.05f || Mathf.Abs(rawY) > 0.05f) {
    ...
}

Upvotes: 1

Related Questions