Unity 摄像机更改会影响单击对象的能力



我正在构建一个第一人称视角的游戏,除非我摧毁了一个盒子,然后是第三人称。

正在销毁的盒子需要打 3 次(也就是点击 3 次(才能完全销毁。相机更改在第一次打孔时有效,但之后当它返回到第一人称摄像机时,它不会记录任何点击,并且在没有相机更改时它会完全工作。它在移动设备上。谁能帮我?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fist : MonoBehaviour
{
public GameObject fistScript;
public bool isBroken = false;
int count = 4;
GameObject piece1;
GameObject piece2;
GameObject piece3;
GameObject piece4;
bool inTrigger;
public GameObject rubble;
public GameObject zone;
Animator anim;
public GameObject player;
public GameObject firstPersonCam;
public GameObject thirdPersonCam;
private void Start()
{
piece1 = rubble.transform.Find("1").gameObject;
piece2 = rubble.transform.Find("2").gameObject;
piece3 = rubble.transform.Find("3").gameObject;
piece4 = rubble.transform.Find("4").gameObject;
zone = rubble.transform.Find("FistTriggerZone").gameObject;
anim = player.GetComponent<Animator>();
}
public void Update()
{
// chainsaw is on
if (fistScript.GetComponent<FistOn>().fistOn == true)
{
//set zone active
zone.SetActive(true);
//set trigger inactive if in trigger so that it can register click on object
if (inTrigger)
{
zone.SetActive(false);
}
else if (!inTrigger)
{
zone.SetActive(true);
}
// chainsaw is off
} 
else 
{
zone.SetActive(false);
}
//if it's not broken yet
if (!isBroken)
{
DestroyBlock();
} 
else 
{
zone.SetActive(false);
}
}
public void DestroyBlock()
{
RaycastHit hit = new RaycastHit();
for (int i = 0; i < Input.touchCount; ++i) 
{
if (Input.GetTouch(i).phase.Equals(TouchPhase.Began)) 
{
// Construct a ray from the current touch coordinates
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
if (Physics.Raycast(ray, out hit)) 
{
if (hit.transform.name == "1" || hit.transform.name == "2" || hit.transform.name == "3" || hit.transform.name == "4")
{
if (fistScript.GetComponent<FistOn>().fistOn == true)
{
if (count == 4)
{
StartCoroutine(SwitchCamera());
Destroy(piece1);
} 
else if (count == 3)
{
StartCoroutine(SwitchCamera());
Destroy(piece2);
} 
else if (count == 2)
{
StartCoroutine(SwitchCamera());
Destroy(piece3);
} 
else if (count == 1)
{
StartCoroutine(SwitchCamera());
Destroy(piece4);
isBroken = true;
}
count-=1;
}
anim.SetInteger("action",0);
}
}
}
}
}
void OnTriggerEnter()
{
inTrigger = true;
}
void OnTriggerExit()
{
inTrigger = false;
}
private IEnumerator SwitchCamera()
{
firstPersonCam.SetActive(false);
thirdPersonCam.SetActive(true);
anim.SetInteger("action", 6);
yield return new WaitForSeconds(2);
firstPersonCam.SetActive(true);
thirdPersonCam.SetActive(false);
}
}

你的问题就在这里:

// Construct a ray from the current touch coordinates
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);

您正在使用 Camera.main 将触摸投影到世界空间,而不是当前活动的相机。你想做这样的事情:

thirdPersonCam.GetComponent<Camera>().ScreenPointToRay(Input.GetTouch(i).position);

另外需要注意的是,您应该避免在 Update(( 函数中使用 Camera.main 和 GetComponent<>((,因为它们都非常慢,不应该每一帧都完成。

如果你需要引用这些东西,如果可能的话,你应该将它们移动到它们自己的变量中,并在 Start(( 函数或其他不是每一帧都发生的地方分配它们。

编辑:为了澄清起见,Camera.main不是指当前正在渲染的相机或位于其他相机之上的相机,它只是Unity找到的第一个带有"主相机"标签的相机。

我个人认为它应该被贬低,因为它会导致许多误解和编写大量缓慢的代码。

最新更新