#Snap to grid
1 messages · Page 1 of 1 (latest)
I'm a bit confused. Are you spawning in the plane or an object into an existing plane?
Both
The grid object which needs to be snapped is set as the child of the plane
The plane is meant to be a guide of sorts
It was just an idea that came to mind so I thought I'd try it.
Are they being created at the same time? If so, why not create the plane at the origin with no rotation, instantiate the game object with the plane as the parent, also centered at the origin so it's in the middle, then move the plane to where it needs to be placed with the correct rotation?
The problem is moving the plane where it needs to be
The approach I'm trying to go for is treating any angled surface as if it were the ground and then rounding the X and Z and then aligning to the surface of the plane
I can provide some visuals if it helps
I don't know how much this helps
Essentially, I need this snapping to work on any plane angle.
Are the objects being spawned the mine sweeper tiles?
Yes, sorry for the confusion I should have cleared that up
The numbers don't need to be worried about for now, I can figure that out later
I'm primarily focused on the snapping issue for now
Gotcha. So a first person shooter 3D Minesweeper and you are spamming in the tiles as they are exposed?
The numbers are more cosmetic for immersion. The player doesn't reveal mines, they place them and then can reveal them to explode whenever
They act like a mine launcher i guess and you can use them to rocket jump or kill enemies
I've been working on it for quite some time
It's a fun project lol
Nice, ahaha. Give me a minute and I can type up some pseudo code of how I would approach it.
Awesome, I appreciate the help
Raycast to point and get hit collider
Get the transform of the hit collider
Move the world space point of the hit to the local space of the collider using the transform
Round the local space hit position to the nearest whole unit in z and x,
from here you can take two appraoches:
instantaite the object in local space (using the local space hit point and Quaternion.Identity) and parent it using SetParent(parent, false) so it moves correctly.
or
transform the rounded local hit position back to world space and Instantiate at that point using the world hit and the same quaternion as the collider
You shouldn't need to cross product or do any particular linear algebra if you take advantage of moving between local and world spaces
Can you clarify what you mean by "Move the world space point of the hit to the local space of the collider using the transform"?
I have the transform which I got from Transform t = collision.contacts[0].otherCollider.transform;
would I be using InverseTransformPoint ?
Yup, that will put you in local space so you can work relative to the hit collider rather than in world space. Since you've local to the collider, the logic becomes the same as if the collider had no rotation
It's a pretty solid approach that's use by most things that require dealing with two objects with arbitrary transforms. If you put one local to the other, it changes the problem to a single arbitrary transform and the other is the identity, which is easier to work with and usually more performant
I assume it still would, but would this still work with a probuilder mesh?
Depends on how the collider is set up. If it's a mesh collider than the transform will always be the identity since all angles are just baked into the mesh. That means the position will work but the rotation won't but you could cheat it by setting the transform.up of your instantiated object newObject.transform.up = collision.normal;
That will update the rotation so make up and normal aligned
Wonderful news, it works for non-probuilder meshes!!
that's at least a step in the right direction!
I'll try this right now
When its instantiated, should I use quaternion.identity and then change the up?
That would work, yeah. You could also technically calculate the quaternion using the normal but setting the up afterward is easier.
So i believe the position is being screwed up when the mesh isn't a plane like the image on the right
the image on the left shows a probuilder mesh which has some modifications. the other surfaces get messed up as a result
I think its more of a positional problem rather than rotation
when you mention modifications, what do you mean?
If it's been moved/rotated then you'll need to apply both approaches
use the transform to get to local space and also use the normal to calculate the rotation. Hard to tell without seeing all the code but at a high level that should be all that is needed
Sorry if my wording wasn't clear, I'll try to rephrase that. Let's say I built a level entirely out of one mesh with a wide assortment of surfaces in probuilder. The picture on the left shows an example of one mesh that has multiple different surfaces.
and that doesn't work compared to the right image which is one flat surface as one mesh
How is the collider set up in that case?
It's a mesh collider
and is the probuilder generated mesh moved/rotated after it's created?
Nope
hmm, then the simpler (transform.up = collision.normal) approach should work, assuming the transform is the identity on the mesh
You may need to get in there with the debugger to figure out where it's breaking
I think I just thought of a possible solution
Going back to the imaginary plane, I think it might be helpful in this case. A plane is placed at the position of the hit point and aligned with the normal. Then instead of using the probuilder mesh to align itself, it uses the plane
constructing the imaginary plane is essentially the same thing as calculating the target rotation, since those things are interchangeable. You should be able to construct it without tangets and only the collision.normal
A plane is just a point in space and a normal vector
Hey! Sorry about the late response, I had to go to work. I ended up getting it to work! It works on all surfaces now regardless of whether or not its a probuilder mesh!