如何围绕圆周调整玩家移动

时间:2016-11-27 22:11:59

标签: c# unity3d geometry

周日好的足球下午大家, 我的问题是我在Unity中创建了一个玩家控制器,玩家应该以圆周运动向左或向右移动。好吧,我已经创造了这个,但是我很难找到如何让玩家围绕一个最终会改变的固定圆周移动。 这是我到目前为止所得到的,代码可以运行。 C#,团结,使用球体。

//editable property
float timeCounter = 0;
public float speed;

void Start()
{
    //Called at the start of the game
    speed = 1;
}

void Update()
{
    timeCounter += Input.GetAxis("Horizontal") * Time.deltaTime * speed; // multiply all this with some speed variable (* speed);
    float x = Mathf.Cos(timeCounter) ;
    float y = Mathf.Sin(timeCounter) + 6;
    float z = 0;
    transform.position = new Vector3(x, y, z);
}

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

}

1 个答案:

答案 0 :(得分:3)

假设你希望玩家以恒定的线速度移动(并且我理解你想要的东西),我会做那样的事情:

float playerAngle = 0;  // the angular position of the player
float playerSpeed = 0.5;  // the linear speed of the player
float radius = 1;  // the radius of the circle

void Update()
{
    playerAngle += Input.GetAxis("Horizontal") * Time.deltaTime * speed / radius;
    float x = radius * Mathf.Cos( playerAngle ) ;
    float y = radius * Mathf.Sin( playerAngle ) + 6;
    float z = 0;
    transform.position = new Vector3(x, y, z);
}