- Home /
how to find the normal of a side face from top view?
Hey all
This is my first post and i searched a lot however i could not find anything relating to my question. Basically I'm creating a top-down iPhone game, similar to a maze game, so I have the camera set to orthographic. For this game I want to have it so that when the user taps a wall, it instantiates 2 capsules either side of the wall. So far i have made a prefab with the two capsules contained in a empty game object and have gotten it to work on horizontal and vertical walls easily enough (this is shown in diagram A).
What i would like to do is make it so that wherever the user taps, it instantiates the prefab of capsules at the correct rotation even on a curved wall or pipe (diagram B). I am using a ray cast from the users tap so i have the point the tap hits the collider. I was thinking it might be possible to use the normal of the side face however not sure how to get this data as the face can not be hit with the ray cast. The only other alternative i can think of would be to have the wall in segments and rotate them inside unity (diagram c) so that i could use collider.transform.rotation but it would be so much better if it could work with any mesh.
Really appreciate any and all help. Im fairly good with code so all I'm really hoping for is a point in the right direction if anyone has any good ideas o has done this before.
Thanks in advance, Jackson.
Answer by Berenger · Feb 03, 2012 at 04:35 PM
Obviously, it would have been easy with vertical and horizontal walls. As you seem to want circular walls as well, I don't think you can do without going down to the mesh. You can access the triangle hit by the ray with raycastHit.triangleIndex, then you might be able to find the faces you need, those with a normal having y = 0 and sharing vertices. I suppose you have walls not perfectly orthogonale to the ground.
So, not really a solution, but a lead I think.
Answer by Owen-Reynolds · Feb 03, 2012 at 04:58 PM
You could child empty "spawn points" to all your objects. Maybe add basePoints which are at expected tap points. On a tap, find the nearest basePoint. Each basePoint has two child spawn points. Or, just orient the basePoints, and always spawn at local +/-3X.
Or, if you don't want preset areas, you could still find the nearest basePoint and use the orientation to spawn from the tap point. Or, could use that as a guide for where to do a sideways rayCast from, to find the exact face to spawn from.
In theory, you can get that orientation data from the verts tangent's. Tangent is exposed in the Unity mesh interface, but not sure if the modelling program will set them the way you need.
Answer by Jackson93 · Feb 04, 2012 at 02:14 AM
Thanks for both your help. I haven't used the triangleIndex yet but i will have a go at that. If that doesn't really work for me I'll try using base points to guide the sideways ray cast.
Your answer
Follow this Question
Related Questions
Cant understand why object rotates when setting transform.up to normal 2 Answers
Automatically rotating objects to fit on to others 0 Answers
default cylinder to indicate hit.normal 1 Answer
Why does the raycast hit only objects/faces of a certain rotation? 1 Answer
Rotating to match normal: Quaternion.FromToRotation(player.up, hit.normal) 1 Answer