In the program, the texture runs back and forth randomly, and if it hits the walls, it changes the direction of movement. Everything is good, but the texture is controlled by two methods, one for reflection from the walls, the other for the chaotic movement. When they match, and the texture is close to the wall - it can pass through the wall.

How can something be done so that under no circumstances could it pass through the wall? Walls with Box Collider 2d and Rigidbody 2d

Why not under any circumstances - because this problem was even before the random element. Just sometimes, leave the texture to run across the field, and in half an hour it will fly through the wall as if nothing had happened. And with the random, this has always happened.

code

{ Vector2 dir; Transform myTransform; public float angle; public float vx = 1f; public float vy = 1f; public float speed = 1f; void Start() { InvokeRepeating("Running", 2, 3); myTransform = transform; dir = Vector2.up; myTransform.localRotation = Quaternion.Euler(0, 0, angle); } void Running() { var random = Random.Range(-10, 10); var random1 = Random.Range(-10, 10); var random3 = Random.Range(-10, 10); vy = random1; vx = random; angle = Vector2.Angle(Vector2.right, new Vector2(vy, vx)); if (random3 > 0) { angle = -angle; } dir = Vector2.up; myTransform.localRotation = Quaternion.Euler(0, 0, angle); } void Update() { myTransform.Translate(dir * speed * Time.deltaTime); } void OnTriggerEnter2D(Collider2D coll) { if (coll.gameObject.name.StartsWith("bbot")) { myTransform.localRotation = Quaternion.Euler(0, 0, angle - Random.Range(-80, 80)); } if (coll.gameObject.name.StartsWith("bleft")) { myTransform.localRotation = Quaternion.Euler(0, 0, angle + Random.Range(185, 350)); } if (coll.gameObject.name.StartsWith("bright")) { myTransform.localRotation = Quaternion.Euler(0, 0, angle - Random.Range(185, 350)); } if (coll.gameObject.name.StartsWith("btop")) { myTransform.localRotation = Quaternion.Euler(0, 0, angle - Random.Range(100, 260)); } } 

    1 answer 1

    Instead of moving your own body, give it to the power of physics on Unity. Calculate random force and instead of transform.Translate() do rigidbody2D.AddForce() then your object will move according to the laws of physics and not teleport on every frame. It is necessary to apply force in the FixedUpdate method where the physicist is calculated at each step, and not in Update which is performed before the frame change. And if your physics works and a small object slips through another object, then try changing the Interpolate and Collision Detection parameters in the Rigidbody2D object, which usually helps.)

    • Thank! rigidbody2d.AddForce () works completely with the same commands as transform.Translate? What are the nuances? Never just worked with him before. - Dmitrii
    • No principle is different. In transform you change the position of an object in space, and in a rigidbody set the direction and force that affects the object. Changing the position in space will be the engine with physics. If the texture has its own styler with a rigidbody then in general, it is not necessary to rigidbody collision, for you the engine will do it all. - KingPeas
    • something does not cheat. Did so { public Rigidbody2D rb; void Start() { rb = GetComponent<Rigidbody2D>(); } void FixedUpdate() { rb.AddForce( new Vector2(0, 10)); } } { public Rigidbody2D rb; void Start() { rb = GetComponent<Rigidbody2D>(); } void FixedUpdate() { rb.AddForce( new Vector2(0, 10)); } } { public Rigidbody2D rb; void Start() { rb = GetComponent<Rigidbody2D>(); } void FixedUpdate() { rb.AddForce( new Vector2(0, 10)); } } The body picks up speed and absolutely calmly passes through the walls. What to add to the code so that it simply rests against the wall, since these are 2 physical bodies with a mass? - Dmitrii
    • rirgidbody wall have a collider and rirgidbody ? Did you change the Interpolate and Collision Detection settings for Rigidbody2D? - KingPeas pm
    • Yes, I tried everything. Little sense, it still sometimes crashes. Transferred to 3D and scored, there the walls are stronger) - Dmitrii