简体   繁体   English

阻止我的2d对象通过盒对撞机

[英]Stop my 2d object from going thru box collider

So right now I have a box that I can move left/right, and on the sides I have other boxes to stop the player from going into the void. 所以现在我有一个可以左右移动的盒子,在侧面,我还有其他盒子可以阻止玩家进入虚空。 Hard to explain, but basically I have the player in the middle, and then a box on each side to stop the player from running off into the void. 很难解释,但是基本上我在中间有玩家,然后在每侧都有一个框来阻止玩家逃入虚空。 Even though I have set everything up with box colliders and ridgidbodies, when I tilt my device all the way to a side the player goes straight thru the boxes, but is I tilt the device slightly the player kinda bounces on the sides and it gets very jerky. 即使我已经使用盒子对撞机和ridgidbody进行了所有设置,当我将设备一直倾斜到一侧时,玩家还是会直直穿过盒子,但是如果我稍微倾斜设备,则玩家会在侧面上弹跳,并且会变得非常生涩的 How can I make some barrier that will be unpenetrable by any object? 我该如何制造任何物体都无法穿透的障碍? Like what do I need to do to make a wall so that nothing will be able to go thru it? 就像我需要做些什么来砌墙,使任何东西都无法穿过它? Here is the code I am using to control the player, thank you! 这是我用来控制播放器的代码,谢谢!

using UnityEngine;
using System.Collections;

public class Tilt : MonoBehaviour {

void FixedUpdate ()
{
    transform.Translate(Input.acceleration.x * Time.deltaTime * 10, 0, 0);
}

} }

You are having this problem because you are using transform.translate to move your character. 您遇到了这个问题,因为您正在使用transform.translate移动角色。 This method will place your character at the assigned position regardless of if there is any collider or not. 无论是否有对撞机,此方法都会将角色放置在指定位置。

As you said you have attached a RigidBody to your character so try using rigidbody.velocity to move your character. 至于你说你已经安装有刚体,以你的性格,那么请尝试使用rigidbody.velocity移动你的性格。

For more help have a look at the following Unity Tutorial . 要获得更多帮助,请查看以下Unity教程

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

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