简体   繁体   中英

How to lift the player along the Y axis?

I'm trying to raise the player along the Y-axis, and substitute a new object instead of the space where the player (cube) was - a cube of exactly the same scale. However, first of all, if I repeatedly click on the mouse 0, then the lifting of the cube along the Y axis does not occur every meter, but more randomly. It can rise five or ten metres and form a gap. Also, I can't create another cube under the player, in exactly the same coordinates. Like a ladder formed from the cubes, for example.

  void Update()
{


    if (Input.GetMouseButtonUp(0))
    {



        Vector3 playerMovement = new Vector3(0, Player.position.y * jump, 0);
        playerMovement *= Time.deltaTime;
        cc.Move(playerMovement);




       Vector3 movement = new Vector3(Player.position.x, Player.position.y-100, Player.position.z);
      Instantiate(Cubes, movement, Quaternion.identity);

    }
}

To spawn a cube under the player in the exact same position, save the coordinates of the player before you start the jump sequence.

Secondly, because you are depending the height of the jump on the deltaTime, then it is affected by the time interval between your clicks, causing it to be different every click. In my opinion, check if the player collides with a "floor" (the cubes under it), and use a flag to trigger the jump.

Edit: I can't add a code example now as I am off my PC, but in pseudo code it should look something like this:

Have an object called "feet" with a collider

  • check if "feet" collides with "floor" (use tags for the floor)
  • if true: set "grounded" (bool) to true. else, start the jump.
Jump(){
grounded=false;
player.y += height;
}

Do not copy this as this is not a real code, but use it as a guideline for your own solution

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