2016-11-27 38 views
1

良好的周日足球下午大家, 我的问题是我在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 
} 

}

+0

所以玩家已经是在y(6)周围移动一个非常小的圆圈,并且我试图让这个圆圈成为一个变量,随着等级的进展可以改变这个变量,可以操纵圆周来增加玩家的移动范围。 –

回答

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); 
} 
+0

速度会是playerSpeed正确吗? –

+0

@ Sage.VS:是的。 – Tryss

+0

所以使用你的代码,我在做实际的动作时会遇到一个非常糟糕的错误。玩家对象将不再走完整个圈子,而只会做一部分弧线。 –