To move objects in response to keyboard operations in unity5

Asked 2 years ago, Updated 2 years ago, 42 views

In unity5, when I press +10 and -10 when I press +10 when I press +10 on the keyboard arrow key 」→ 。, I want to write something that moves to -10, but I don't know how to do it

functionUpdate(){
    varx: float=Input.GetAxis("Horizontal");
    if(x!=0){
      if(x>0){
        transform.Translate(10,0,0);
      } else {
        transform.Translate(-10,0,0);
      }
    }
}

The current code looks like this, and it moves while you press it.

Click
Sue
Click
Sue, Sue
What should I do to make it look like?

Please use JavaScript as the language.

javascript unity3d

2022-09-29 22:32

2 Answers

In unity5, when I press the arrow key → on the keyboard, I want to write +10 for x coordinates and -10 for x coordinates when I press ←, but I don't know how to do it.

Let's design the above first.
What the questioner wants to do is
If there is a distance of 10 from point A to point B, instead of going 10 at once,
I think it's going to go to 10 points after a little by little.
In order to meet this requirement, I think you can implement it as follows:
*I wrote that I want to move RPG like 2D Drakue.

Implement 1.10 progress using n frames and 10 progress using n frames
2. Disable keystrokes while moving

I think you can do it like that.
This is either a routine, or if you create a system that manages frames in Update,
I think we can make it happen.

The key is not to let them handle it in one frame, but in a few frames.

http://docs.unity3d.com/ScriptReference/Input.GetKey.html
http://docs.unity3d.com/ScriptReference/KeyCode.html

The above is the official document.
I think this is the key input flag.


2022-09-29 22:32

I understand that the question is, "How do I keep the key moving even if I press it?" Is this correct?

If the understanding is correct, basically keep the amount of movement first and keep giving the amount of movement to transform.Translate() every frame, then it will keep moving without keystrokes.

"I don't know the specifications of the ""Soo"" part, so I wrote it appropriately (speed is given by key input and gradually decelerates over a few frames), but I wonder if it's like this."
I think you can see how it keeps moving even if you release the key.
Please try the amount of movement and the degree of deceleration according to your environment.
This is my first time using JavaScript in Unity, so if it's strange, I'll use Sumimasen.

#pragma strict

var v —Vector3;

function Start() {
}

function Update() {
    varx: float=Input.GetAxis("Horizontal");
    if(x!=0){
        if(x>0){
            // transform.Translate(10,0,0);
            v.x = 0.1f; // Change the amount of movement
        } else {
            // transform.Translate(-10,0,0);
            v.x = -0.1f;// Change the amount of movement
        }
    }
    transform.Translate(v);
    v.x* = 0.95f;// Update Movements
}


2022-09-29 22:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.