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.

  1. 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.
    ImageShack - Best place for all of your image hosting and image sharing needs

  2. 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?

  3. 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

3 Answers

3

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 :)

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 :)

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..

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.