- Home /
Best way to Camera Pick multiple objects and move them together
Hello
Given that there are multiple GamObject cubes aligned to shape a terrain , each being in a different position and height ;
How to make a raycast radius bigger to select multiple blocks, perhaps put them in a list and then calculate where to put them to flatten out the ones in the middle ?
In this poor ms paint picture you can see that the inner circle there are the cubes mostly affected, and the outer circle has a lower scale on the height.
actually the terrain would be an array inside of an array. this allows for info to be stored about 2 dimentional coordinates. the circular selection area would be a list of coordinates reletive to a center.
then the list of circular coordinates could be quicky compared to the 2d grid array with the offset of the mouse.
from there there you would use whatever block search algorithyms or flattening algos to do whatever you want to do .
the thing is I already have a generated terrain in a 2d array, but with chunks I cant access any, is there a way to camera pick multiple ones ? no tthe one the mouse is exactly pointing at while picking
I think toddisarockstar is suggesting the best method for you. You don't want to use the camera to pick objects. If the camera follows the mouse perhaps the distance of the camera from the block the mouse is directly over might be a nice way to set the radius.
Personally I would only use the camera if it is every block currently seen by the camera that is to be altered.
Answer by Cornelis-de-Jager · Jul 11, 2017 at 09:26 PM
The answer you are looking for is SphereCastAll. With this you can make a Cylinder shape when raycasting. I used it for a Force Push type ability in one of my projects to push down a wall of bricks and works amazing. Heres the code since it works differently for normal Raycast:
using System.Collections.Generic;
[...]
RaycastHit[] getObjects () {
// Variables for Sphere Cast
Vector3 origin = transform.position;
float radius = 5f;
Vector3 direction = transform.forward;
float maxDistance = Mathf.Infinity;
// Layer is an optional parameter and works just like Raycast layer
// Counter to keep all the objects
RaycastHit[] hits;
// Do the Raycast
hits = Physics.SphereCastAll ( origin, radius, direction, maxDistance );
return hits;
}
You can then simply move the objects in the Array as you which.
So how do you take the hit GameObjects ? What is RaycastHit ? it seems very useless like that
never$$anonymous$$d, I got it. But is there a way to set the direction as a mousePosition ?
Answer by Nischo · Jul 11, 2017 at 09:02 PM
First you have to use an appropriate container for your blocks, an array is probably the worst brute force container for any kind of operation off that sort. I suggest using an Octree. If you want to do multiple operations you want to store it instead of discarding it after a single operation as it can get expensive to built. Then you can check which of the octree nodes are inside your sphere, once you have it narrowed down, you can check the individual blocks inside each node and use the distance to your sphere center to know how much affected the node is by that operation. Or you could calculate the Y position average in relation to your sphere center Y to change their position.
And a final thought: Having one GameObject per block, especially each with its own collider and mesh is extremely inefficient and you should consider to change that, depending on the size of the world. If it is only a couple 1000 blocks, you should be fine. If its way more and maybe you want to have more fancy effects, you will have to reconsider.
Hope that helps you, good luck!