• 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. T$$anonymous$$s 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

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() (w$$anonymous$$ch 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 t$$anonymous$$s way will only work if you have an Orthograp$$anonymous$$c 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 $$anonymous$$tPt = mseRay.GetPoint(d);
 
    // click would $$anonymous$$t the same vertical plane as the player at $$anonymous$$tPt
  }

Then wit$$anonymous$$n that block you would create a Ray from the player's position to $$anonymous$$tPt, like t$$anonymous$$s:

 Ray playerToClick = new Ray(player.transform.position, $$anonymous$$tPt - 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Ray camera to Terrain goes Wrong. 1 Answer

Unity Android Movement Problem 0 Answers

2D Character Flip 2 Answers

How to create a multilayered 2D effect? 1 Answer

Top down movement and rotation 2 Answers


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