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 BlueSin · Mar 06, 2014 at 08:11 PM · camera2draycastcharactermouse

Raycast from Character to Mouse Position?

I am making a 2D procedurally generated game similar to Terraria or Starbound. However, I use 3D colliders since I am using the Character Controller component on my player. So when my character clicks, I want to break a block and attempt to raycast from the character to wherever the mouse was clicked. This isn't working for some reason so I switched to Debug.DrawRay and I see now the ray is being draw upwards, even though I might have clicked to the left, right, or below the player. I cannot seem to get the line to aim down. What am I doing wrong here?

 Vector3 mousePosition = playerCamera.ScreenToWorldPoint(Input.mousePosition);
 mousePosition.z = 0;
 
 // Draw a debug line (transform.position = character position)
 Debug.DrawRay(transform.position, mousePosition, Color.red, 0f);

Also here is a picture of the issue:

http://imgur.com/NqZvDlR

Comment
Add comment · Show 1
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 jakovd · Mar 06, 2014 at 08:57 PM 0
Share

You are using ScreenToWorld with just x,y coordinates from mouse screen position so that assumes that you want the Z to be 0. Which is not exactly the position to which you are trying to draw your ray to. There are infinitely many positions that resemble your mouse click position. It might be the position right behind your camera, or the position far far away from it. All those points are a part of one Ray. You can get that ray by using

 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

and then pass that to Physics.Raycast to see where that ray hits some object:

 RaycastHit hit;
 if (Physics.Raycast (ray, hit, 100.0)) {}

Then you can use that 'hit' variable to get the reference to that object that was hit by ray and Destroy it

2 Replies

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

Answer by robertbu · Mar 06, 2014 at 09:17 PM

You are using DrawRay() with two positions. DrawRay() takes a position and a direction. You can either switch and Debug.DrawLine() (which takes two positions), or you can modify your DrawRay() as:

  Debug.DrawRay(transform.position, mousePosition - transform.position);

Also note that your use of ScreenToWorldPoint() used this way will only work if you have an Orthographic camera. It will not work for a Perspective camera.

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 jotapeh_ · Mar 07, 2014 at 12:07 AM

As has already been said, you need to create a ray from the Camera outwards where the mouse is clicked..

 Ray mseRay = Camera.main.ScreenPointToRay(Input.mousePosition);

However, your question asks for a Raycast from the player to the point where the mouse was clicked. Probably then you want the player's plane:

 Plane playerPlane = new Plane(Vector3.back, player.transform.position);
 float d;
     
  if (playerPlane.Raycast(mseRay, out d)) {
     Vector3 hitPt = mseRay.GetPoint(d);
 
    // click would hit the same vertical plane as the player at hitPt
  }

Then within that block you would create a Ray from the player's position to hitPt, like this:

 Ray playerToClick = new Ray(player.transform.position, hitPt - player.transform.position);

Raycast as normal from there.

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 robertbu · Mar 07, 2014 at 12:30 AM 0
Share

@jotapeh_ - your code works well in a 3D environment incliding a Perspective camera where the camera is at an angle relative to the axes. The OPs code will only work in the more restricted situation of a axes aligned, Orthographic camera. But it will work given the restrictions.

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

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

Related Questions

2D Character Flip 2 Answers

Camera.ScreenToWorldPoint with Perspective camera 1 Answer

How to create a multilayered 2D effect? 1 Answer

Unity Android Movement Problem 0 Answers

Ray camera to Terrain goes Wrong. 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