Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
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
7
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.

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

Unity 2D Rouge-Like game tutorial help 0 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Objects seemingly not being instantiated after touch. 1 Answer

Trying To Find Mouse Position On Tap 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