Last active
August 29, 2015 14:12
-
-
Save takahi5/d56310b53e3628568064 to your computer and use it in GitHub Desktop.
[Unity]Check camera direction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Cubeがカメラ視野の中心にあるか調べるスクリプト | |
// このスクリプトをCubeにアタッチする | |
public class direction : MonoBehaviour { | |
GameObject camera; | |
void Start () { | |
camera = GameObject.FindWithTag ("MainCamera"); | |
} | |
void Update () { | |
// camera→cubeへの単位ベクトルv1 | |
Vector3 v1 = new Vector3 ( | |
transform.position.x - camera.transform.position.x, | |
transform.position.y - camera.transform.position.y, | |
transform.position.z - camera.transform.position.z | |
); | |
v1.Normalize (); | |
// カメラの正面方向の単位ベクトルv2 | |
Vector3 v2 = camera.transform.forward; | |
// 内積を求める | |
float dot = Vector3.Dot (v1, v2); | |
// 角度差を求める | |
float rad = Mathf.Acos (dot); | |
// 角度差が一定値以下ならCubeを赤くする | |
if (rad < 0.2f) { | |
renderer.material.color = Color.red; | |
} else { | |
renderer.material.color = Color.white; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment