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
7
Question by e-bonneville · Mar 22, 2010 at 01:09 AM · raycastinputmouseclick

Using RayCast to Get Mouse Input

I am really frustrated, so much so that I am going to ask this last question and walk away from the computer and wait. I'm trying to use raycasts to get the current mouse position at a click. For now, I'm instantiating an object as a debugger. Here's my code:

var particle : GameObject;
function Update () {
if (Input.GetButtonDown ("Fire1")) {
// Construct a ray from the current mouse coordinates
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray)) {
// Create a particle if hit
Instantiate (particle, transform.position, transform.rotation);
}
}
}

Now, when I play the game, when I click, it instantiates the object right at the camera's position. What am I doing wrong? Any and all help appreciated.

Elliot Bonneville

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

Answer by Eric5h5 · Mar 22, 2010 at 01:45 AM

You say to instantiate at transform.position, which is the camera's position (I'm assuming your script is attached to the camera). You need to create a RaycastHit variable and use the position where the raycast actually hits.

var hit : RaycastHit;
if (Physics.Raycast (ray, hit)) {
// Create a particle if hit
Instantiate (particle, hit.point, transform.rotation);

That will still use the camera's rotation; probably you want to use Quaternion.identity instead.

Comment
Add comment · Show 7 · 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 e-bonneville · Mar 22, 2010 at 06:52 PM 2
Share

Thanks. I spent all night on that!

avatar image e-bonneville · Mar 22, 2010 at 06:52 PM 2
Share

Would give 20 votes... If I could!

avatar image Jibidiah · Oct 24, 2011 at 11:49 PM 2
Share

come on people! upvote it so it gets to 20!

avatar image syclamoth · Oct 24, 2011 at 11:52 PM 4
Share

Wouldn't you say Eric5h5 has enough karma as it is? This is a pretty simple answer! I never get 20 upvotes for basic tutorials... T.T

avatar image Eric5h5 · Oct 26, 2011 at 02:42 AM 4
Share

I don't think that "having enough karma" is a good reason for not upvoting an answer if it deserves it. Not that this one particularly deserves it; it already has more than enough votes for what it is. Also, I'd discourage upvoting an answer as some sort of popularity contest. Just stick to upvoting answers if you find them helpful and don't take any other factors into consideration.

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do you use a queuing mechanism in Unity? 1 Answer

GetMouseButtonDown(1) true in multiple frames 1 Answer

Detect if object clicked on 2 Answers

Simulate a mouse Click? 2 Answers

click on a game object that script isn't attached to 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