在Unity中更改Transform变量的值 [英] Change value of Transform variable in Unity
问题描述
我有一个摄像机,当玩家开车时,它会跟在汽车后面.问题出在汽车上:UnityScript中的transform变量看起来像这样
I have a camera which follows a car when a player is driving. The issue is the car : transform variable in the UnityScript looks like this
var car : Transform;
这意味着我将不得不将转换拖动到侧面板的小框中进行分配.
Which means I would have to drag the transform onto the little box in the side panel to assign it.
是否可以在如下代码中分配此变量:
Is it possible to assign this variable within the code like:
var car : Transform = Player1;
//BTW Player1 is the transform I want
我需要在代码之间进行更改的代码需要更改 播放器1 播放器2 Player3
The reason why it needs to be changed is in the code I want it to change between Player1 Player2 Player3
取决于当前选择的一个(我已经对此部分进行了编码)
dependant on the currently selected one (i already coded this part)
推荐答案
这取决于您要使用的方式和时间.在您的Camera类中,您可以执行以下操作:
It depends on how and when you want to do it. From your Camera class you can do something like:
var car : Transform;
public void ChangePlayer(string playerName) {
GameObject playerGO = GameObject.Find(playerName);
if(playerGO != null)
car = playerGO.transform;
}
这篇关于在Unity中更改Transform变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!