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 MadToLove · Dec 30, 2012 at 02:13 AM · guitextureraycastimagedistance

How can I add a distance cap to this script?

I found this script on the Unitywiki, Whenever the mouse cursor is over an object in my scene that has this script attached to it it displays a image. The only thing left that I need to do to this script is make it so you have to be within a certain distance of this object. As of now I could be infinite away from it and if i hover over it it will still display the image. I'm looking at trying to restrict it from doing so unless within say 3.5 feet. Any help would be greatly appreciated.

using UnityEngine; using System.Collections;

 public class MouseManipulateIcon : MonoBehaviour 
 {
     public Texture2D cursorImage;
  
     private int cursorWidth = 48;
     private int cursorHeight = 48;
     private bool showCursor = false;
    private string defaultResource = "MousePointer";
  
     void Start()
     {
         if(!cursorImage) {
             cursorImage = (Texture2D) Resources.Load(defaultResource);
             Debug.Log(cursorImage);
         }
         //cursorImage = (Texture2D) Instantiate(cursorImage);
     }
  
  
     void OnMouseEnter()
     {
         Debug.Log("Entered");
         Screen.showCursor = false;
         showCursor = true;
     }
     void OnGUI() {
         if(showCursor) {
             GUI.DrawTexture(new Rect(Input.mousePosition.x, Screen.height - Input.mousePosition.y, cursorWidth, cursorHeight), cursorImage);
         }
     }
     void OnMouseExit()
     {
         Debug.Log("Left");
         showCursor = false;
         Screen.showCursor = true;
     }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by voodoo · Dec 30, 2012 at 04:30 AM

Check that the distance between the object's position and the camera's position is less than a specified float (in meters) before you do your logic.

Vector3.Distance

Camera.main

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 mel1992 · Dec 30, 2012 at 04:30 AM

Well you can do it different ways. One way is to check the distance to the MainCamera:

 void OnMouseEnter()
 {
     if (Vector3.Distance(Camera.main.transform.position,transform.position) > 5)
     {
        Debug.Log("Entered");
        Screen.showCursor = false;
        showCursor = true;
     }
     else 
     {
        Debug.Log("Left");
        showCursor = false;
        Screen.showCursor = true;
     }
 }

or you can send a raycast out from the Camera in an Update():

  void Update()
 {
     RaycastHit hitInfo;
     if (Physics.Raycast(Camera.main.transform.position, Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 3.5f)), out hitInfo) && hitInfo.collider.gameObject.name == "The name of Object")
     {
         Debug.Log("Entered");
         Screen.showCursor = false;
         showCursor = true;
     }
     else
     {
         Debug.Log("Left");
         showCursor = false;
         Screen.showCursor = true;
     }
 }
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 Straight Rainbow · Dec 30, 2012 at 03:55 AM

You could do a simple Vector3.Distance(transform.position,Camera.mainCamera.transform.position); or you can modify to to do a Ray collision test and make a distance limitation on that.

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

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

11 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

Related Questions

Get GUITexture to follow ScreenPointToRay ray cast 1 Answer

Change slection grid content texture size/scale 0 Answers

Dynamic Alpha Gradient Texture Collision Detection 0 Answers

What's the best way to incorporate dynamically loaded textures into GUI controls? 1 Answer

GUI.DrawTextureWithTexCoords question 1 Answer


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