This is my code for the controller that my character uses. I only want him to move left and right, I want him to move at a decently slow speed. This code works in that it makes my character move, however, there is a problem.
The problem is my character moves at a ridiculous speed.
Is there a way to make him slower?
Code is below:
using UnityEngine;
using System.Collections;
public class playerController : MonoBehaviour
{
private Animator animator;
// Use this for initialization
void Start()
{
animator = this.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
this.transform.Translate(Input.GetAxis("Horizontal"),0,0);
var horizontal = Input.GetAxis("Horizontal");
if (horizontal > 0)
{
animator.SetInteger("Direction", 1);
}
else if (horizontal < 0)
{
animator.SetInteger("Direction", 3);
}
}
}
I just included a speed variable to multiply your input by.
I also included Time.deltaTime because the way you currently have it, your speed would increase as your framerate increases. So someone running your game at 100fps would move twice as fast as someone at 50fps.
Time.deltaTime is the amount of time since the last frame, (usually a very small fraction of a second), so by multiplying it, you effectively make the whole thing “per second”.
So this would make your character move “speed” units per second, in the direction of “horizontal”, independent of framerate.
That wasn’t a complete class, just the update function and some additional variables. Replace your update function with mine, and add the new variables to your class.
Make sure you set a speed value in the inspector or your player will be moving at 0 units per second.
using UnityEngine;
using System.Collections;
public class playerController : MonoBehaviour
{
private Animator animator;
// Use this for initialization
void Start()
{
animator = this.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
this.transform.Translate(Input.GetAxis("Horizontal"),0,0);
var horizontal = Input.GetAxis("Horizontal");
if (horizontal > 0)
{
animator.SetInteger("Direction", 1);
}
else if (horizontal < 0)
{
animator.SetInteger("Direction", 3);
}
}
}
Is this what it should look like now?
using UnityEngine;
using System.Collections;
public class playerController : MonoBehaviour
{
private Animator animator;
// Use this for initialization
void Start()
{
animator = this.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
public float speed;
private float horizontal;
private void Update()
{
horizontal = Input.GetAxis("Horizontal");
transform.Translate(horizontal * speed * Time.deltaTime, 0, 0);
if(horizontal > 0)
{
animator.SetInteger("Direction", 1);
}
else if(horizontal < 0)
{
animator.SetInteger("Direction", 3);
}
}
}
}
Okay, so there are no errors in the console, the game tests fine, however, the character does not move left or right when the “A”, “D”, “Left”, or “Right” keys are pressed.
Then either your bindings for the “Horizontal” axis aren’t setup correctly in the Input settings, or you didn’t set a speed value in the inspector for that component.