I’m trying to make a trigger boxcollider2d follow another trigger boxcollider2d with some added offset.
The problem is I’m getting performance problems when there’s several colliders following.
Mind you I’m not talking about thousands, I’m talking about 50 or so.
There’s millions of forum posts saying something different and I need to know the correct and most performant way.
I tried positioning them in lateupdate by setting the GetComponent().position, which works but starts to become sluggish with a couple of them on screen.
From reading most forum posts, I understand that it’s advised that you set the position of the rigidbody in fixedupdate instead of the transform, but this makes the object vibrate when moving around with my player.
You should not be calling GetComponent every frame. Instead call it once and assign the rectransform to it’s own variable and get the position from that.
Are you offsetting the collider through the transform or through the rigidbody center? It might be worth offsetting through the center. Unity Doc: Rigidbody
If you’re using a rigidbody and it’s suppose to collide with stuff using physics, you should never be setting the transform position/rotation directly (except to teleport). Doing that bypasses collision detection.
You need to stop setting position and get use to updating velocity instead. Acceleration (add to/subtract from the velocity) is better, as then the physics engine may manipulate the velocity without you constantly counteracting what it’s doing. This is why you see “vibrating”. The physics engine is wanting to collide, but you keep saying “I don’t care, move here” and it has to obey… only to try again the very next FixedUpdate.
Ye but the vibrating part happens without collisions, with just 1 object on screen.
I assume because the position only updates each fixed update and the camera renders alot faster.
As you can see with the chest, it’s smooth cause it uses the transform to position in lateupdate.
But the gold uses the rigidbody positioning in fixed update.
So how would I go about positioning a rigidbody at a certain position by settings it’s velocity and making it smooth follow?
I see this is a HUD positioning problem. I know you are already able to position these accurately, but why not use Camera.WorldToScreenPoint? If you want “locked” follow, center your rect on that (plus offset). If you want an accelerated “smoothed” follow, I’d still use that function to at least determine your target coordinate, calculate difference between target and current position, and move by some fraction of that distance.
I don’t understand need for colliders here.
I also would do this in Update instead of FixedUpdate, since you only need your HUD elements to update once per frame.
The reason I need colliders is that I need to detect overlap so that they can stack on each other.
Like diablo loot, but as I said with only 20-ish items on the floor it already starts to become sluggish because of the constant triggering and positioning I guess.
And then there’s this forum post which states that positioning them by rigidbody is 10X more performant, so that’s what I’m trying to do.
You do realize that this is a trivial calculation that doesn’t need the breadth of an entire physics engine behind it, right? Sure, you may be writing less code, but that doesn’t mean less code is going into it.
What I don’t think you noticed on that same thread as your referenced post, is that someone a few posts down points out the reason behind the improved performance. It is only faster to use rigidbody because they have colliders attached.
That said, if you remove the physics components from your HUD objects and detect collision on your own (these are unrotated boxes, it’s literally grade school level math here), I promise you will see vast improvements to performance.
Here, I’ll get you started:
If boxA’s LEFT edge is to the LEFT of boxB’s RIGHT edge, and boxA’s RIGHT edge is to the RIGHT of boxB’s LEFT edge, you know they are overlapping on the X axis.
Think about that for a bit, and I’m sure you’ll see the logic is simple and can directly apply it to the Y axis too.
Using the physics system seemed to be the most logical solution. Seeing as I need to detect collisions…
They have box colliders attached, so they can check for triggers.
I’m not a moron
I realise there are different ways of approaching this, but I went with the physics system cause that’s exactly what I needed.
What I don’t know, is how heavy every unity system is on the cpu/gpu.
I guess I can try to detect it manually, but that would still mean checking overlap every frame, hence why I just used boxcolliders with triggers, cause why wouldn’t I.
Why wouldn’t you? I just told you:
“Sure, you may be writing less code, but that doesn’t mean less code is going into it.”
Actually quite the contrary.
Not trying to insinuate anyone’s a moron with my explanations. Just providing as much guidance as I can without writing everyone’s code for them. I mean, we all have to hear these things at some point, and I never know who’s already heard them.
Not exactly what you need. You’re trying to use a bulky system for simple overlap detection. And I hope the performance you’ve witnessed speaks to how heavy this particular system is on processing.
Now, that said, if you were trying to detect overlap between rotated pentagons, triangles, etc. you’d benefit from using the built-in trigger system for overlap detection. You could still use manual calculations to determine overlap for your specific shapes and squeeze out better performance, but at that point it wouldn’t be as great of an improvement as you’d be seeing with your setup of unrotated boxes.
I don’t know man.
I don’t really see a solution where I can do this without 2d collision.
Sure I can check if they overlap manually, but then I’d need to reference each label somehow.
How could one label detect who it overlaps with when I’m running around without a collider?
You could use a “labelManager” script (with static self-reference field) to handle all labels that should be on-screen.
A self-referenced class like this needs to ensure it is the only one of its kind, which can be done like so:
public class GameController : MonoBehaviour
{
//
// GameController is responsible for global game functions
//
public static GameController control; // Static access to singleton gamecontroller instance
// Awake fires before Start does
void Awake() {
if (control == null)
{
control = this;
DontDestroyOnLoad(gameObject); // Only do this if your controller must remain between scenes
} else
{
if (control != this)
{
Destroy(gameObject);
}
}
}
}
And now, from any other script in your game, you can reference this controller like:
GameController.control
With that in place, have your controller maintain a list of labels and the object they belong to.
In Update you can then iterate through all labels, perform overlap calculations, and adjust positions from a single script.