Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
毎日出題の3日目第三問の答え
public class CreatePrimitiveByMouse2 : MonoBehaviour {
GameObject primitive = null;
int currentPrimitiveIndex = 0;
List<PrimitiveType> primitiveTypeList = new List<PrimitiveType>();
void Start()
{
// 必要なPrimitiveTypeを用意
this.primitiveTypeList = new List<PrimitiveType>()
{
PrimitiveType.Sphere,
PrimitiveType.Capsule,
PrimitiveType.Cylinder,
PrimitiveType.Cube
};
}
void Update () {
if (Input.GetMouseButton(0))
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, float.MaxValue))
{
// Nullなら新規作成。
if( this.primitive == null )
this.primitive = this.CreatePrimitive(hit.point);
// nullでないなら追従
else
this.primitive.transform.position = hit.point;
}
}
else if (Input.GetMouseButtonUp(0))
{
// ボタンを話したら消滅
if (this.primitive != null)
{
Destroy(this.primitive);
}
}
}
// 仕様にあったインターフェースで対象おオブジェクトを循環させる
GameObject CreatePrimitive(Vector3 position)
{
if (this.currentPrimitiveIndex >= this.primitiveTypeList.Count )
{
this.currentPrimitiveIndex = 0;
}
var p = GameObject.CreatePrimitive( primitiveTypeList[this.currentPrimitiveIndex ++ ] );
Destroy(p.GetComponent<Collider>());
p.transform.position = position;
return p;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.