简体   繁体   English

我制作了一个健康栏以保持在敌人上方,但它并不总是在敌人的中心

[英]I made a health bar to stay above enemy but it's not always in the center of enemy

Health bar image is a children of an "Screen Space - overlay" Canvas and it has this code inside of it健康栏图像是“屏幕空间 - 覆盖”Canvas 的子项,其中包含此代码

void Update()
    {
        var wantedPos = Camera.main.WorldToScreenPoint(Enemy.position);
        transform.position = new Vector3(wantedPos.x, wantedPos.y + 40, wantedPos.z);
    }

but the problem is the health bar apear above enemy but it's not always in the center of enemy但问题是健康栏出现在敌人上方但并不总是在敌人的中心

here is a video to explain the problem more https://youtu.be/ZMWKxJvjy2o这是一个视频来解释更多问题https://youtu.be/ZMWKxJvjy2o

So looks like you are taking the centrepoint of the enemy, converting that point into screenspace, and then moving vertically up on screen to get your health bar position.所以看起来你正在占领敌人的中心点,将该点转换为屏幕空间,然后在屏幕上垂直向上移动以获得你的健康条 position。 It seems to work ok but u get that undesired effect.它似乎工作正常,但你得到了不希望的效果。

I dont really know what you want, but you could start with a higher point in world space.我真的不知道你想要什么,但你可以从世界空间的更高点开始。

Instead of Enemy.position, make a child gameobject of the enemy that is higher up, closer to the enemies head, use that position instead of Enemy.position, and maybe reduce that 40 number.. It might be closer to what u want.代替 Enemy.position,制作一个更高的敌人的子游戏对象,更靠近敌人的头部,使用 position 而不是 Enemy.position,可能更接近想要的数量。

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

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