How do I add extra points for coming close to obstacle but NOT for coming close and colliding?
Here is what I have to check to see if object is close:
void CloseCall()
{
GameObject[] x= GameObject.FindGameObjectsWithTag("xx");
foreach(GameObject target in x) {
float distance = Vector3.Distance(target.transform.position, transform.position);
if(distance < 10 ) {
//if in a certain distance add extra points for near missing obstacles with tag
}
}
FindGameObjectsWithTag()
is slow. And doing that every frame is even slower.
Ins$$anonymous$$d of that why not add an appropriate collider to each GameObject, but resize the collider so that it is larger than the object itself (to the appropriate size). $$anonymous$$ark the "Is Trigger" checkbox on the collider. Then add a script with an OnTriggerEnter(), or OnTriggerEnter2D(), something like this (just an example)
using UnityEngine;
using System.Collections;
public class EnemyScript : $$anonymous$$onoBehaviour
{
private PlayerScript playerScript = null;
// Use this for initialization
void Start ()
{
if ((GameObject.FindWithTag("Player") != null) && (GameObject.FindWithTag("Player").Getcomponent<PlayerScript>() != null))
{
playerScript = GameObject.FindWithTag("Player").Getcomponent<PlayerScript>();
}
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
if (playerScript != null)
{
playerScript.points++;
}
}
}
}
If you already have a collider on the object then add a child object (call it Hitbox), add the collider to the HitBox game object. Set the Is Trigger
checkbox on the collider. $$anonymous$$ake the collider the appropriate size.
Note: You also need to add a Rigidbody (or Rigidbody2D) to one of the objects (preferably the player). And you may want to disable gravity (depending on your game type).
Won't the player still get the points if they collide with the object because they are going through the trigger ?
Depends what you are looking for and how big you make the collider.
Imagine a square and the collider is twice the size of the square. The trigger will occur when the player reaches the edge of the collider, not when it collides with the edge of the square.
And if you want you can add a flag that is set when the player enters and exits the collider, loke so
using UnityEngine;
using System.Collections;
public class EnemyScript : $$anonymous$$onoBehaviour
{
private PlayerScript playerScript = null;
private bool triggered = false;
// Use this for initialization
void Start ()
{
if ((GameObject.FindWithTag("Player") != null) && (GameObject.FindWithTag("Player").Getcomponent<PlayerScript>() != null))
{
playerScript = GameObject.FindWithTag("Player").Getcomponent<PlayerScript>();
}
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player") && (!triggered))
{
triggered = true;
if (playerScript != null)
{
playerScript.points++;
}
}
}
void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player") && (triggered))
{
triggered = false;
}
}
}
Answer by CCGnomes · Oct 03, 2016 at 07:07 PM
I think i figured it out. My solution was kind of what you said with the triggers but check the players health as soon as it hits trigger and on trigger exit have it add points ONLY if the player has the same health. Or for example if I didn't have health. Use a count system that checks to see if the number that it is before it hits the trigger is the same after it exits trigger and have it increment on collisions.
Finally had a brain blast today. Hopefully this can help someone in the future.
Your answer
Follow this Question
Related Questions
Help With Score C# Error CS0131 1 Answer
Scoring Points with UI Problem! 0 Answers
reload game 1 Answer
UI printing in multiple sections.(not supposed to) c#(Solved) 1 Answer
Spawn Object on Set Score 0 Answers