Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rekaens · May 24, 2010 at 12:10 AM · vector3clickrangequest

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

==

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rekaens · May 24, 2010 at 12:58 AM 0
Share

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?

avatar image equalsequals · May 24, 2010 at 01:05 AM 0
Share

sorry, no this is C# but it can easily be ported to Java. Ins$$anonymous$$d use the syntax-- function OnTriggerExit(other:Collider):void

avatar image Rekaens · May 24, 2010 at 01:15 AM 0
Share

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

avatar image Mike 3 · May 24, 2010 at 01:44 AM 0
Share

change the two void FunctionName things to function FunctionName

avatar image Mike 3 · May 24, 2010 at 01:45 AM 0
Share

and Collider other to other : Collider

Show more comments
avatar image
0
Best Answer

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

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image equalsequals · May 24, 2010 at 01:02 AM 0
Share

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

avatar image Rekaens · May 24, 2010 at 01:05 AM 0
Share

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

avatar image Mike 3 · May 24, 2010 at 01:05 AM 0
Share

yup yup, that's how i'd do it for an rpg

avatar image Rekaens · May 24, 2010 at 01:18 AM 0
Share

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

avatar image Mike 3 · May 24, 2010 at 01:23 AM 0
Share

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)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

CLick on terrain and record vector3 location I clicked at? 3 Answers

How to catch a Vector3-value by clicking at a world position in editor mode? 2 Answers

2d character controller for platformer with touch or mouse input? 2 Answers

Advice on keeping distance using AddForce 2 Answers

Check if 2 objects positions are close enough 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges