简体   繁体   English

Unity游戏对象位置不正确

[英]Unity game object not in correct position

I'm making a unity 3D game, part of the game allows the player to get into a car and drive it. 我正在制作一个统一的3D游戏,游戏的一部分允许玩家进入汽车并驾驶它。

Inside the car I have put a "seat" GameObject whose position and rotation is used to determine where the player will sit. 在车内,我放置了一个“座位” GameObject,其位置和旋转度用于确定玩家的坐姿。

When I want the player to sit in the car, I make the car the players parent, then set the player transform position and rotation to that of the seat. 当我希望玩家坐在汽车上时,我将汽车设为玩家的父母,然后将玩家的变换位置和旋转设置为座椅的位置和旋转。

Here is the C# code 这是C#代码

 // make player's parent the same as the seats parent (which is the car)
 transform.parent = seat.transform.parent;

 // put player in the exact position and rotation as the seat
 animator.transform.position = transform.position = seat.transform.position;
 animator.transform.rotation = transform.rotation = seat.transform.rotation;

 animator.transform.localPosition = transform.localPosition = seat.transform.localPosition;
 animator.transform.localRotation = transform.localRotation = seat.transform.localRotation;

This seems like it should work, but what ends up happenning is that for some reason the player does not end up perfectly in the seat, but some short distance away from it, and also the players rotation doesnt match the seat, instead ends up slightly off. 这似乎应该起作用,但最终发生的原因是,由于某种原因,玩家不能完全落在座位上,而是离它很短的距离,并且玩家的旋转与座位不匹配,而是稍微停下来关。 So the player looks like he is not really in the seat but floating near it, and turned around in some other direction. 因此,玩家看起来好像他不是真正坐在座位上,而是在座位附近浮动,然后朝其他方向转过身。

Anybody knows what I'm doing wrong? 有人知道我在做什么错吗?

position specifies an object's location in the world. position指定对象在世界上的位置。 If you take two objects and assign the same position , then they will both be in the same place. 如果您使用两个对象并分配相同的position ,则它们都将位于同一位置。

localPosition specifies an object's location relative to its parent. localPosition指定对象相对于其父对象的位置。 If you take two objects and assign the same localPosition , the outcome will depend on the position of each object's parent. 如果您使用两个对象并分配相同的localPosition ,则结果将取决于每个对象的父对象的位置。 If they have different parents, different scaling, or different rotation, they may end up in different places. 如果他们有不同的父母,不同的比例或不同的轮换,他们可能会出现在不同的地方。

Roughly the same idea applies to rotation and localRotation . 大致相同的想法适用于rotationlocalRotation

You seem to have confused the relationship between world-space and local-space coordinates. 您似乎混淆了世界空间坐标和局部空间坐标之间的关系。 It's very important to understand the difference, and how they relate to the scene hierarchy. 了解差异以及它们与场景层次结构的关系非常重要。

If you want to put both objects in the same place, this should suffice: 如果要将两个对象放在同一位置,则应满足以下条件:

transform.position = seat.transform.position;
transform.rotation = seat.transform.rotation;

If you want the object to move with the car, afterward, you could also reassign its parent: 如果要让对象随汽车一起移动,则还可以重新分配其父对象:

transform.parent = seat.transform.parent;

检查播放器对象的枢轴点,它可能不在您期望的位置上,如果不在,请尝试通过拖动模型使其指向所需位置。

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

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