如何引用虚拟相机Cinemachine在Unity中。

huangapple go评论81阅读模式
英文:

how to reference virtual camera cinemachine unity

问题

我在使用Cinemachine进行2D的Unity项目,代码实际上是有效的,但每次按播放按钮时,虚拟摄像机在玩家脚本引用中消失。在这里看一下如何引用虚拟相机Cinemachine在Unity中。

正如你在玩家控制中看到的,有一个用于虚拟摄像机的变量/引用,但当我按播放按钮时
如何引用虚拟相机Cinemachine在Unity中。
然后摄像机就从引用中消失了。

我尝试使用GameObject.findobjectwithtag找到摄像机,但它报错说“无法将GameObject转换为虚拟摄像机”。有其他方法引用虚拟摄像机吗?

英文:

i was doing unity project with cinemachine in 2D, and the code is actually work but everytime i press play, the virtual cam dissapear in player script reference, here take a look如何引用虚拟相机Cinemachine在Unity中。

as you can see in player control there is a variable/reference for virtual cam but when i press play
如何引用虚拟相机Cinemachine在Unity中。
and boom the camera is dissapear from its reference

i tried to find the camera using GameObject.findobjectwithtag but its says an error that basicaly said "cannot convert Gameobject to virtual camera". is there any other ways to reference virtual camera?

答案1

得分: 1

我无法留下评论因为我的声誉,但我想说你需要分享更多信息和代码示例,这样我们才能帮助。无论如何,如果你想使用GameObject.FindGameObjectWithTag()或类似的东西,它会返回一个GameObject,然后你可以像这样访问所需的组件:

var gameObj = GameObject.FindGameObjectWithTag("awesomeObj");
var requiredComponent = gameObj.GetComponent<CinemachineVirtualCamera>();

但我不建议这样做,如果你能分享你的代码,也许我们可以看到是什么导致了这个错误。

英文:

I couldn't leave a comment because of my reputation, but I would like to say that you need to share more information and code examples so that we can help. Anyway, if you want to use GameObject.FindGameObjectWithTag() or something like that, it returns a GameObject, and you can reach the component you wanted like:

var gameObj = GameObject.FindGameObjectWithTag(&quot;awesomeObj&quot;);
var requiredComponent = gameObj.GetComponent&lt;CinemachineVirtualCamera&gt;();

but i do not recommend it, if you can share your code, maybe we can see what causes that mistake.

huangapple
  • 本文由 发表于 2023年5月6日 21:13:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定