- Home /
Formed puzzle piece and sense the existence of the character and enemy moving around
I have a few question and appreciate if anyone can help out.
Basically player will go around the maze to collect the puzzle piece. After collecting the puzzle piece, it will direct them to a scene to match the puzzle piece. As i am creating chinese games, so the puzzle piece will only have two pieces. So what i want to ask is how to match the piece. http://imageshack.us/photo/my-images/839/post05.png/
It is possible to "sense" that the person is coming near you? For example, player is walking toward someone and ask them some question.So what i want to do is when the person go near that someone, either the player click on the someone or the someone automatically sense that someone come near them?
I will have an enemy prefab walking around the scene. So what i want to do is when the enemy go near the player, the player's BP will reduce by 10. As the enemy prefab required to walk around the scene, i have actually created an animation for it and checked the 'isTrigger'. So if player go near it, player's BP will be deducted. But there is no effect( mean that no deduct of BP) at all. And i am thinking to let the player kill the worm by "stepping" on it. Something similar to super mario. Argh.. how can i do it?
if(hit.gameObject.tag == "worm") { HealthControl.LIVES -= 1; print(HealthControl.LIVES); // play a sound clip at the exact position we hit the object AudioSource.PlayClipAtPoint(hitSound, transform.position); }
*edit as my question are not very clear. sorry about that
Answer by ByteSheep · Mar 04, 2012 at 05:55 PM
I guess the main idea would be to create prefabs/puzzle pieces that can be collected and then check to see if all have been collected? You could probably achieve this by making the puzzle pieces check for collision with the player (meaning player has walked into them/collected them) using the OnTriggerEnter or OnCollisionEnter function. This could look something like:
var collected : int = 0; var totalpieces : int = 10;
OnTriggerEnter(Trig : Collider) {
if(Trig.gameObject.name == "player") {
collected++;
Destroy(gameObject);
if(collected==totalpieces)
{
// go to the puzzle scene - all pieces are collected
}
} }
This will check if the player has walked into the puzzle piece, then add one to the total collected pieces variable, then destroy/remove the puzzle piece and finally if all pieces are collected go to the puzzle scene. Let me know if anything in the code is unclear or if I misinterpreted your question :)
hello merry_christmas, thank you. you are right. i have actually figure out how to do it already. What bother me is the matching of the puzzle part.
As mention, i am creating chinese game, so it will be quite different from the normal jigsaw puzzle which requires alot of pieces to form it. So for my case, i only need two or three to become a "complete" puzzle. http://imageshack.us/photo/my-images/404/post005.png/ -> required player to find a missing piece. http://imageshack.us/photo/my-images/839/post05.png/ -> missing piece that player have found.
So i am headache how to match the two pieces correctly. As players will be collecting quite a number of pieces, then go to the scene and match it with the piece they collected. Any ideas how i can do it? thank you for spending time to help :)
Hmm, so you'd need each pair to have a different id, but the two matching pieces to have the same. Are all pieces instantiated as the same prefab or have you created a prefab for each image/piece ? If you are instantiating all pieces with one prefab and setting each ones texture via script then you could also give them individual tags, for example pair-1, pair-2, etc. You could also use tags if you are creating a prefab for each though..
ok. So after doing all this, how to do the validation part? Like comparing the tag they have? And usually when for$$anonymous$$g puzzle right, we will drag the piece to the respective place right, how to drag the puzzle piece to the position?
To do the drag part you would have to check the mouse position and set the puzzle pieces position accordingly, here are some links that might help: http://answers.unity3d.com/questions/32807/instantiate-object-at-mouse-position.html and http://unity3d.com/support/documentation/ScriptReference/Camera.ScreenToWorldPoint.html . Then perhaps check if a piece is touching another using OnTriggerEnter and then check if both have the same tag name. Perhaps like this:
OnTriggerEnter(Trig : Collider) {
if(Trig.gameObject.tag == gameObject.tag) { //both pieces have same tag/are fitting pieces }
}
Answer by ByteSheep · Mar 04, 2012 at 05:20 PM
You can use this to check the distance between two objects:
var other : Transform;
function Update() {
if (other) { var dist = Vector3.Distance(other.position, transform.position); print ("Distance to other: " + dist); }
if(dist<4) { //player is close enough print ("Player is close enough"); } }
Add this to each puzzle prefab and set the variable other to the player object in the inspector view.
Don't quite know what you mean with the puzzle pieces, hope this helps with question 2 though :)
hi thank you. I will try it out. Regarding the puzzle piece, it is quite similar in playing jigsaw puzzle. So in the beginning, i will show the players all the missing pieces for the jigsaw puzzle, so the players have to walk around the maze to find the missing piece. Any ideas how i can go about doing it?
anyway regarding this code, where should i put it to?? The player? and the code i should put it in the update function?
Answer by ByteSheep · Mar 05, 2012 at 03:12 AM
Afraid I can't edit the answer directly so here it is again:
var other : Transform;
function Update() {
if (other) {
var dist = Vector3.Distance(other.position, transform.position);
print ("Distance to other: " + dist);
}
if(dist<4)
{
//player is close enough
}
}
Add this to the puzzle prefab and set the variable "other" to the player object in the inspector view of the prefab.
Your answer
![](https://koobas.hobune.stream/wayback/20220613064743im_/https://answers.unity.com/themes/thub/images/avi.jpg)