简体   繁体   中英

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. 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. 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.

For more help have a look at the following Unity Tutorial .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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