简体   繁体   English

变形看虫

[英]Transform.LookAt Bug

My game is giving me a bug when I'm using transform.LookAt(); 当我使用transform.LookAt();时,我的游戏给我一个错误。

Here is a screen shot: http://imgur.com/pMnPVpk 这是一个屏幕截图: http : //imgur.com/pMnPVpk

I have a car that is on the ground, but the bug makes it stand on its end. 我有一辆在地上的汽车,但虫子使它停在了尽头。

Code: 码:

void Update () 
{
    if(Vector3.Distance(Player.transform.position,transform.position)< MaxDistnace)
    {
        transform.LookAt(PlayerTransform);
    }
}

Your problem is to do with the way your car is created. 您的问题与汽车的制造方式有关。 LookAt() will just make the object face the target, regardless of what axis it is on. 无论对象在哪个轴上,LookAt()都只会使对象面对目标。

A quick way to fix it (not necessarily the best) is to create an empty GameObject and make the car a child of the empty GameObject. 修复它的快速方法 (不一定是最好的方法)是创建一个空的GameObject,并使汽车成为该空GameObject的子代。

Then rotate the car 90 degrees in whichever direction is wrong. 然后将汽车向错误的方向旋转90度。 If the car rotates 90 degress forwards the wrong way, then rotate it 90 degress backwards. 如果汽车以错误的方式向前旋转90度,则向后旋转90度。

Remove the script from the car and add it to the empty GameObject. 从汽车上删除脚本并将其添加到空的GameObject。

Looking at your code again, perhaps a few things to try before the above: 再次查看您的代码,在上述操作之前可能需要尝试一些操作:

transform.LookAt(PlayerTransform) should probably be Player.transform unless you have created a variable to hold this called PlayerTransform . transform.LookAt(PlayerTransform)大概应该是Player.transform ,除非你已经创建了一个变量来保存这个叫PlayerTransform

Rename your MaxDistnace to maxDistance this is much better coding practice. 将您的MaxDistnace重命名为maxDistance这是更好的编码实践。

Hope it helps! 希望能帮助到你!

EDIT Alternatively, your player could be directly above your car, which is why the car is facing up! 编辑或者,您的播放器可以直接在您的汽车上方,这就是为什么汽车朝上的原因! Put your player on the same level as your car and see if that makes a difference too! 将您的播放器放在与汽车相同的水平上,看看是否也有所不同!

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

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