Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by defcon2006 · Aug 24, 2011 at 10:51 PM · raycastonmousedraggetmousebuttondown

OnMouseDown and OnMouseDrag with RayCast

I've been working on a clone similar to fruit slice and I've been having a problem with onMouseDown and onMouseDrag working 100% correctly. Right now if I click and drag on the screen before it touches an object (e.g. the fruit) it will correctly slice as many objects as I want. It's using a RayCast to figure out whether it has touched the object and then it will change the graphic on the object after it has touched. The problem comes in when I begin my slice on the actual object. It'll change the animation of the object that I initallity touched; however, it won't let me continue to 'slice' any of the other objects. The RayCast seems to stop working after the first object is touched directly until I release the mouse and then re-click another object. Then it goes back into the same pattern, if I don't click the object directly first it'll continue to slice, or if I do touch the object directly it'll only allow the first object to be detected.

Here is the onMouseDrag code that I've been using:

    RaycastHit hit;
    if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
    {
        Transform objecthit = hit.transform;
        if (hit.transform.gameObject.tag == "testObject")
        {
            //Change material
        }
    }

I've also included a getMouseButtonDown statement in my Update(), since that was the only way I could get it to at least change the material on the initial click of the object. If I didn't add this in nothing would happen if I clicked the object directly:

  void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             RaycastHit hit;
             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
             {
                 Transform objecthit = hit.transform;
                 if (hit.transform.gameObject.tag == "testObject")
                 {
                 //Change Material
                 }
             }
         }
     }

Is there a way to get the RayCast to continue to detect even if the object is touched directly?

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
2
Best Answer

Answer by BinaryCaveman · Aug 25, 2011 at 02:07 AM

Try replacing Input.GetMouseButtonDown(0) with Input.GetMouseButton(0).

Input.GetMouseButtonDown(0) returns true only during the first frame when the user *presse*s the left mouse button. However, Input.GetMouseButton(0) returns true while the left mouse button is being held down. I hope that makes sense.
Good luck with your game! I love Fruit Slice/Fruit Ninja (iOS) too. :)

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 defcon2006 · Aug 25, 2011 at 02:50 AM 0
Share

Thank you so much! It works perfectly now. ^_^

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

UI element blocking OnMouseDrag()? 1 Answer

Raycast repeats multiple times 2 Answers

Raycasting vs. Input.GetMouseButtonDown 2 Answers

Dragging object in constrained area 1 Answer

Raycast and OnMouseButtonDown combination problem 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