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 Persona · Oct 23, 2011 at 06:07 PM · spawnclickscreentoworldpointmouse location

Spawn on Mouse Locatiion

I'm trying to make a script for spawning an item within a radius around where the mouse is positioned on screen, like in the example below. I've made a script that works for overhead viewpoint, but on third person view, behind the shoulders, it is off by a wide margin when it comes to depth perception. So can anyone:

  • Show me how to correct it

  • Draw the radius or point on screen so the player will know where it will be spawned

My Code so far: Code:

 if(Input.GetMouseButtonDown(0)){
  
  var playerPlane = new Plane(Vector3.up, transform.position);
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var hitdist = 0.0;
        
         if (playerPlane.Raycast (ray, hitdist)) {
             var targetPoint = ray.GetPoint(hitdist);
             }
 Spawn();
     }
 }

Example: http://youtu.be/M0EdKtq9fUo?t=1m12s

Thread Link: http://forum.unity3d.com/threads/109172-Spawn-on-mouse-location?p=723067#post723067

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 Persona · Oct 25, 2011 at 05:46 PM 0
Share

I think it has something to do with the Player Plane, but my efforts to alter it end badly.

2 Replies

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

Answer by Umbra · Oct 25, 2011 at 07:56 PM

Camera:ScreenToWorldPoint could be what you're looking for. Remember that the engine really has no way of telling the "depth" of the game, especially not in third person, other than distance from the camera.

I don't know exactly what it is you're trying to do, but maybe let the user scroll up and down to adjust the distance?

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 Persona · Oct 25, 2011 at 10:26 PM 0
Share

It requires the mouse input to deter$$anonymous$$e where to spawn it. If I could cast the ray through the screen and against the collider, then I could get somewhere.

avatar image Umbra · Oct 26, 2011 at 08:40 PM 1
Share

Isn't the mouse input (i.e. the mouse position) exactly what you're trying to do, though?

I use something similar in a script of $$anonymous$$e. It's for turning the main character, but I think you may find some use in it.

var ray = Camera.main.ScreenPointToRay(Input.mousePosition); var hit:RaycastHit; if (Physics.Raycast(ray, hit, 100)) { var tarPos:Vector3 = Vector3(hit.point.x, 0.5, hit.point.z); transform.LookAt(tarPos); }

You basically shoot a ray from the camera through the world point that the mouse is in, and co$$anonymous$$g up with a point in the world. That point could then be the center of the effect you want to spawn.

avatar image Persona · Oct 27, 2011 at 05:58 PM 0
Share

Thanks! That was driving me nuts.

avatar image
0

Answer by ptdnet · Oct 23, 2011 at 09:34 PM

Take a look at Random.onUnitSphere. It gives you a position on a sphere, which you could center on your mouse.

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 Persona · Oct 24, 2011 at 02:08 PM 0
Share

You going to have to be more specific that that, please.

avatar image ptdnet · Oct 25, 2011 at 06:03 PM 0
Share

It's a function that comes with Unity. Specifically: http://unity3d.com/support/documentation/ScriptReference/Random-onUnitSphere.html

avatar image Persona · Oct 25, 2011 at 06:30 PM 0
Share

No, what I mean is that it shows a random spot on a sphere but nothing that I'm doing uses random or a sphere in this particular script.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Spawn prefab where i click. 1 Answer

spawn prefab on mouse up 1 Answer

Multiplayer click and spawn 1 Answer

Spawn Objects Where i click 2 Answers

Make a object spawn 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