2016-12-02 8 views
0

코드를 사용하면 플레이어가 원의 원주를 따라 움직여 그 원의 중심으로 "점프"할 수 있습니다.Lerp 점프 중에 플레이어를 수평으로 이동 Unity C#

내가하려는 것은 점프 중에 플레이어가 원을 중심으로 자신의 움직임을 계속할 수있게하여 점프하는 동안 플레이어가 자신의 위치를 ​​변경할 수있게하는 것입니다.

이것은 지금까지 가지고있는 전체 코드입니다. 사용자가 점프하면, 왼쪽 및 오른쪽 키의 입력을 무시하고 있기 때문에 현재 Update 아이디어와

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; 
} 

}

답변

2

은 가능하지 않습니다.

if(jumping)을 실제 점프 값으로 이동하면이 기능을 무시할 수 있습니다.

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