I have a script with a variable that says whether or not the enemy is stunned (from a stun gun), and I wan tit so the player can get about 1-2 meters away from the enemy, and then pick it up, thus adding time and points to the time and score. I have the add score, etc set up, but i cant figure out how to have the 1-2 meters away, and you can “Pick it up” (it would just add to the score, and Destroy its self)
Are you asking how to find the distance?
If you are, use Vector3.Distance() http://unity3d.com/support/documentation/ScriptReference/Vector3.Distance.html
I know how, I just dont know how to set up a system of doing so
I don’t understand what you are asking for, so I’ll keep shooting scripts at you.
This one return true if the player is 1-2 units away from the enemy.
var enemy : Transform;
function Update() {
if(Vector3.Distance(transform.position, enemy.position) > 1 Vector3.Distance(transform.position, enemy.position) < 2) {
Pickup();
}
}
ok, 2 things, im writing in C#, and there are multiple enemies. I can convert most UnityScript stuff into C#, though
try a tag?
i had some problems with distance and using a tag before, it would only register the last one that was put into the scene, but if u can get it working boy sure let me know! lol
Do you have an array of enemies that you can access? If you do just cycle through the array and check the distance for each one. If the enemy is within the required distance, “pick them up”.
If you don’t already have an array of enemies and they are tagged you can use “FindGameObjectsWithTag” which will return an array of enemies that you can use.
I already have a tag system done, and that gave me an idea
how would I access all of the scripts of all of the enemies (one script in particular) to check if isStunned is true?
Im also encountering a problem with this script
sing UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PickUpEnemy : MonoBehaviour {
private GameObject[] enemies;
public float pickUpRange = 1.5f;
private Transform keeper;
private ScoreKeeper keep;
private bool inRange = false;
// Use this for initialization
void Start () {
keeper = GameObject.FindWithTag("GameController").transform;
keep = keeper.GetComponent<ScoreKeeper>();
}
// Update is called once per frame
void Update () {
enemies = GameObject.FindGameObjectsWithTag("Enemy");
foreach (GameObject value in enemies) {
if (Vector3.Distance(transform.position, value.transform.position) <= pickUpRange) {
if (Input.GetKeyDown(KeyCode.E)) {
Destroy(value);
keep.score += 4;
keep.time += 30;
}
inRange = true;
}
[COLOR="yellow"]else if (Vector3.Distance(transform.position, value.transform.position) > pickUpRange) {
inRange = false;
}[/COLOR]
}
}
void OnGUI () {
if (inRange) {
GUI.Box(new Rect(Screen.width / 4, Screen.height / 4, 120, 25), "Press E to pick up");
}
}
}
specifically the yellow part. i add this, the GUI doesnt show up, i remove it, it works, but the GUI doesnt go away
find game OBJECTS ,plural, genius! lolll
as for your last script, i dont know C#, but if you know the similar term to the “else” statement in unity javascript you might be able to do something like
void OnGUI () {
if (inRange) {
GUI.Box(new Rect(Screen.width / 4, Screen.height / 4, 120, 25), "Press E to pick up");
}
else
{
return false;
}
}
or if that doesnt work, you can try to always have that button already existing in the scene just off screen, and change the location of it when you need to see it and bring it back off screen when you dont
your return statement doesnt work in C#
any one?
Since it’s looping through all the enemies, inRange is getting set based solely on the last enemy in the array. You’ll want to set inRange to false before the loop, then take out the else if statement. That way it will be false by default but get set to true if any enemy is in range.
You could also return immediately after finding one enemy that is in range, no need to cycle through all of them once one is found to be in range.
FindGameObjectsWithTag is a fairly slow operation. Instead of running it every frame, get a list of them at startup, then every time you create or destroy one, add or remove it from that list.
ok, it works!!! just the other problem “how would I access all of the scripts of all of the enemies (one script in particular) to check if isStunned is true?”
When you are looping through your enemies:
CertainScript script = enemy.GetComponent<CertainScript>();
if(script.isStunned)
{
// Do something
}
thanks, works perfectly, you dman!! (you da man?? hardy har har)