Very Basic Question - Controls and their speed

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);
        }
    }
}

try something like this:

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.

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);
    }
}

Thank you! I’ll try it out

1 Like

I tried putting the script in and now this script:

using UnityEngine;
using System.Collections;

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);
    }
}

Gives me the following errors:

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.

Here is my original code:

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);
        }
    }
    }
}

Almost, you’ve got a few extra lines and braces in there.

using UnityEngine;
using System.Collections;

public class playerController : MonoBehaviour
{
    public float speed;
    private float horizontal;
    private Animator animator;

    // Use this for initialization
    void Start()
    {
        animator = GetComponent<Animator>();
    }

    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.

Add this code called
Time.deltaTime
to your vector3 in transform component and it will be move smoothly.

.....
this.transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime,0,0);
.....

You should also make public variable speed for adjust the speed of the character.

Please read the thread before replying. I already addressed these things in previous posts.

I tried to make it clear for him. He said it won’t move.

Yes, and the code he quoted when he said that includes both Time.deltaTime and a speed variable.