Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在lerp跳跃时水平移动玩家C#_C#_Unity3d_Trigonometry_Lerp - Fatal编程技术网

在lerp跳跃时水平移动玩家C#

在lerp跳跃时水平移动玩家C#,c#,unity3d,trigonometry,lerp,C#,Unity3d,Trigonometry,Lerp,我的代码允许玩家对象围绕一个圆的圆周移动,然后“跳”到该圆的中心 我想做的是让玩家在跳跃时继续绕圆圈移动,这样玩家可以在跳跃时改变自己的位置 这是我到目前为止的全部代码 using UnityEngine; using System.Collections; public class Movement : MonoBehaviour { //editable property if made public public float playerSpeed = 20f;

我的代码允许玩家对象围绕一个圆的圆周移动,然后“跳”到该圆的中心

我想做的是让玩家在跳跃时继续绕圆圈移动,这样玩家可以在跳跃时改变自己的位置

这是我到目前为止的全部代码

    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;
}
使用UnityEngine;
使用系统集合;
公共阶级运动:单一行为
{
//可编辑属性(如果已公开)
public float player速度=20f;//这是玩家移动的速度
float playerAngle=0;//这是players移动变量
float radius=4f;//这是圆有多大的半径(圆周轨迹2piRadius
float startTime;//这是游戏开始的时间
float gameTime;//这将是基于游戏运行时间的玩家点数
float playerRadius=.5f;//对PLAYER对象变量的更改是PLAYER与lvlradius的偏移量。
private bool jumping=false;//这会影响游戏中的移动
private Vector3 playerPosition;//这是游戏中的playerPosition
void Start()
{
//比赛一开始就打电话来
}
无效更新()
{
如果(跳跃)
{
返回;
}
else if(Input.GetKey(KeyCode.LeftArrow)| | Input.GetKey(KeyCode.RightArrow))
{
循环运动();
}
else if(Input.GetKeyDown(KeyCode.Space))
{
开始例行程序(jumpPlayer());
}
}
void FixedUpdate()
{
//在进行物理计算之前调用
}
无效循环()
{
//玩家变量
//玩家所处的角度等于玩家的速度除以游戏中的半径乘以时间,再加上左右键
playerAngle+=Input.GetAxis(“水平”)*Time.deltaTime*(播放器速度/半径);
//这是x轴上的运动
浮点数x=数学坐标(游戏者角)*(半径-游戏者角);
//这是y轴上的运动
浮动y=数学正弦(游戏者角)*(半径-游戏者角);
//球员此时不向前移动这将是如何移动球员的
浮点数z=0;
//沿玩家单击的方向移动玩家,但将原始坐标添加到玩家位置
transform.position=新矢量3(x,y,z);
//移动玩家;
playerPosition=transform.position;
}
私人IEnumerator jumpPlayer()
{
Vector3 playerEndPos=新Vector3(0f,0f,0f);
Vector3 playerStartPos=播放位置;
浮点数i=0.0f;
浮动利率=.1f/时间延迟;
跳跃=正确;
而(i<1.0)
{
i+=时间增量*速率;
transform.position=Vector3.Lerp(playerStartPos,playerEndPos,i);
收益返回空;
}
transform.position=playerEndPos;
i=0.0f;
而(i<1.0)
{
i+=时间增量*速率;
transform.position=Vector3.Lerp(playerEndPos,playerStartPos,i);
收益返回空;
}
transform.position=播放器开始位置;
跳跃=错误;
屈服断裂;
}

}

使用当前的
更新
您的想法将永远不可能实现,因为如果用户正在跳转,您将忽略左右键的输入

通过将
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());
    }
}

使用当前的
Update
您的想法将永远不可能实现,因为如果用户正在跳转,您将忽略左右键的输入

通过将
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());
    }
}