C# Unity2D小行星风格游戏
我正试图建立一个统一的小行星风格的游戏,真的需要一些帮助。我相信我所有的数学都是正确的,因为船的运动,但我有困难让它在团结内部工作。我有两个不同的问题C# Unity2D小行星风格游戏,c#,unity3d,game-physics,C#,Unity3d,Game Physics,我正试图建立一个统一的小行星风格的游戏,真的需要一些帮助。我相信我所有的数学都是正确的,因为船的运动,但我有困难让它在团结内部工作。我有两个不同的问题 船不会随着速度而更新(如果你开始移动然后放开,它将静止不动) 我不确定如何将船舶旋转设置为我的特定角度 任何帮助都将不胜感激 public class playerController : MonoBehaviour { public static float timer; public static bool timeStar
public class playerController : MonoBehaviour {
public static float timer;
public static bool timeStarted = false;
Vector2 accel;
Vector2 velocity;
float direction;
float angle;
float shotCooldown;
float speed;
const float pi = 3.141592f;
const float maxSpeed = 300;
const float maxAccel = 500;
void Start () {
timeStarted = true;
}
void Update () {
if (timeStarted == true) {
timer += Time.deltaTime;
}
shotCooldown -= (timer%60);
angle = direction * pi / 180;
if (Input.GetKey(KeyCode.W)) {
accel.y = -Mathf.Cos(angle) * maxAccel;
accel.x = Mathf.Sin(angle) * maxAccel;
velocity += accel * Time.deltaTime;
}
if (Input.GetKey(KeyCode.S)) {
accel.y = -Mathf.Cos(angle) * maxAccel;
accel.x = Mathf.Sin(angle) * maxAccel;
velocity -= accel * Time.deltaTime;
}
if (Input.GetKey(KeyCode.Space)) {
if (shotCooldown <= 0)
{
// Create new shot by instantiating a bullet with current position and angle
shotCooldown = .25f;
}
}
if (Input.GetKey(KeyCode.D)) {
direction += 360 * Time.deltaTime;
}
if (Input.GetKey(KeyCode.A)) {
direction -= 360 * Time.deltaTime;
}
/*
if (this.transform.position.x >= Screen.width && velocity.x > 0) {
this.transform.position.x = 0;
}*/
while (direction < 0) {
direction += 360;
}
while (direction > 360) {
direction -= 360;
}
speed = Mathf.Sqrt( (velocity.x * velocity.x) + (velocity.y * velocity.y));
if (speed > maxSpeed) {
Vector2 normalizedVector = velocity;
normalizedVector.x = normalizedVector.x / speed;
normalizedVector.y = normalizedVector.y / speed;
velocity = normalizedVector * maxSpeed;
}
this.transform.position = velocity * Time.deltaTime;
transform.rotation = Quaternion.AngleAxis(0, new Vector3(0,0,angle));
}
}
公共类playerController:MonoBehavior{
公共静态浮点计时器;
公共静态bool timeStarted=false;
矢量2加速度;
矢量2速度;
浮动方向;
浮动角;
浮动冷却;
浮动速度;
常数浮点pi=3.141592f;
常量浮点最大速度=300;
常量浮点最大加速度=500;
无效开始(){
timeStarted=true;
}
无效更新(){
if(timeStarted==true){
timer+=Time.deltaTime;
}
快照冷却时间-=(计时器%60);
角度=方向*pi/180;
if(Input.GetKey(KeyCode.W)){
加速度y=-Mathf.Cos(角度)*最大加速度;
加速度x=数学正弦(角度)*最大加速度;
速度+=加速度*时间增量;
}
if(Input.GetKey(KeyCode.S)){
加速度y=-Mathf.Cos(角度)*最大加速度;
加速度x=数学正弦(角度)*最大加速度;
速度-=加速度*时间增量;
}
if(Input.GetKey(KeyCode.Space)){
如果(shotcollodown=Screen.width&&velocity.x>0){
这个.transform.position.x=0;
}*/
而(方向<0){
方向+=360;
}
同时(方向>360){
方向-=360;
}
速度=数学Sqrt((速度.x*速度.x)+(速度.y*速度.y));
如果(速度>最大速度){
向量2归一化向量=速度;
normalizedVector.x=normalizedVector.x/速度;
normalizedVector.y=normalizedVector.y/速度;
速度=标准化矢量*最大速度;
}
this.transform.position=速度*时间.deltaTime;
transform.rotation=四元数.AngleAxis(0,新矢量3(0,0,角度));
}
}
按你现在的方式设置位置通常是个坏主意,因为你实际上没有使用任何物理。按你现在的方式,速度
是船的一个新位置,而不是速度。一旦你放开钥匙,它就停止计算新位置,从而停止移动
有两种备选方案可以获得更好的结果:
1)这样做的一种方法是调用:<代码> Trime.Enter(VoCTRESHONTRO**RTIME.DELTATIME) 2) 如果您在船上使用刚体,您可以简单地执行以下操作:
rigidbody.AddForce(Vector3.forward*speed*Time.deltaTime)
它会自动按给定的速度在给定方向上加速船舶
至于旋转,可以尝试以下方法:
if (Input.GetKey(KeyCode.D))
{
Vector3 newRotation = transform.rotation.eulerAngles;
newRotation.z += 10;
transform.rotation = Quaternion.Euler (newRotation);
}
else if (Input.GetKey(KeyCode.A))
{
Vector3 newRotation = transform.rotation.eulerAngles;
newRotation.z -= 10;
transform.rotation = Quaternion.Euler (newRotation);
}