简体   繁体   中英

#Unity Cube Movement (Jumping +1 Forward/Right/Backward/Left)

Hey stackoverflow Community,

First of all:

  • I'm still very new about programming with C# and Unity.

My question: I'm working on an idea for a Movement of a Cube. It is planned that the cube will move forward by pressing a key (W-Key). But it shouldn't just move forward. It should jump forward to the next point. So always plus 1 of its axis into which it should go. Accordingly, it is only intended to go forward, right, down, left. He won't be able to jump over behind. You should also see that the cube jumps in the respective direction, so it should not teleport itself. :D

Does anyone have an idea how I can realize this movement? I am very much looking forward to your ideas.

(Sorry if my English is not so good, my English not the best. ^^)

best regards xKarToSx

So, in order to understand movement, it's best to first understand Vectors in Unity. Since you want to be moving the cube in the forward direction, I'm going to assume this is a 3D game, in which case you want to use a Vector3.

A Vector3 has three components: X, Y, and Z. Each component is tied to an axis. In simple terms, X is tied to left and right, Y is tied to up and down, and Z is tied to forward and back. So, Vector3 position = new Vector3(0, 1, 2); will be a vector that is 1 unit above and 2 units in front of the starting position.

Assuming you've attached this script to the cube you want to move, you can track its position with transform.position . So, if you want to move the cube one unit forward, your code would look something like this:

if(Input.GetKeyDown(KeyCode.W)) // This code will activate once the user presses W. { transform.position += new Vector3(0, 0, 1); }

That will move the cube one unit forward in the Z direction. However, you don't want it to teleport, you want to see it move, correct? In that case, you want to check out Unity's Vector3.Lerp function. Basically, you would use it to smoothly transition an object between two defined positions. You'll need to implement a timer and a for loop in order to make this work correctly.

So, to summarize, for moving one unit forward in the Z direction, your code would look something like this:

if(Input.GetKeyDown(KeyCode.Z))
{
    float startTime = Time.time; //Time.time is the current in-game time when this line is called. You'll want to save this to a variable
    float speed = 1.0f; //The speed if something you'll want to define. The higher the speed, the faster the cube will move. 
    Vector3 startPosition = transform.position; //Save the starting position to a different variable so you can reference it later
    Vector3 endPosition = startPosition + Vector3.forward; //Vector3.Forward is equivalent to saying (0, 0, 1);
    float length = Vector3.Distance(startPosition, endPosition); //You'll need to know the total distance that the cube will move.
    while(transform.position != endPosition) //This loop while keep running until the cube reaches its endpoint
    {
        float distCovered = (Time.time - startTime) * speed; //subtracting the current time from your start time and multiplying by speed will tell us how far the cube's moved
        float fraction = distCovered / length; //This will tell us how far along the cube is in relation to the start and end points. 
        transform.position = Vector3.Lerp(startPosition, endPosition, fraction); //This line will smoothly transition between the start and end points
    }
}

I hope this helps you. This is my first time answering a question so sorry if I got some things wrong/it's not the most optimized. Good Luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM