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 Benjamin6817 · Jul 16, 2012 at 01:16 PM · instantiateprefabpositionmousecursor

Instantiate a prefab at the Mouse's position?

I have been trying to instantiate a prefab at the position of the cursor, like this:

alt text

However the when I click the prefab isn't instantiating at the exact position of the cursor, It is off most always and sometimes it won't even show up in front of the camera.

my code is:

 function Update () {
 var MousePosition = Input.mousePosition;
     if (Input.GetMouseButtonDown(0)) 
     {
         var ObjectPosition = Camera.main.ScreenToWorldPoint(MousePosition);
     
         ObjectPosition.y = 0;
     
         Instantiate(Object, ObjectPosition, Quaternion.identity);
     }
 }

This script is attached to the Main Camera if that helps any. I am trying to instantiate the prefab on a plane, also.

unityanswersphoto.png (21.0 kB)
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
0
Best Answer

Answer by Piflik · Jul 16, 2012 at 01:26 PM

ScreenToWorldPoint(MousePosition) doesn't necessarily give you the coordinate you want (but since your question is a bit vague, I don't exactly know what you want...the mouse position is generally in 2D, while the world is 3D...do you want to spawn the object on a terrain?)

I'd use a raycast and instantiate the object on the intersection of the ray and whatever plane the object should sit on.

Comment
Add comment · Show 3 · 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 Benjamin6817 · Jul 17, 2012 at 02:39 AM 0
Share

Well, I'm no expert with Unity and I have no experience with Rays, What would you suggest I do? If you can't point me to a specific API reference, could you give me an overview of what I should do?

avatar image Piflik · Jul 17, 2012 at 03:01 AM 0
Share

This raycast would hit only objects on the 8th layer (that's what the layer$$anonymous$$ask is doing) and instantiate the object at the intersection. You need a $$anonymous$$ain Camera, but there is one by default, so if you didn't delete or retag it, it should be there. If it isn't there, you'd need to find the camera you are using a different way, probably by assigning it via the Inspector.

 private var layer$$anonymous$$ask  = 1 << 8;
 
 if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), mousPos, $$anonymous$$athf.Infinity, layer$$anonymous$$ask)){
     Instantiate(Object, mousPos.point, Quaternion.identity);
 }

Further information can be found in the script reference.

avatar image Benjamin6817 · Jul 17, 2012 at 11:37 PM 0
Share

Well I have this now and it isn't even instantiating the transform at all.

This is my code

function Update () {

 var hit : RaycastHit;
 var layer$$anonymous$$ask = 1 &lt;&lt; 8;
 
 if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit, $$anonymous$$athf.Infinity, layer$$anonymous$$ask)){
     if (Input.Get$$anonymous$$ouseButtonDown(0)) {
         Instantiate(Object, hit.point, Quaternion.identity);
     }
 }

}

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 to follow multiple clones positions of an instantiate prefab having a velocity ? 1 Answer

Prefab instantiating problem 1 Answer

Explosion at mouse 1 Answer

Wrong particle and prefab spawn position 1 Answer

Instantiating an Object doesn't use the given position. 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