- Home /
is there a way to assign north south to an object?
Hi,
I am pretty new to unity3d, so i m asking in hope that there's someway to this. what i m trying to do is have a panel, the panel would have two cube on each side of the panel. the two cube is 10x, 5y,and 1z. sitting on each edge of the panel, so you have a walkway for a hall. all of this is inside an empty gameobject call hallway
I want to be able to have another gameobject look at the hallway object and determine if its looking at the side with the cube or if the side its facing is the side without the cube. The hallway object can can randomly turn 90 %. the second object cannot touch the hallway object.
I hope i m explaingin this correctly.
An illustration would probably help , but the general idea would be that you have to figure out if object B is visible to object A and check if B is facing the right way. It depends on how precise all that has to be. If your trying to make a simple puzzle similar to the light puzzles in certain games where a beam of light would go from object A to B to C and so on... the easiest would probably be to use a RayCast.. if it's a matter of B being visible to A.. then you could try to do it other ways.
You could try using cameras for that and do something like http://answers.unity3d.com/questions/8003/how-can-i-know-if-a-gameobject-is-seen-by-a-partic.html but it would probably be better if you'd find some other way to do that ins$$anonymous$$d of having multiple cameras for that
What I am doing is randomizing GO that contains panels and walls to make a maze. and then randomizing a spawn point for the player. the problem is that the player can spawn on a set of panels that is part of the GO which are all block and cannot exit out due to how each GO rotates (also being randomly chosen.
this way when playing the game again the next time the maze is completely different than it was before.
edit: I got it to working via adding a script to the GO that will validate if it turn or not, and the main script will check the GO's component for the variable, it it holds true it turned, so it will then turn with it as well.
the maze now has not block after running it a few dozen times just to check and increasing the size value of the maze to 500 panel (each panel is 10 unit). and no block, but it not completely randomize as it depends on teh one before and the below it.
This gives me a new map each time i replay the scene, even though there's still some $$anonymous$$or bug on it.
Answer by BigRoy · Jan 03, 2014 at 10:30 AM
You could take the position of both cubes and subtract the viewer's position from it. This way you get a vector from the viewer to the cubes.
Vector3 Vcube1 = Cube1.position - Viewer.position;
Vector3 Vcube2 = Cube2.position - Viewer.position;
Vector3 LookDirection = Viewer.forward;
float dotCube1 = Vector3.Dot(Vcube1.normalized, LookDirection);
float dotCube2 = Vector3.Dot(Vcube2.normalized, LookDirection);
The one with the higher dot product is the one that is mostly in line with the look direction so is likely to be the one you're looking at.
Looking at the comments on the original post I probably misunderstood the question.
Checking if you're looking at the cube or the opposite direction
You can check whether you're looking at the cube or not with a single dot product of that cube with the LookDirection. If it's above 0 it's ranging from almost perpendicular to looking at the object to fully looking at the object. If it's below 0 it's ranging from almost perpendicular to facing away from the object. And zero would be perpendicular.
Assuming the vectors were normalized before you got the dot product you can easily get the angle by doing:
float angle = Mathf.Acos(dotProduct);
Otherwise you'll have to do:
float angle = Mathf.Acos(dotProduct / (V1.magnitude & V2.magnitude));
Answer by finae · Jan 05, 2014 at 06:09 AM
You completely lost me , as mentioned i m still pretty new to unity3d. I did however tried a few scenarios and found that i can attach a custom property to the empty GameObject (GO) during runtime as all GO for the maze is generated at Runtime via Resources.Load().
what i end up doing:
public class wProperties : MonoBehaviour public class wProperties : MonoBehaviour{ bool bForward; bool bBack; bool bLeft; bool bRight;
public setAvailPath(bool isAllow, iType){
switch(iType)
{
case 0:
bForward=isAllow;
break;
case 1:
bBack=isAllow;
break;
case 2:
bLeft=isAllow;
break;
case 3:
bRight=isAllow;
break;
}
public bool getForward(){return bForward;}
public bool getBack() {return bBack;}
public bool getLeft() {return bLeft;}
public bool getRight() {return bRight;}
}
on the main script, i would reference the script component to the GO and ask if forward/backward (z coord) is available, or left/right (x coord). if it is not, than i can re-randomize the current GO with a different maze piece.
I have not completely tried but on the generator project but did test the properties above on a new project and the values returned as needed.
Thanks.
Your answer
Follow this Question
Related Questions
Remove object assignment in runtime? 1 Answer
How can I assign a GameObject to a variable at runtime? 0 Answers
Assign a gameobject in a scene to a varible automatically? 0 Answers
Keeping old GameObject values 1 Answer
Raycast object tag check? 3 Answers