Z
Z
zergon3212017-03-25 14:07:05
C++ / C#
zergon321, 2017-03-25 14:07:05

Why doesn't scripting work?

Wrote the script for character movement:

using UnityEngine;

public class Moving : MonoBehaviour {

    public float Speed = 0.5f;
    private float Horisontal, Vertical; 
  
  void Update ()
    {
        Horisontal = Input.GetAxis("Horisontal") * Speed;
        Vertical = Input.GetAxis("Vertical") * Speed;
        transform.Translate(Horisontal, 0, Vertical);
    }
}

Before the test, I set the scene in the Input Manager'e A and D for Horisontal and S and W for Vertical. The object with the component still doesn't move.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Basmanov, 2017-03-25
@zergon321

And why did you create your exact same axes if the unit already has Horizontal and Vertical out of the box? And by the way, yes, you have a typo in the title, it will be correct through the letter z. Are there any errors in the console? Have you tried running it on an empty stage with a cube? Is the script enabled in the inspector? GameObject not marked as static? At worst, you can try using the buttons directly:

using UnityEngine;

public class Moving : MonoBehaviour
{
    public float Speed = 0.5f;

    private void Update()
    {
        float horizontal = Input.GetKey(KeyCode.A) ? -Speed : Input.GetKey(KeyCode.D) ? Speed : 0;
        float vertical = Input.GetKey(KeyCode.S) ? -Speed : Input.GetKey(KeyCode.W) ? Speed : 0;
        transform.Translate(horizontal, 0, vertical);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question