• 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 Beruseruku · Mar 03, 2014 at 09:11 PM · movementraycastraycastingmouseposition2dmovement

Can someone explain why my Raycasting doesn't work? please :)

I want to make an object (my player) move toward a mouse click. I managed to get the player to move to the target, but I can't set the position of the target to where I click with the mouse. I found out about Raycasting, but I haven't got any experience with programming/scripting so I'm struggling.

Here's what I have: An object (my player), a quad (the ground, tagged "Ground") and a target object (to walk towards). The game is setup in 2D mode (it's a mini 2D game).

Here's the code in the script I attached to the main camera:

 void Update ()
 {
     Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
     RaycastHit hit;
     
     if (Input.GetMouseButtonDown (0)) {
         if (Physics.Raycast(ray, out hit)) {
             if (hit.collider.tag == "Ground") {
                 Debug.Log ("Touch Detected...");
             }
         }
     }
 }

Any help/pointers appreciated! :)

Comment
Add comment · Show 2
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 robertbu · Mar 03, 2014 at 09:15 PM 0
Share

The code looks fine. Put this between line 8 and 9 to check if your Raycast() is succeeding, and if so, what it is hitting:

  Debug.Log(hit.collider.name+", "+hit.collider.tag);
avatar image Beruseruku · Mar 05, 2014 at 12:13 AM 0
Share

Thanks robertbu! It turns out I tagged it "ground" and not "Ground".

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mohsentta · Jul 15, 2015 at 09:53 PM

this code wont work if your main camera doesn't set in your scene. get your camera in a public variable and use ScreenPointToRay

 public GameObject cam; 
 void Update ()
  {
      Ray ray = cam.camera.ScreenPointToRay (Input.mousePosition);
      Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
      RaycastHit hit;
      
      if (Input.GetMouseButtonDown (0)) {
          if (Physics.Raycast(ray, out hit)) {
              if (hit.collider.tag == "Ground") {
                  Debug.Log ("Touch Detected...");
              }
          }
      }
  }



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

21 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 avatar image avatar image

Related Questions

Raycast2d not working C# :( 1 Answer

How to Limit Input.mousePosition or Raycast 1 Answer

Edge/Corner Stick — issue or logical error? 0 Answers

Push an object opposite direction of mouse position 0 Answers

Problem with raycast detection? 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