简体   繁体   English

我如何统一更改玩家位置?

[英]How do i change player position in unity?

So im new to c# and i don't know much but i wanna know how to change the position of players and all i have is this line of code: transform.position = 0.067, 1.52, -0.37;所以我是 C# 新手,我知道的不多,但我想知道如何改变玩家的位置,我只有这行代码: transform.position = 0.067, 1.52, -0.37; It throws an error at me.它向我抛出一个错误。

Transform.position takes Vector3.. Transform.position 采用 Vector3..
You can do it like this.你可以这样做。

transform.position = new Vector3(0.067f,1.52f,-0.37f);

By creating new vector3 and set it to transform.position and make sure that this line of code will work to any game object which have this script on.通过创建新的 vector3 并将其设置为 transform.position 并确保这行代码适用于任何具有此脚本的游戏对象。
Edit 1:编辑1:
As you have error in converting int to float.因为您在将 int 转换为 float 时出错。 There is an rule in unity that if the number is float (having decimal in it) then f has to be write at end.统一规则是,如果数字是浮点数(其中包含小数),那么f必须写在最后。 Like this.像这样。

int a = 4 //int doesn't require `f`
float a = 4.002f //decimal require f, else throw error

Try re-positioning using an external reference.尝试使用外部参考重新定位。

public Transform Target;

void Start()
{
transform.postition = target.position;
}

Just create a gameobject where you want your object to be repositioned and pass that gameobject to your script and press play > it will reposition you object to your target position.只需创建一个游戏对象,您希望在其中重新定位您的对象并将该游戏对象传递给您的脚本,然后按 play > 它会将您的对象重新定位到您的目标位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM