- Home /
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
I would guess that the cause of your problem is not anything you've told us so far. Velocity shouldn't change without something somehow modifying it, and the challenge is going to be finding out what's modifying it. And I'm sorry, but it's most likely your own code, somewhere, somehow.
So you've debugged these values? If they're through the roof, why not try something like:
float $$anonymous$$axValue = 10.0F;
rigidbody2D.velocity = new Vector2($$anonymous$$athf.Clamp(rigidbody2D.velocity.x, 0, $$anonymous$$axValue), $$anonymous$$athf.Clamp(rigidbody2D.velocity.y, 0, $$anonymous$$axValue));
That'll (probably) make sure the values do not go above $$anonymous$$axValue (10 in this case)... Hope it helps!
Well, that's not really my problem. Sorry if my description was not clear, but I do not need to controll the magnitude, but the vector (both x and y values), because this actually causes balls to leave the camera area.
Answer by brycem24 · Sep 03, 2014 at 10:20 PM
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.
Thank You for answering! I know that triggers do not block colliders, but my script attached to those triggers should, and it does, in most cases. I'm 100% sure, the script is attached to all four bounds of my area. Probably it doesn't really matter, whether 2D uses Y or Z, beacuse I always use Vector2s, so I just operate on the second component, which is this Y or Z.
Answer by LSPressWorks · Sep 03, 2014 at 10:36 PM
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
}
Thanks for Your answer! But I fear, that's not a solution for my problem :/ I don't need to regulate the magnitude, but the vector. It is just somehow changed to a vector, that leads the ball out of bound, what never should happen...
How much code is in this project? I think we might need to see it all.
I've posted an answer with a full description of a testing-project, where the same problem occurs, but it's still waiting for a moderator.
Answer by mayonez · Sep 05, 2014 at 09:44 AM
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.
Are you sure the ball doesn't hit the same trigger twice? $$anonymous$$aybe try making them really wide ins$$anonymous$$d of super narrow.
Yes, I'm sure it doesn't hit twice. I checked with a debug and actually this random velocity change does not act like my scrpit. I mean, when the velocity vector is f.e. [2, 2] and the ball enters the upper trigger, the velocity is changed to [2, -2], so it's done properly. But then, a few frames later, it's changed to [0.5, 1.6] and the ball leaves the screen. That's how it looks like.
Ok, I've just made the triggers wider, just in case, but it gave nothing.
I can't duplicate this. I copied your code into a little scene, and it worked just fine; nothing escaped, nothing even behaved weird.
But have You tried to put more than one ball in the scene? For one ball it works fine also for me, but if there's about 40, it doesn't.