Time.deltaTime trouble
I'm trying to move a character by holding down the arrow keys, but it's not working. When I go without Time.deltaTime and just press the arrow keys it works fine. I want to move my character more precisely by holding down the arrow keys.
Help?
This is the code I'm using:
using UnityEngine;
using System.Collections;
public class Ctrl : MonoBehaviour { public float speed = 1.5f;
void Update ()
{
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.RightArrow))
{
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.UpArrow))
{
transform.position += Vector3.up * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.DownArrow))
{
transform.position += Vector3.down * speed * Time.deltaTime;
}
}
}
Answer by Namey5 · Apr 19, 2017 at 02:32 AM
Time.deltaTime isn't exactly necessary, it just ensures that your character will move an equal amount no matter the frame rate of you program. Because of this, values using Time.deltaTime are generally quite small, so it could just be that you need to greatly increase the speed at which you are moving.
Your answer
Follow this Question
Related Questions
How can I translate an object smoothly on the X Axis only? [C#] 1 Answer
How could I navigate horizontally through a scroll view using left and right arrow keys? 0 Answers
How do I move a game object only on the x axis with arrow keys? 1 Answer
disable/enable arrow keys with button,turn 0 Answers
WebGL: disable scrolling 1 Answer