#Rigidbody Follow function

1 messages · Page 1 of 1 (latest)

cloud wigeon
#

Alright. Post the code you have so far.

hollow parrot
#
void FixedUpdate(){
        //float dx = character.transform.position.x - transform.position.x;
        //float dy = character.transform.position.y + 0.8f - transform.position.y;
        //float t = Mathf.Rad2Deg * Mathf.Atan(dy / dx);
        //if (dx < 0) {
        //    t += 180;
        //}
        //vx = ratSpeed * Mathf.Cos(Mathf.Deg2Rad * t);
        //vy = ratSpeed * Mathf.Sin(Mathf.Deg2Rad * t);
        //transform.Translate(new Vector2(vx * Time.deltaTime, vy * Time.deltaTime));
        rigid.MovePosition(character.transform.position);
    }```
#

the commented code works, i just need it faster

opaque hollow
#

It would be simpler for you if you just set your velocity to Vector2 with vx, vy.

cloud wigeon
#

That is true, but not really a good solution.

opaque hollow
#

Why not?

cloud wigeon
#

Actually, I misinterpreted what you meant. Nevermind.

#

I was thinking about something else.

hollow parrot
#

so what the conventional unity way of doing this

#

i know i do most stuff not in an efficient way

opaque hollow
#

There isn't. All 3 ways that I mentioned would work. If you want the object to be moved by other forces too, you should probably use add force though.

hollow parrot
#

i just want whatever is going to make my fps not bad

cloud wigeon
# hollow parrot so what the conventional unity way of doing this

With Rigidbody.MovePosition, it is the way explained in the documentation script example.
If you copy it without modifications, it will work.

But the example uses GetAxis to produce a direction
While you must calculate it from knowing the base and target position

opaque hollow
hollow parrot
#

yes and that helped a little

#

but still i should have no issues getting high fps

#

this is a tiny 2d game

opaque hollow
#

You're wrong. Several thousand moving and colliding physics objects is super heavy for the physics system.

#

But you should share your collision matrix, because I feel like there might be an issue with it.

cloud wigeon
#

Is this relevant to the topic of the thread?
I have limited time.

hollow parrot
#
float dx = character.transform.position.x - transform.position.x;
        float dy = character.transform.position.y + 0.8f - transform.position.y;
        float t = Mathf.Rad2Deg * Mathf.Atan(dy / dx);
        if (dx < 0) {
            t += 180;
        }
        vx = ratSpeed * Mathf.Cos(Mathf.Deg2Rad * t);
        vy = ratSpeed * Mathf.Sin(Mathf.Deg2Rad * t);

        velocity = new Vector2(vx, vy);
        rigid.MovePosition(rigid.position + velocity * Time.fixedDeltaTime);
``` this works
hollow parrot
opaque hollow
stiff verge
#

Saw you created a thread. I'll leave it here . . .

opaque hollow
hollow parrot
#

im too stupid for DOTS

cloud wigeon
#

Then if you don't mind, I'll take it from here.

opaque hollow
hollow parrot
#

but factorio has millions of things

#

not thousands

opaque hollow
#

Yes. With dots that's possible. Also, it's probably not using physics for these million of things.

#

Only for some of them.

hollow parrot
#

not that many

#

modern cpu

opaque hollow
#

We already talked about it. I'm not gonna repeat myself. If you need, read our conversation from the very start.

hollow parrot
#

ive shown my collision stuff

#

nobody knows whats wrong with it

opaque hollow
#

And you still didn't share the collision matrix. So if there is anything to improve, you're not helping with it.

hollow parrot
#

ive been here many times

opaque hollow
hollow parrot
opaque hollow
#

Does the enemy need to collide with UI or Water?

hollow parrot
#

there isnt water

opaque hollow
#

Or with any of the other layers?

hollow parrot
opaque hollow
#

Also, is that from the physics2d tab?

hollow parrot
#

yes

opaque hollow
#

Okay. Is there any change in performance?

hollow parrot
#

testing now but i doubt it

#

no

opaque hollow
#

Welp, there's just too many possible collisions.🤷‍♂️

hollow parrot
#

im still failing to understand that

opaque hollow
#

What does the profiler physics module say?

hollow parrot
#

i can get the exact same performance out of a single python script

opaque hollow
#

Same? Or better?

hollow parrot
opaque hollow
hollow parrot
#

its showing all the physics stuff

opaque hollow
#

No it doesn't. That is not the physics module.

#

See profiler modules at the top left? Click and enable physics module.

hollow parrot
#

its enabled

opaque hollow
#

It shows 0 for everything...

hollow parrot
opaque hollow
#

Ok, that's better.

#

Okay

#

You have over 5k rigid bodies.

#

5 dynamic

#

95 static colliders.

hollow parrot
#

yuh

opaque hollow
#

I don't know how many of them are bullets, but if it's even just 4, you need to run 20+k distance checks. That's a lot. And I'm sure there's something else going on too.

hollow parrot
#

0 are bullets

#

so this evidently is not a good type of game to make as a noob?

opaque hollow
#

Most definitely not

#

If you're gonna use the normal physics system, at least make some limitations. Several hundreds of rigidbodies should be fine.

hollow parrot
#

i like a lot of rats

#

otherwise the game is too easy

opaque hollow
#

There are ways where you could optimize that without using dots(and the physics system), but non of them are beginner friendly.

hollow parrot
#

and here i thought unity was my saving grace

cloud wigeon
#

What FPS are you reaching currently?

hollow parrot
#

3-50

#

400-700 at the start of the game

hollow parrot
cloud wigeon
#

Are they spawning while the game is running, or are they all already spawned?

opaque hollow
#

You're mixing up a lot of things without even understanding them. Cs go is a completely different type of game and it's definitely not developed by a newbie.

hollow parrot
hollow parrot
#

mine is a 2d game with lil cute rats

opaque hollow
# hollow parrot

Also, this doesn't look like 5k rats, so I really think there might be something else to it...

hollow parrot
opaque hollow
opaque hollow
# hollow parrot thats not 5k?

Can't say for sure, but it doesn't look so. But if you can't tell for sure, I'd suspect that you don't completely understand what's going on in your code.

hollow parrot
#

i completely deleted the list of rats

opaque hollow
#

Anyways, I'm off to sleep. There are a few more things we could check(like inspecting the average frame, not a spike one) as well as checking the physics timestep.
If you want we could check that later.

cloud wigeon
#

With some basic optimizations, you could try it with up to maybe 1k Rigidbodies.
Creating objects is expensive, so we prefer keeping a number of them already in a pool, and then effectively Enabling/Disabling them as we "create and destroy" objects.

There is also the issue of how realistic and accurate you need the collisions; because Discrete is cheap, but will not work for high speeds etc. Dynamic is much more expensive, and if you need that, I imagine you'd need a system to control which ones are Dynamic and when.

Changing from moving by transform.Translate in Update, to Rigidbody in FixedUpdate, will probably improve the FPS slightly.
It sounds like your vision for the game is currently out of reach, but you could still make something out of this idea.

hollow parrot
cloud wigeon
hollow parrot
#

so what does fixed do

#

FixedUpdate

#

also now that i made the rats rigid bodies, not all of the collisions work

cloud wigeon
#

Update runs every visual frame.
FixedUpdate runs at a fixed timestep, set in the properties of the Physics system.

By default, Time.fixedDeltaTime is 0.02
which means it runs 50 FPS.

Update will try running at the target framerate, or default (as much as possible).

#

Time.deltaTime can be used within FixedUpdate() because it is effectively aware it has been that precise amount of time.
While in Update(), Time.deltaTime represents the time elapsed since the last frame. (which then varies)

#

Can you explain or show a video of how Collision should work vs how they are now?

hollow parrot
#

the bullets are colliding correctly but the saw blades are not which is weird because they run on the same system

opaque hollow
#

@hollow parrot if you upload your project to GitHub and share the link, I might have a look tomorrow. I'm kinda curious how far you can push the 2d physics and why you get the results you get now.

cloud wigeon
#

did you give them a Rigidbody or a Rigidbody2D ?

#

2D and 3D physics are entirely separate systems, and will not interact with one another.
That means Rigidbody (3D) won't work with BoxCollider2D etc.

hollow parrot
#

everything has 2d

#

the rats dont collide with the character either

cloud wigeon
#

do the rats collide with one another?

#

that would be expensive

#

Actually, nevermind, they are kinematic

hollow parrot
#

no they dont

cloud wigeon
#

Well, without more code, I can't be certain.

#

Have you rewritten the movement?

hollow parrot
cloud wigeon
hollow parrot
#
void Update(){
        float dx = character.transform.position.x - transform.position.x;
        float dy = character.transform.position.y + 0.8f - transform.position.y;
        float t = Mathf.Rad2Deg * Mathf.Atan(dy / dx);
        if (dx < 0) {
            t += 180;
        }
        vx = ratSpeed * Mathf.Cos(Mathf.Deg2Rad * t);
        vy = ratSpeed * Mathf.Sin(Mathf.Deg2Rad * t);

        velocity = new Vector2(vx, vy);
        rigid.MovePosition(rigid.position + velocity * Time.deltaTime);
    }```
cloud wigeon
#

This works?

hollow parrot
#

rn im trying to figure out why the bullet collisions work but none of the other collisions do

#

even though they are exactly the same

#

this is the saw blade

#

this is the bullet

#

and the scripts are the same

cloud wigeon
hollow parrot
#

Bullet.cs

#

sawBladeScript.cs

cloud wigeon
#

in your sawBladeScript you have movement code in Update.
Calling transform.position will conflict with the physics system.
Rigidbodies cannot be moved by transform.position if there is supposed to be any interaction.

#

the same goes for Rotate, if that is supposed to be affected (or can be)

#

are the saw blades Kinematic?

hollow parrot
#

they are dynamic

cloud wigeon
#

or rather, I assume the bullets are not kinematic?

hollow parrot
#

both are dynamic

cloud wigeon
#

I am saying Kinematic, not dynamic*

hollow parrot
#

what

cloud wigeon
#

Kinematic Rigidbody and Dynamic Rigidbody behave differently.

hollow parrot
#

what do they need to be

cloud wigeon
#

Kinematic Rigidbodies are not affected by other objects or forces. (For example a Player rigidbody, where you script your own physics in the Controller or Motor script. )
Dynamic Rigidbodies are however only affected by the forces of physics, such as the simulation of a collision between two objects.

hollow parrot
#

so i want kinematic

cloud wigeon
#

Kinematic moves by MovePosition()
Dynamic moves by AddForce()

#

Check your bullets. Are they Kinematic?

hollow parrot
#

they wer dynamic

#

i made them kinematic

#

although its still not changing

cloud wigeon
#

so it worked the way you wanted, when they were Dynamic

hollow parrot
#

it worked the way i wanted before i made the rats rigid bodies

cloud wigeon
#

but after that, you said the Bullet collisions worked, but the sawBlades did not

hollow parrot
#

yes

cloud wigeon
#

the way you solve the sawBlades, is by making them like the bullets

hollow parrot
#

what is different

cloud wigeon
#

Kinematic or Dynamic movement

#

if bullets work the way you want them to, and they are Dynamic
then the sawBlades also need to be Dynamic

hollow parrot
#

they were both dynamic

cloud wigeon
#

Your sawBlade script has something the Bullet script does not have.
Do you know what that is?

hollow parrot
#

nope

#

u can see both scripts

cloud wigeon
#

movement code by transform.Translate() inside Update
which will interfere with the physics system

hollow parrot
#

bullets are moved using transform too

cloud wigeon
#

not in this script

#

where is that code?

hollow parrot
#
void Update() {
        mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        x = gameObject.transform.position.x;
        y = gameObject.transform.position.y;
        float dx = mouseWorldPosition.x - x;
        float dy = mouseWorldPosition.y - y;
        t = AngleBetweenPoints(arm.transform.position, mouseWorldPosition) + 180;
        arm.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, t));
        transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, t));
        if (gameObject.name == "deagle(Clone)") {
            pistol();
        }
        else if (gameObject.name == "shottyPrefab(Clone)") {
            shotgun();
        }
        foreach (Bullet b in gameController.bullets) {
            if (b != null) {
                //b.transform.position = new Vector2(b.transform.position.x + b.vx, b.transform.position.y + b.vy);
                b.transform.Translate(new Vector2(b.vx, b.vy) * Time.deltaTime, Space.World);
            }
        }
    }```
cloud wigeon
#

post the entire script where that code is inside

hollow parrot
#

it uses transform

#

oh i fixed it

#

it was the physics matrix

cloud wigeon
#

Which layer was unaccounted?

hollow parrot
#

default

cloud wigeon
#

ok

hollow parrot
#

ok i think everything is working now

#

and i have to just settle for the fact that unity cant do that many collisions

cloud wigeon
#

That's good. Because I don't think I can help you further.
It is - for lack of a better term - wrong, to use transform-based movement with Rigidbody objects.
I'm not sure how it will pan out, but if you say it works, then good stuff.

hollow parrot
#

although one idea is why is a rat that is nowhere near the player checking for a collision

#

instead of checking all the rats why not just check the ones near the player

cloud wigeon
#

Indeed.

#

Two things:

  1. Comment your code, or a break from coding might cause you to lose bearings in your scripts.
  2. Consider rewriting the transform-based movement into Rigidbody-based.
#

Comments would also help those helping

hollow parrot
#

so rigid body movement is better in every way

cloud wigeon
#

No, but it would probably be an optimization.
However, if you are nowhere else using Rigidbody-based movement, it is fine.
I suspect you are currently only using the physics system to identify collisions.

#

The reason it would be an optimization, is because your transform-based movement is calculated every single visual frame.
And also, if you are to publish the game, it would be best to keep collision-detection to a fixed timestep, so that the game works the same for all framerates.

hollow parrot
#

what makes unity better than tiny game engines?

cloud wigeon
#

I am the wrong person to answer that question.

hollow parrot
#

ok

#

ty for your help with the collisions 🙂

cloud wigeon
#

You're welcome.
I was initially only intending to help with a single multi-stepped movement formula 😅

#

Good luck with your game.

hollow parrot
#

im still a bit confused at why java runs faster than unity if unity is c# and modern

opaque hollow
#

@hollow parrot I mentioned earlier, but if you're interested in finding ways to optimize this further, upload your project to GitHub or other source control system, and I can have a look. While, several thousand objects would definitely be an overkill, what you actually had on screen at that time should be possible without hindering the performance too much.

opaque hollow
opaque hollow
#

Now, I tried enabling Use multithreading(that apparently you have enabled judging from the profiler calls) with the default settings in physics2d - job options and it went down to 6ms at max which is even more amazing.

opaque hollow
#

Circle.
I know they used capsule, so it should probably be a bit more expensive.

cloud wigeon
#

nice

#

what happens if you increase the collider radius?

#

also, are they colliding with one-another?

opaque hollow
#

Capsules seem to add 1-2ms more.

opaque hollow
#

or rather the capsule size

#

And no, they don't collide with each other.

cloud wigeon
#

kk

#

what do you think causes the extra drain from the bigger size?

opaque hollow
#

Interestingly, enabling multithreading takes it back to 6 ms

cloud wigeon
#

huh

#

how big can it get then?

opaque hollow
cloud wigeon
#

ah

opaque hollow
#

But there were spikes that took more time

cloud wigeon
#

Sounds scalable 👍

#

to a point

#

Also interesting that small colliders would demand less resources.
Something to consider in the early stages of a prototype

opaque hollow
#

Yeah, still it's definitely not made for such a scale. That's why DOTS appeared in the first place

cloud wigeon
#

still, 1-3k is good stuff

#

What happens if you introduce a collidable rigidbody, which simply moves through the sphere?

#

You don't need to entertain me, but man I'm curious 😅

#

small colliders vs big/medium rigidbody

#

like a ship flying through an asteroid field

cloud wigeon
opaque hollow
#

discrete

opaque hollow
#

Adding a bigger rotating circle that collides and destroys the small red capsules significantly decreases the performance without multithreading. Just the physics update was going up to 100-200 ms. Admittedly, it was also, due to the fixed update snowballing due to fps drop. There were probably 3-4 fixed updates per frame making things worse.

#

But what amazing is that turning multithreading on, took it down to 6-10 ms

#

It also seems like the more spaced out the objects are, the better the performance is. So I guess my guess on quad trees is correct.