Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 AjelG · Dec 08, 2016 at 06:45 AM · raycastcollidersraycastingraycasthitdrawer

Ray cast affects all colliders in scene if mouse button is not releasesd.

To be specific i have some drawers in my scene and i want to open-close them by click and drag them to open-close. Each drawer has a collider atached. When i hit the first drawer it opens-closes corectlly but before i release the mouse button and move the mouse pointer over another drawer or all drawers in scene, all of them starts to open at once.

What i want is to click a drawer and drag it to open-close it, the click another one and so on. I know that a way to do this is by using tags or layers but i am not very good at scripting yet.

Here is the code.

 public GameObject drawer;
 
     void Update () {
         if (Input.GetMouseButton(0)) {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); 
             if (Physics.Raycast (ray, out hit, 100.0f)) {
                 drawer = hit.collider.gameObject;
             }
 
             if (drawer != null ) {
                 translate_z += Input.GetAxis ("Mouse Y") * sensX * Time.deltaTime;
                 translate_z += Input.GetAxis ("Mouse X") * sensX * Time.deltaTime;
                 translate_z = Mathf.Clamp (translate_z, -1.25f, 0.0f);
                 drawer.transform.localPosition = new Vector3 (0.0f, 0.0f, translate_z);
             }
         } else {
         //drawer = null;
         }
     }
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

1 Reply

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

Answer by getyour411 · Dec 09, 2016 at 04:38 AM

I think you want something like

 4: if inputgetmouse && !isDragging (new bool)
 11.5/newline isDraggging = true;
 19.5/newline  if(input.getmouseup) isDragging = false

Something to capture the state of dragging and only do stuff when you aren't dragging.

Comment
Add comment · Show 1 · 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 AjelG · Dec 09, 2016 at 03:21 PM 0
Share

Thanks man for your answer. I adjusted my code as you mention and now it works great.

Here is the corrected code.

 using UnityEngine;
 using System.Collections;
 
 public class draw : $$anonymous$$onoBehaviour {
 
     float translate_z;
     float translate_new_z;
     float sensX = 2.0f;
     public GameObject drawer;
     public bool isDraging;
 
     void Update () {
         if (Input.Get$$anonymous$$ouseButton (0)) {
             if (!isDraging) {
                 RaycastHit hit;
                 Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); 
                 if (Physics.Raycast (ray, out hit, 100.0f)) {
                     drawer = hit.collider.gameObject;
                     isDraging = true;
                 }
             }
 
             if (isDraging) {
                 translate_z += Input.GetAxis ("$$anonymous$$ouse Y") * sensX * Time.deltaTime;
                 translate_z += Input.GetAxis ("$$anonymous$$ouse X") * sensX * Time.deltaTime;
                 translate_z = $$anonymous$$athf.Clamp (translate_z, -1.25f, 0.0f);
                 drawer.transform.localPosition = new Vector3 (0.0f, 0.0f, translate_z);
                 translate_new_z = drawer.transform.localPosition.z;
                 Debug.Log (translate_new_z);
             }
         } else if (Input.Get$$anonymous$$ouseButtonUp(0)) {
             isDraging = false;
         }
     }
 }
     
 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

RaycastHit returns object without collider, in wrong layer 1 Answer

Raycast script help? 1 Answer

Raycast hit in OnDrawGizmos but not in Update 1 Answer

Vehicle is vibrating on the edge of the ramp/surface when I get the normal of the surface 0 Answers

Help finding out which side of a cube a raycast hits 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