简体   繁体   English

玩家不断穿墙

[英]Player Keeps Going Through the Wall

I have a game where map/background is made up of prefabs.我有一个游戏,其中地图/背景由预制件组成。 My player and prefabs both have rigidbodies and colliders.我的播放器和预制件都有刚体和碰撞器。 Neither of them have is trigger checked and the prefabs have collision detection set as continuous dynamic.它们都没有经过触发器检查,并且预制件的碰撞检测设置为连续动态。 The player's collision detection is set on continuous.玩家的碰撞检测设置为连续。 Each of the prefabs have a mesh collider and the individual walls of the prefabs have box colliders (none are set to is trigger).每个预制件都有一个网格碰撞器,预制件的各个墙壁都有盒式碰撞器(没有一个设置为触发器)。 I keep trying to test it on my phone using Unity Remote 5, and every time I move the player it goes through the walls.我一直尝试使用 Unity Remote 5 在手机上对其进行测试,每次移动播放器时它都会穿过墙壁。 If anyone has any advice on how to prevent my player from going through the walls, I would really appreciate it!如果有人对如何防止我的播放器穿过墙壁有任何建议,我将不胜感激!

My movement script is:我的运动脚本是:

  public class Movement : MonoBehaviour
   {
     private Touch touch; 
     private float speedModifier;

 
void Start()
{
    speedModifier = 0.01f; 
}

void Update()
{
    if(Input.touchCount > 0) 
    { 
        touch = Input.GetTouch(0);
        if(touch.phase == TouchPhase.Moved)
            { transform.position = new Vector3(transform.position.x + touch.deltaPosition.x * speedModifier,
             transform.position.y, 
             transform.position.z + touch.deltaPosition.y * speedModifier);
             }
    }
        
}

} }

It would really help to see your movement script however it sounds like you are moving player by manipulating transform.position or using rigidbody.MovePosition() without setting isKinematic to true .查看您的移动脚本确实会有所帮助,但听起来您正在通过操纵transform.position使用刚体.MovePosition() 而不将 isKinematic 设置为 true来移动玩家。 The documentation says; 文件说;

If the rigidbody has isKinematic set to false, it works like transform.position=newPosition and teleports the object to the new position (rather than performing a smooth transition).如果刚体的 isKinematic 设置为 false,它的工作方式类似于 transform.position=newPosition 并将 object 传送到新的 position(而不是执行平滑过渡)。

This behaviour will ignore the collision.此行为将忽略碰撞。 Also you don't need rigidbody on every GameObject in the scene.此外,场景中的每个游戏对象都不需要刚体。 1 rigidbody on the player is enough to collide it with other objects.玩家身上的 1 个刚体足以与其他物体发生碰撞。

depend on what I got from your question, when you are playing and the player hit the wall, it goes inside;取决于我从你的问题中得到什么,当你在玩并且玩家撞到墙上时,它会进入里面; so the problem might be the mass of the wall.所以问题可能是墙的质量。 if you must add a Rigidbody to the wall so you need to add more mass to the wall also edit the border of the box collider and make them reasonably a bit wider, otherwise if you dont need a rigidbody on the wall simply keep just the box collider and it will works good.如果你必须在墙上添加一个刚体,那么你需要向墙上添加更多的质量,还可以编辑盒子碰撞器的边界并使它们合理地宽一些,否则如果你不需要墙上的刚体,只需保留盒子对撞机,它会很好用。 hope this answer help you, and it will be better if you can explain more the situation using some pics.希望这个答案对您有所帮助,如果您可以使用一些图片来解释更多情况会更好。

Using the Rigidbody position or applying force tends to cause the Rigidbody to clip through other colliders.使用刚体 position 或施加力会导致刚体穿过其他对撞机。 I recommend changing rigidbody.velocity instead of directly changing the position.我建议更改rigidbody.velocity 而不是直接更改position。

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

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