- Home /
making objects clickable when 2 meters away
im making an RPG, and all is going quite ok, made a level system, gold etc, my problem now is i want to make quests, and the NPC's must only be 2-3 meters away to be clicked at, i know i shall use Vector3, and on mouse click, but i dont know how to do it, what comamnds to use, i checked out various sites, but this whole scripting thing is still very new to me, so if some kind sould could guide me though it, i would be really glad! make an example or something and what to put on where :) and i should be able to manage the rest ^^ thanks alot in advance!
-Martin
Answer by equalsequals · May 24, 2010 at 12:47 AM
Any object with a Collider component will fire events like OnMouseOver() which can be found in the MonoBehaviour class reference.
As for the distance checking, there are a few ways to do this. The less expensive way is to have a spherical collider that is set to a trigger and a radius of 1-1.5 (2-3 meters in diameter in Unity measurements). From there you can attach a behaviour script that looks sort of like this:
private bool _clickable = false;
void OnTriggerEnter(Collider other) { if(other.gameObject.name == "Player") { _clickable = true; } }
void OnMouseDown() { if(!_clickable) return; //do your interaction stuff below this line }
void OnTriggerExit(Collider other) { if(other.gameObject.name == "Player") { _clickable = false; } }
Another way to do it using the vector3 method would be to use Vector3.Distance()
[Edit] here is the Vector3 method that Mike suggested below, in greater detail:
// this script should be attached to whatever the player will be interacting with. please insure that the game object this script is a component of also has a collider otherwise OnMouseDown won't fire.
//called when the user presses down on the left mouse button over the attached game object's collider.
public var dist:int = 2;
function OnMouseDown() {
/ in order for this to just work, please chance "Player" below to whatever the root GameObject which your Player is inside of is named. /
var _playerPosition : Vector3 = GameObject.Find("Player").transform.position;
/ here we check if the distance between our "Player" and the game object this script is attached to is less then or equal to our defined distance variable <dist> /
if(Vector3.Distance(_playerPosition,transform.position) <= dist) { print("I should print something if you are in range and click on me."); //insert all of your code to reveal interaction GUI here. }}
Hope that helps.
==
hmmm is this even Java? xD i only do java, i get a bunch of errors on all "void" and stuff like that, also how do i add 2 colliders to the same npc? without overwriting the prefab?
sorry, no this is C# but it can easily be ported to Java. Ins$$anonymous$$d use the syntax-- function OnTriggerExit(other:Collider):void
var _clickable : boolean = false;
function OnTriggerExit(other:Collider):void { if(other.gameObject.name == "Player") { _clickable = true; } }
void On$$anonymous$$ouseDown() { if(!_clickable) return; //do your interaction stuff below this line }
void OnTriggerExit(Collider other) { if(other.gameObject.name == "Player") { _clickable = false; } }
still nothing..... apart from 100 errors, this is giving me grey hairs xD
15,1): expecting EOF, found '}'. (13,20): expected. Insert a semicolon at the end. BCE0043: Unexpected token: if.
and so forth
change the two void FunctionName things to function FunctionName
Answer by Mike 3 · May 24, 2010 at 12:56 AM
Expanding on equalsequals' suggestion about Vector3.Distance:
function OnMouseDown()
{
if(Vector3.Distance(yourPlayerTransform.position, transform.position) > 2) return;
//do your interaction stuff below this line
}
where yourPlayerTransform is the transform component on your player
The one problem I see with this method is how exactly the NPCs/Objects gets reference to the Player. The easiest workaround I can think of would have some sort of static reference to the player game object to avoid using GameObject.Find()
that does not work neither :P i got no real experience with scripts, what so ever i dont know what to start or fisnish with, im trying to learn, but non of this makes any sense, just alot of errors :(
not working :P can someone try and do a script in JAVA and post it :P should not be hard for some of you skilled folks
that is in javascript (now at least - edited it) - you'll need to get a reference to your player object somehow (e.g. GameObject.Find("Player") or a static reference in a player script)
Your answer
Follow this Question
Related Questions
2d character controller for platformer with touch or mouse input? 2 Answers
How to catch a Vector3-value by clicking at a world position in editor mode? 2 Answers
Advice on keeping distance using AddForce 2 Answers
Check if 2 objects positions are close enough 2 Answers
CLick on terrain and record vector3 location I clicked at? 3 Answers