K
K
Kukurum12021-01-20 15:35:50
C++ / C#
Kukurum1, 2021-01-20 15:35:50

How can I use a script to move an object in Unity by a certain distance, for example by "1" with input.GetKeyDown?

there is a function:

if (input.GetKeyDown(KeyCode.A))
    {
      //плавно передвинуть объект (translate) на 1 по оси z влево
    }
else if (input.GetKeyDown(KeyCode.D))
    {
      //плавно передвинуть объект (translate) на 1 по оси z вправо
    }

When you press a key, you need to smoothly move the object using transform.translate (I believe) a certain distance.
Tell me how to implement it?

Tried inside the if function, but naturally when you press the key - the movement occurs 1 frame and stops.
I tried using bool and using other methods, but during the first movement, the animation is saved, and during subsequent movements, it does not move, but teleports the object, i.e. for 1 frame.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2021-01-20
@freeExec

DoTween

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question