やまめも

【 ゲーム開発・日々のことについて 】

Raycast,C#でサンプル

using UnityEngine;
using System.Collections;
public class CubeManager : MonoBehaviour {
	public static CubeManager instance = null;
	void Awake(){
		if(instance = null){
			instance = this;
		}
	}
	void Update () {
		if( this.gameObject == GameManager.instance.SelectedGameObject){
			transform.localScale = new Vector3(1.2f, 1.2f, 1.2f);
		}
		else{
			transform.localScale = new Vector3(1, 1, 1);
		}
	}
}
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour {
	public GameObject SelectedGameObject;
	public static GameManager instance = null;
	void Awake(){
		if(instance == null){
			instance = this;
		}
	}
	void Update () {
		// カメラのタグをmainタグに変更しておく。
		// 物体にはboxcolliderをつけておく。
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		RaycastHit hit;
		if(Physics.Raycast(ray,out hit,100)){
			SelectedGameObject = hit.collider.gameObject;
		}
		else {
			SelectedGameObject = null;
		}
	}
}

d.hatena.ne.jp