I know no C#, as I’ve just recently begun using Unity, so please understand that I am a complete beginner.
I’m making a game where the player needs to stand near a door, and if I press the E key, it deletes the door and plays a sound. I’ve looked up a lot of tutorials, and I’ve found some that are nearly exactly what I need, but they are written in JS, which I know even less about.
How would this be done?
here’s something i ripped out of one of my old projects. It only returns the closest GO, but it’s a start:
public GameObject FindClosestObjectWithTag()
{
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag("Proximity");
GameObject closest = null;
float distance = Mathf.Infinity; //max radius size
foreach (GameObject go in gos)
{
Vector3 diff = go.transform.position - transform.position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
return closest;
}
Just add a “Proximity” tag to your door (rename to your liking)
[EDIT]
Here’s an example more aligned with your original question (untested):
using UnityEngine;
using System.Collections;
public class DestroyClosestDoor: MonoBehaviour {
/*Attach this script to your player gameobject*/
AudioSource m_audio;
public AudioClip clip;
void Start() {
m_audio = gameObject.AddComponent<AudioSource> ();
m_audio.clip = clip;
m_audio.Stop ();
}
void Update() {
if (Input.GetKeyDown (KeyCode.E)) {
var go = FindClosestObjectWithTag ("Door");
GameObject.Destroy (go);
m_audio.Play ();
}
}
public GameObject FindClosestObjectWithTag(string tagName)
{
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag(tagName);
GameObject closest = null;
float distance = Mathf.Infinity; //max radius size
foreach (GameObject go in gos)
{
Vector3 diff = go.transform.position - transform.position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
return closest;
}
}
CloudyVR:
here’s something i ripped out of one of my old projects. It only returns the closest GO, but it’s a start:
public GameObject FindClosestObjectWithTag()
{
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag("Proximity");
GameObject closest = null;
float distance = Mathf.Infinity; //max radius size
foreach (GameObject go in gos)
{
Vector3 diff = go.transform.position - transform.position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
return closest;
}
Just add a “Proximity” tag to your door (rename to your liking)
[EDIT]
Here’s an example more aligned with your original question (untested):
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
/*Attach this script to your player gameobject*/
void Update() {
if (Input.GetKeyDown ("E")) {
var go = FindClosestObjectWithTag ("Door");
GameObject.Destroy (go);
}
}
public GameObject FindClosestObjectWithTag(string tagName)
{
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag(tagName);
GameObject closest = null;
float distance = Mathf.Infinity; //max radius size
foreach (GameObject go in gos)
{
Vector3 diff = go.transform.position - transform.position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
return closest;
}
}
Thanks, but I don’t know how to code in the least. Where do I put the key input?
Just create a new script called DestroyClosestDoor and copy/paste the above DestroyClosestDoor class to it.
Then add that script to the gameobject that will be approaching the door.
As for the key input, it’s already taken care of in Update();
CloudyVR:
Just create a new script called DestroyClosestDoor and copy/paste the above DestroyClosestDoor class to it.
Then add that script to the gameobject that will be approaching the door.
As for the key input, it’s already taken care of in Update();
My console says that it doesn’t understand what “E” means, it says ArgumentException: Input Key named: E is unknown…", although I’m not getting any compiler errors that aren’t allowing me to play the game.
I’m not sure if I should be inputting something, but when I press E, nothing happens.
Wormling59mep:
My console says that it doesn’t understand what “E” means, it says ArgumentException: Input Key named: E is unknown…", although I’m not getting any compiler errors that aren’t allowing me to play the game.
I’m not sure if I should be inputting something, but when I press E, nothing happens.
My mistake, It should be KeyCode.E - I updated the example, hopefully that fixed it!
https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html
Thank you! It now works perfectly.
1 Like
If I wanted to change the value from infinity to, say, 0.5 meters, how would I do that?
duisti
April 7, 2018, 7:39pm
9
Then you type 0.5f instead of Mathf.Infinity