Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 DimitriUK · Jan 11, 2014 at 03:50 PM · buttonmouserangedoors

MouseButtonDown on button (Far away) NEED: Range for button

So in my script, I now have working audio and such which was what I was aiming for. NOW I am trying to figure out that when I click the MouseButtonDown on the Door button, it works but can happen from any DISTANCE. I basically want a range of maybe half a meter to be able to open the door.

Opening the door with a button like that of course is really.. You know.. Unrealistic, haha.

If you can help me, it would be very generous of you.. If you can not, thank you for at least looking in to my problem.

My main goal is to further develop this script in to something more powerful with more functionality as well as GUI functions.


EXAMPLE IMAGE:


alt text

 #pragma strict
 
 var doorObj:GameObject;
 var isOpen = false;
 var delay : float = 0.3;
 var openSound : AudioClip;
 var closeSound : AudioClip;
 
 function OnMouseDown(){
    Debug.Log("Mouse is down");
       
    if(!this.isOpen){
        doorObj.animation.Play("NewOpen");
        this.isOpen = true;
        audio.PlayOneShot(openSound);
            
       }else{
          doorObj.animation.Play("NewClose");
          this.isOpen = false;
          if (closeSound)
         audio.PlayOneShot(closeSound);
       }
 }
       
   function Start() {
       }

   function Update() {
       }

clickedfaraway.jpg (165.8 kB)
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
3
Best Answer

Answer by robertbu · Jan 11, 2014 at 04:43 PM

A quick hack solution is to put a empty game object with a box collider in front of the button at the distance you want to establish as the maximum distance to hit the button. Any click beyond that distance will hit the box collider and not the button. A bit more elegant solution is to test the distance between the player and the button before allowing the button to be pressed. Say your your player is tagged 'Player', you could do this in the top of your OnMouseButtonDwon() callback:

 if (Vector3.Distance(GameObject.FindWithTag("Player").transform.position, transform.position) > someValue) {
     return;
 }
Comment
Add comment · 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
0

Answer by agies1 · Jan 11, 2014 at 05:07 PM

Basically, what you are looking for is a distance from your Avatar/Character's current position to the clicked Collider. Something similar to;

This is an all purpose version of the script, that you would drop on your avatar...

 var playerReach : float = .5;
 if ( Input.GetMouseButtonDown(0)){
     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     if (Physics.Raycast (ray, hit)){
         //Ok we hit something, is that thing in range?
         //transform should be the avatar's
         var dist = Vector3.Distance(hit.collider.transform, transform.position);
         //Are we within 1/2 a meter along the z?
         if (dist.z <= playerReach){
             //Strongly typed interaction
             var interactiveObject = hit.collider.gameObeject.GetComponent<InteractiveObject>();
             if (interactieObject) {
                 interactiveObject.Touch(gameObject);
             }
             //Loosely typed interaction
             SendMessage("Touch", gameObject, SendMessageOptions.DontRequireReceiver);
         }
     }
 }

 //Script to inherit from and add to any game object 
 public class InteractiveObject : MonoBehaviour {
     //Method to implement the touch behavior
     public abstract void Touch(GameObject gameObject);
 }

Put this on your door button, this works for a nice one off feature, or if refactored as a Trigger based script.

 function OnMouseDown(){
    Debug.Log("Mouse is down");
    //as @Robertbu pointed out
    if (Vector3.Distance(GameObject.FindWithTag("Player").transform.position, transform.position).z > .5 /*worth making a variable*/) {
         return;
    }
    if(!this.isOpen){
        doorObj.animation.Play("NewOpen");
        this.isOpen = true;
        audio.PlayOneShot(openSound);
  
       }else{
          doorObj.animation.Play("NewClose");
          this.isOpen = false;
          if (closeSound)
         audio.PlayOneShot(closeSound);
       }
 }
Comment
Add comment · Show 2 · 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 DimitriUK · Jan 12, 2014 at 02:49 PM 0
Share

Hey buddy. The code definitely looks good, however do I attach it to the GameObject that has the box collider in it or?

avatar image agies1 · Jan 12, 2014 at 04:06 PM 0
Share

This code would go on the Player.

You could switch the code if you want it on the door, see above edit.

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

GetMouseButtonDown From a close distant 1 Answer

Screen.lockCursor Acting Irregular 2 Answers

Why Is OnMouse Not Working Correctly? 1 Answer

Audio Clip trouble 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