Change value of Transform variable in Unity

 ̄綄美尐妖づ 提交于 2019-12-13 21:07:02

问题


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

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)


回答1:


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;
}


来源:https://stackoverflow.com/questions/19893598/change-value-of-transform-variable-in-unity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!