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 CursedScripter · Jan 05, 2012 at 06:11 AM · raycastpositionmousespawnclick

Spawn Objects Where i click

Basically i have a script written to where i have a gameobject and i have a simple 2x4 wood cube/model attached to it. I dont understand why when i click it spawns that object where the camera is and not where im pointing... All help is appreciated thanks

Heres my code

 public GameObject Wood;
     public bool isWood = true;
     public GameObject Metal;
     public bool isMetal = false;
     
     public Transform spawn;
     
     
     RaycastHit hit;
     public GameObject target;
     
     
     // Use this for initialization
     void Start () {
         
         target = Wood;
     }
     
     // Update is called once per frame
     void Update () {
         
         if (Input.GetMouseButtonDown(0)){
             Ray ray = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
                 if (Physics.Raycast(ray)){
                 
                 }
                         Instantiate(target, ray.origin, Quaternion.identity);
             
           }
     }

Theres more code than that but all the other code is unnecessary to include. I dont get any errors this just doesnt work.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by fonql · Apr 20, 2013 at 03:46 PM

Actually, I got it working without having to cast a ray. The trick is to pass the INVERSE z position of your camera in the ScreenToWorldPoint last argument. So if your camera's z position is -10, pass 10. Here's my C# code:

 using UnityEngine;
 using System.Collections;
 
 public class MousePick : MonoBehaviour {
     
    public GameObject cuber;
     
     void Update() {
        
         if (Input.GetButtonDown("Fire1")) {
              print(Input.mousePosition);
              Vector3 p = Camera.main.**ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y,10.0f));
             print(p);
             print(p.x);
             print(p.y);
             
             Instantiate(cuber,new Vector3(p.x,p.y, 0.0f),Quaternion.identity);
             
         }
     }
 }
Comment
Add comment · Show 4 · 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 asafsitner · Apr 20, 2013 at 04:00 PM 0
Share

How do you calculate the camera's Z position in relation to the surface you click on?

avatar image fonql · Apr 20, 2013 at 04:11 PM 0
Share

Well spotted Sir! I derive the z position simply from camera.transform.position.z; HOWEVER this is not the distance between a camera and a ray hitpoint, for that I suggest your script:) Agreed, my script is only helpful for 2d games.

avatar image asafsitner · Apr 20, 2013 at 05:05 PM 0
Share

Wouldn't it make sense to simply use -camera.position.z as the Z parameter of the ScreenToWorldPoint method to make it more generic?

That way you can theoretically support camera zoom, and avoid magic numbers in the code. ^^

avatar image fonql · Apr 20, 2013 at 05:40 PM 1
Share

Absolutely Agreed.

avatar image
0

Answer by asafsitner · Jan 05, 2012 at 07:57 AM

That's because you Instantiate the objects at the ray's origin, i.e. the camera in your case as it's the object shooting the ray. You want to add `hitInfo` and spawn the object at `hitInfo.point`.

 if (Input.GetMouseButtonDown(0))
 {
     if (Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0), out rayInfo))
     {
         Instantiate(target, rayInfo.point, Quaternion.identity);
     }
 }

I also don't understand why you put the Instantiate call outside the if statement's block so I put it back in. If it's a mistake ignore it.

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

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mouse Click and Spawn object 4 Answers

Shoot A Raycast At A Specific Location? 1 Answer

Getting raycasting info from mouse event (eg through RaycastHit) 0 Answers

click on screen to get the coordinate on the ground(x,z plane) 1 Answer

How to prevent GameObject from spawning on top of each other? 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