宇宙船|Unity5

宇宙船|Unity5

宇宙船|Unity5 : ZIPファイル(509kb)

宇宙船をキーボード上の up, down, left, rightキーでコントロール。

C#

Ship.cs

using UnityEngine;
using System.Collections;

public class Ship : MonoBehaviour {
	SpriteRenderer MainSpriteRenderer;
	public Sprite StandBySprite;
	public Sprite MoveSprite;

	float speed = 0;
	float accel = 0;
	float breaking = 0;
	// Use this for initialization
	void Start () {
		MainSpriteRenderer = gameObject.GetComponent<SpriteRenderer> ();
		transform.Rotate (0, 0, 90f);
	}

	// Update is called once per frame
	void Update () {
		Vector2 v = transform.position;
		if (Input.GetKey (KeyCode.RightArrow)) {
			float r = -1f;
			transform.Rotate (0, 0, r);
			MainSpriteRenderer.sprite = MoveSprite;
		}
		else
			if (Input.GetKey (KeyCode.LeftArrow)) {
				float r = 1f;
				transform.Rotate (0, 0, r);
				MainSpriteRenderer.sprite = MoveSprite;
			}
			else
				if (Input.GetKey (KeyCode.UpArrow)) {
					breaking = 0;
					accel = 0.0001f;
					speed += accel;
					MainSpriteRenderer.sprite = MoveSprite;
				} 
				else 
					if (Input.GetKey (KeyCode.DownArrow)) {
						accel = 0;
						breaking = 0.0001f;
						speed -= breaking;
						if (speed < 0) {
							speed = 0;
						}
						MainSpriteRenderer.sprite = StandBySprite;
					} 
					else {
						accel = 0;
						breaking = 0;
						MainSpriteRenderer.sprite = StandBySprite;
					}

		float radius = transform.localEulerAngles.z * Mathf.PI / 180; // 既存の角度を取得
		float rotation = radius * 180 / Mathf.PI;
		transform.rotation = Quaternion.Euler (0, 0, rotation);
		float vx = Mathf.Cos (radius) * speed;
		float vy = Mathf.Sin (radius) * speed;
		v.x += vx;
		v.y += vy;
		transform.position = v;
	}
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です