简体   繁体   English

在lerp跳跃时水平移动播放器Unity C#

[英]Moving player horizontally while in lerp jump Unity C#

The code I have allows a player object to move around the circumference of a circle and then "jump" to the center of that circle. 我拥有的代码允许播放器对象绕圆的圆周移动,然后“跳转”到该圆的中心。

What I am trying to do is allow the player to continue their movement around the circle while jumping so that the player can alter their position while jumping. 我正在尝试做的是允许玩家在跳跃时继续绕圈运动,以便玩家可以在跳跃时改变自己的位置。

This is the full code I have so far. 这是我到目前为止的完整代码。

    using UnityEngine;
    using System.Collections;

    public class Movement : MonoBehaviour
 {
//editable property if made public
public float playerSpeed = 20f;//This is how fast the player moves
float playerAngle = 0; //This is the players movement variable
float radius = 4f; //This is the radius of how big the circle is (Circumference track 2piRadius
float startTime; //This is the time the game started
float gameTime; //This will be player points BASE THIS OFF OF HOW LONG THE GAMES RUNNING
float playerRadius = .5f; //CHANGE TO THE PLAYER OBJECT VARIABLE, is how offset the player will be from the lvlradius.
private bool jumping = false; //This effects movement during the game
private Vector3 playerPosition; //This is the playerPosition in the game
void Start()
{
    //Called at the start of the game
}

void Update()
{
    if (jumping)
    {
        return;
    }
    else if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow))
    {
        circularMove();
    }
    else if (Input.GetKeyDown(KeyCode.Space))
    {
        StartCoroutine(jumpPlayer());
    }
}

void FixedUpdate()
{
    //Called before performing physics calculations
}

void circularMove()
{
    //player variables

    //The angle the player is at is equal to the speed of the player divided by radius times time in the game and the addition of the left right keys
    playerAngle += Input.GetAxis("Horizontal") * Time.deltaTime * (playerSpeed / radius);
    //This is the movement on the x axis
    float x = Mathf.Cos(playerAngle) * (radius - playerRadius);
    //this is the movement on the y axis
    float y = Mathf.Sin(playerAngle) * (radius - playerRadius);
    //the player does not move forward at this time THIS WILL BE HOW TO MOVE THE PLAYER
    float z = 0;
    //move the player in the direction that they clicked but add the original coordinates to the players location
    transform.position = new Vector3(x, y, z);
    //Move the player;
    playerPosition = transform.position;
}

private IEnumerator jumpPlayer()
{
    Vector3 playerEndPos = new Vector3(0f, 0f, 0f);
    Vector3 playerStartPos = playerPosition;
    float i = 0.0f;
    float rate = .1f / Time.deltaTime;
    jumping = true;
    while (i< 1.0)
    {
        i += Time.deltaTime * rate;
        transform.position = Vector3.Lerp(playerStartPos, playerEndPos, i);
        yield return null;
    }
    transform.position = playerEndPos;
    i = 0.0f;
    while (i < 1.0)
    {
        i += Time.deltaTime * rate;
        transform.position = Vector3.Lerp(playerEndPos, playerStartPos, i);
        yield return null;
    }
    transform.position = playerStartPos;
    jumping = false;
    yield break;
}

} }

With the current Update your idea will never be possible, because if a user is jumping, you are ignoring the input of the left and right key. 使用当前的Update您的想法将永远无法实现,因为如果用户在跳跃,则将忽略向左和向右键的输入。

By shifting the if(jumping) to the actual jump statement you could probably bypass this. 通过将if(jumping)转换为实际的jump语句,您可能可以绕过它。

void Update()
{
    if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow))
    {
        circularMove();
    }
    else if (Input.GetKeyDown(KeyCode.Space))
    {
        if (!jumping)
            StartCoroutine(jumpPlayer());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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