Показать сообщение отдельно
Старый 16.11.2011, 13:46   #2
Amatsu
Дэвелопер
 
Аватар для Amatsu
 
Регистрация: 24.07.2008
Сообщений: 1,544
Написано 1,095 полезных сообщений
(для 2,706 пользователей)
Ответ: Движение 2D космического корабля

// Player class
public class Player {

  public GameObject move_pivot, rotate_pivot, model;
  public float yaw_speed,yaw_rotation,x_speed,z_speed;

  // Procedure of player creation
  public void Create() {
    move_pivot = new GameObject("player");
    rotate_pivot = new GameObject("player_rotate_pivot");
    rotate_pivot.transform.parent = move_pivot.transform; 

    model = Instantiate(player_mesh) as GameObject;
    model.transform.localScale = new Vector3(10, 10, 10);
    model.transform.name = "player_model";
    model.transform.parent = rotate_pivot.transform; 
  }
	
  // Procedure of player update
  public void Update(float yaw_acceleration, float speed_acceleration) {
			
    float width;

    if (Input.GetKey(KeyCode.LeftArrow)) {yaw_speed += -yaw_acceleration;};
    if (Input.GetKey(KeyCode.RightArrow)) {yaw_speed += yaw_acceleration;};

    yaw_speed *= 0.85F;
    yaw_rotation += yaw_speed;
    rotate_pivot.transform.localRotation = Quaternion.Euler(0, yaw_rotation, 0);

    model.transform.localRotation = Quaternion.Euler(0, 0, yaw_speed * 10);

    if (Input.GetKey(KeyCode.UpArrow)) {
      x_speed += (Mathf.Sin(((rotate_pivot.transform.transform.eulerAngles.y - 180) * Mathf.PI) / 180F) * speed_acceleration);
      z_speed += (Mathf.Cos(((rotate_pivot.transform.transform.eulerAngles.y - 180) * Mathf.PI) / 180F) * speed_acceleration);
    };

    x_speed *= 0.95F;
    z_speed *= 0.95F;

    width = ((float)Screen.width / (float)Screen.height) * 4.6F;

    if ((move_pivot.transform.position.x < -width) & (x_speed < 0)) {x_speed = 0;};			
    if ((move_pivot.transform.position.x > width) & (x_speed > 0)) {x_speed = 0;};
    if ((move_pivot.transform.position.z < -4.4F) & (z_speed < 0)) {z_speed = 0;};			
    if ((move_pivot.transform.position.z > 4.4F) & (z_speed > 0)) {z_speed = 0;};

    move_pivot.transform.Translate(x_speed, 0, z_speed);
  }
}
(Offline)
 
Ответить с цитированием
Сообщение было полезно следующим пользователям:
Wegox (08.04.2012)