When I hit play in Unity, var hor
value is 0
, as it should be, then 1
when I press and hold Horizontal
’s positive key, as it should. Then -1
, as I press and hold Horizontal
’s negative key, as it should.
But hor
is not changing, it is 0
all the time.
screenshot of a text editor|690x442
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SimpleMovement : MonoBehaviour
{
public float speed = 5.0f;
private Rigidbody2D rigidBody2D;
void Start()
{
rigidBody2D = GetComponent<Rigidbody2D>();
}
void Update()
{
var hor = Input.GetAxis("Horizontal") * Time.deltaTime;
rigidBody2D.velocity = new Vector2(hor * speed, rigidBody2D.velocity.y);
Debug.Log(hor);
}
}