Rigidbody2D.velocity out of controll

Hi Guys!

I’m quite new to Unity (I’m working on my first project). In my game, I have a reactangle area, in which there’s a couple of balls. These balls aren’t affected by gravity, nor friction. I only add them a force at launch and then I set them free.

But, Unity’s built-in colliders are a bit too realistic for my game. Instead of four edge colliders, I use four very narrow trigger box colliders, with a really simple script. F.e. if a ball enters the upper trigger i change it’s velocity from [x, y] to [x, -y], and so on.

And generally, everything’s working nearly fine. But, as the game goes on, some balls just disappear! They approach a trigger, enter it (checked with a debug), their velocity is changed properly (also checked), but then, somehow (with no collision or antyhing like that), a few frames later, these velocities change to some random values that make them leave the screen. I don’t know, if it’s my fault, or maybe Unity’s physics engine ;), but i certainly need Your help!

Best regards,
mayonez

Triggers do not block colliders. You could add both a box collider, and a box trigger to handle this. If this somehow screws up your script. I would verify that you have your script applied to all of the trigger areas. If this causes problems for the bottom ones perhaps I would change the velocity from x,y to according to which side it is on. Bottom remaining +Y. I just remembered in typing this for another thing you should watch out for. I believe in my experience with 2D. Y is not used everything is X and Z so I would watch out for that in your trigger script aswell. This could be when the ball touches it the velocity is changed so rather than the ball touching the collider and going back down its actually going to the side and past your camera giving the illusion of it dissapearing.

You can always cap the speeds in code with clampf or a couple of simple if statements to measure the velocity and adjust accordingly.

if(this.rigidbody.velocity.magnitude > someNumber)
{//then do stuff

}

Okay, I read Your answers and thought that it’s probably my code’s fault. So I decided, to cut this part of my game, and put into a separate, testing project. Now, this testing project contains only two real object - bounds, and balls. I’ve attached the following script to the bounds (I set all four Mods depending on the position of a bound - whether it’s top, bottom, left or right):

using UnityEngine;
using System.Collections;

public class BoundScript : MonoBehaviour {

	public int xMod, yMod, xPosMod, yPosMod;

	void Start() {
		Vector2 stw = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
		transform.position = new Vector2(stw.x * xPosMod, stw.y * yPosMod);
	}

	void OnTriggerEnter2D(Collider2D c) { 
		c.attachedRigidbody.velocity = new Vector2(c.attachedRigidbody.velocity.x * xMod, c.attachedRigidbody.velocity.y * yMod);
	}

}

And this one to all of the balls:

using UnityEngine;
using System.Collections;

public class ObjectColliderController : MonoBehaviour {

	void Start() {
		rigidbody2D.AddForce(new Vector2(35, 35));
	}

}

There’s literally nothing (apart from the camera) on the scene, but the problem remains the same…

Thanks for all help, mayonez.