• 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
3
Question by Josh 14 · Dec 26, 2011 at 11:22 PM · c#instantiatevector3touchscreentoworldpoint

Problem with ScreenToWorldPoint and touches (C#)

I am trying to instantiate an object at the point where the the player touches the screen. Here is my code so far:

 public class CreateObject : MonoBehaviour
 {
     public GameObject createdObject;
     
     void Update ()
     {
         foreach (Touch touch in Input.touches)
         {
             if (touch.phase == TouchPhase.Began)
             {
                 Vector3 vec = Camera.main.ScreenToWorldPoint(touch.position);
                 vec.z = 0;
                 Instantiate(createdObject, vec, Quaternion.identity);
             }
         }
     }
 }

The problem is that vec always returns as 0,0,0 (or rather 0, 0, -10, but I changed the z value to 0). What am I doing wrong? Just FYI, I have my camera located at 0, 0, -10 and facing 0, 0, 0 in world space. Not sure if that matters or not. Thanks!

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

Answer by aldonaletto · Dec 27, 2011 at 12:42 AM

ScreenToWorldPoint must receive a Vector3 where x and y are the screen coordinates, and z is the distance from the camera. Since you're passing a Vector2, the compiler promotes it to Vector3 setting the z coordinate to 0, what will always return the camera position, no matter what x and y are.

If must copy touch.position to a Vector3 variable and set its z component to the distance you want:

    Vector3 tch = touch.position;
    tch.z = 15; // define distance from the camera
    Vector3 vec = Camera.main.ScreenToWorldPoint(tch);
    Instantiate(....);
Unfortunately, this will create all objects at a fixed distance from the camera - or at the surface of a sphere with radius 15, in this case.
If you want to create the objects in a plane, you should use ScreenPointToRay instead: create the base plane, create the ray, use plane.Raycast to find the distance, and ray.GetPoint(distance) to find the point... a piece of cake, for sure! But don't panic, the whole thing is easier than it may look:

public class CreateObject : MonoBehaviour {

 public GameObject createdObject;
 // create a plane in xy, at z = 5
 Plane basePlane = new Plane(-Vector3.forward, new Vector3(0, 0, 5));

 void Update (){
    foreach (Touch touch in Input.touches){
      if (touch.phase == TouchPhase.Began){ 
        // no need to set z, because ScreenPointToRay ignores it
        Ray ray = Camera.main.ScreenPointToRay(touch.position);
        float distance;
        if (basePlane.Raycast(ray, distance)){
          Vector3 vec = ray.GetPoint(distance); // get the plane point hit
          Instantiate(createdObject, vec, Quaternion.identity);
        }
      }
    }
 }

}

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
avatar image
3

Answer by Rod-Green · Dec 27, 2011 at 01:03 AM

It's an issue with vec2 to vec3 conversion:

 public class CreateObject : MonoBehaviour
 {
     public GameObject createdObject;
 
     void Update ()
     {
         foreach (Touch touch in Input.touches)
         {
             
             if (touch.phase == TouchPhase.Began)
             {
                 Vector3 vec = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, Camera.main.nearClipPlane + 5.0f));
                 Vector3 oldVec = Camera.main.ScreenToWorldPoint(touch.position);
                 Debug.Log (oldVec + " vs " + vec);
                 //vec.z = 0;
                 Instantiate (createdObject, vec, Quaternion.identity);
             }
         }
     }
 }
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

Trying To Find Mouse Position On Tap 1 Answer

Rotating a Vector3 in Instantiation 1 Answer

Moving a 3D object with 2D swipes 2 Answers

Instantiating Weirdly 1 Answer

Instantiate Prefab 1 Answer

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