Question about how to render or not render parts within camera view?

So I read another post a while back that said something like “To make your NPC’s more efficient and less laggy in masses. You can only render NPC’s and their animations only when a player can see them or is in X distance”

How is this done? Like lets say I only want to render a character when he’s in the camera view? How do you do that, do you constantly have a script running checking if player x Root part has the NPC root part within his camera view and at that point by rendering do they mean just make the character invisible and anchor him? Or is there some totally different other method to be used here?

Thanks.

https://developer.roblox.com/en-us/api-reference/function/Camera/WorldToViewportPoint
You can use this function to determine whether something is on the screen or not.

Not sure about the performance difference if you do this though.

Yea I know how to use a raycast to check if it’s visible. But I’m wondering, when people say “Render a unit or not” how exactly is that done? Do you remove and add character based on if a player can see him there. Maybe replace character with a placeholder invisible brick. And spawn character there, or do you just make all their parts transparent and anchor their root part? This way no physics are running afaik.

And perhaps also disable and re-enable their scripts when they’re active vs not active.

Well, I wouldn’t worry about it.
It won’t make that much or a difference.

If you were to do it though, I would approach it like this:

  1. Check visibility every second or so for all NPCs that have animations playing or something.
  2. Have just one script that handles all the NPc animations, and fire an event with the NPC instance as an argument.
  3. The script pauses all animations, and anchors the NPC. Maybe makes it invisible too, if you want.

Ok so another question.

I think it would be best to do the animations locally and just have an unanimated zombie server side.

How do you do something for only 1 player? I’m recently coming back to ROBLOX and it’s been a while gotta get back up to speed.

Like do I just run a local script inside starter player scripts and run the animations there and it only applies to the person who had ran the script? At that point I then check if x player can see zombie.

So this way only the player who sees the zombie will render his animations and not also render all the other players who see zombies in different areas?

Welcome back.

Yes, you can use a localscript.

You can also use a remote event and fire it to the designated player.

Thank you, remove events and that stuff I never got the hang of before I quit back in the day. I’ll read up on it. Thanks.

1 Like