• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Mouse Click and Spawn object 4 Answers

Instantiating Object On Map 1 Answer

Shoot A Raycast At A Specific Location? 1 Answer

Firing projectile towards mouse position from player... 0 Answers

Fire weapon from turret to mouse position (3D Isometric shooter) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges