用射線檢測、實作:三個Cube,點擊其中一個,如果該Cube靜止不動、則該Cube就會旋轉,其他Cube停止旋轉;如果該Cube正在旋轉,則本方塊停止旋轉,其他方塊不動。
uj5u.com熱心網友回復:
public GameObject[] _Cubes;
public int _Speed = 30;
bool isRotation1 = false;
bool isRotation2 = false;
bool isRotation3 = false;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
bool isHit = Physics.Raycast(ray, out hit);
if (isHit)
{
if (hit.transform.name =="Cube1")
{
Debug.Log("click cube 1");
if (isRotation1) isRotation1 = false;
else isRotation1 = true;
isRotation2 = false;
isRotation3 = false;
}
else if (hit.transform.name == "Cube2")
{
Debug.Log("click cube 2");
if (isRotation2) isRotation2 = false;
else isRotation2 = true;
isRotation1 = false;
isRotation3 = false;
}
else if (hit.transform.name == "Cube3")
{
Debug.Log("click cube 3");
if (isRotation3) isRotation3 = false;
else isRotation3 = true;
isRotation2 = false;
isRotation1 = false;
}
}
}
if (isRotation1) _Cubes[0].transform.Rotate(Vector3.forward * _Speed * Time.deltaTime);
if (isRotation2) _Cubes[1].transform.Rotate(Vector3.forward * _Speed * Time.deltaTime);
if (isRotation3) _Cubes[2].transform.Rotate(Vector3.forward * _Speed * Time.deltaTime);
}
uj5u.com熱心網友回復:
你自己都寫出來了,哪里不對嗎?轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/9393.html
標籤:Unity3D
上一篇:untiy 選中建筑然后外圍形成一圈流光,不停的流動
下一篇:pygame是啥?
