Skip to content

Instantly share code, notes, and snippets.

@KzoNag
Created June 16, 2015 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KzoNag/da1632a915df7010bf9f to your computer and use it in GitHub Desktop.
Save KzoNag/da1632a915df7010bf9f to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour
{
//public Vector3 initialPosition;
// 動きかたの種類
// 0 : 前後
// 1 : 初期位置を中心に円
// 2 : 初期位置を中心として四角
public int moveType;
// 初期位置
private Vector3 initialPosition;
// どっちにどれくらい動くか
private Vector3 moveValue = new Vector3(0, 0, 0.1f);
// Updateが呼ばれた回数を数えるカウンター
private int counter;
// Use this for initialization
void Start ()
{
// 初期位置を保持しておく
initialPosition = transform.position;
}
// Update is called once per frame
void Update ()
{
if (moveType == 0)
{
MoveLine ();
}
else if (moveType == 1)
{
MoveCircle ();
}
else if (moveType == 2)
{
MoveSquare ();
}
}
void OnTriggerEnter(Collider other)
{
Debug.Log ("Game Over");
}
void MoveLine()
{
if (transform.position.z < initialPosition.z - 5)
{
moveValue = new Vector3 (0, 0, 0.1f);
}
else if(transform.position.z > initialPosition.z + 5)
{
moveValue = new Vector3 (0, 0, -0.1f);
}
//transform.position = transform.position + moveValue;
transform.position += moveValue; // 上と同じ意味
}
void MoveCircle()
{
// 三角関数的なかんじ?
float degree = counter / 30f;
float radius = 3;
float x = radius * Mathf.Sin (degree); // サインの計算 Mathfクラスに数学の便利機能が入ってる
float z = radius * Mathf.Cos (degree); // コサインの計算
transform.position = initialPosition + new Vector3 (x, 0, z);
counter++;
}
void MoveSquare()
{
if (counter < 100)
{
moveValue = new Vector3 (0, 0, 0.1f);
}
else if (counter < 200)
{
moveValue = new Vector3 (0.1f, 0, 0);
}
else if (counter < 300)
{
moveValue = new Vector3 (0, 0, -0.1f);
}
else if (counter < 400)
{
moveValue = new Vector3 (-0.1f, 0, 0);
}
else if (counter < 500)
{
// 1周したらカウンターリセット
counter = 0;
}
transform.position = transform.position + moveValue;
// カウント
//counter = counter + 1;
//counter += 1;
counter++; // 上ふたつと同じ意味
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment