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
1
Question by Viktor.Eisenmann · Apr 14, 2012 at 04:53 PM · instantiatepositiontouchdragdelta position

How to instantiate object at touch position?

Hello, first, I wanna say I tried a lot before asking, I checked the scripting references, I checked another questions, and all that helped, but I can't figure out how to do it.

I'm trying to make a "Fruit Ninja" like effect. When the player drag the finger over the screen, a trail will appear and follow the movement.

My script is ALMOST working... Every time I touch the screen, my prefab empty gameObject called "Trail" instantiate, and I can move it around and leave a trail with a Trail Renderer. When the finger leaves, I destroy it from the scene.

The problem is. It's NOT instantiating correctly. First it would instantiate at the center of screen, even if I touched the corner and started moving my finger from there. I changed the script a little, and now is instantiating from the corner (0,0).

My situation is: Inside the prefab I have the movement script. In the scene I have a script calling the prefab when I touch the screen. A total of 2 scripts. (1 for moving around, which is inside the prefab, and 1 for instantiating to the scene, which is not working right).

What I want is to instantiate exactly where the finger touches the screen.

I tried a Raycast, tried a ScreenToWorldPoint, a WorldToScreenPoint... I don't know exactly what to do.

Here goes the Movement Script:

 using UnityEngine;
 using System.Collections;
 
 public class Finger_Move : MonoBehaviour
 {
 
     float swipeSpeed = 0.1F;
     public static float inputX;
     public static float inputY;
 
     void Update ()
     {
        if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Ended) {
          Destroy (gameObject);
        }
 
     }
 
     void FixedUpdate ()
     {
        if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) {
          Vector2 touchDeltaPosition = Input.GetTouch (0).deltaPosition;
          inputX = touchDeltaPosition.x * swipeSpeed;
          inputY = touchDeltaPosition.y * swipeSpeed;
          transform.Translate (inputX, inputY, 0);
 
 
          //Debug.Log ("X, Y: " + touchDeltaPosition.x + ", " + touchDeltaPosition.y);
 
 
        }
     }
 }

And now the Instantiate Script (which I think is wrong):

 using UnityEngine;
 using System.Collections;
 
 public class Create_Trail : MonoBehaviour {
 
     // Use this for initialization
     public Transform trail;
     public GameObject clone;
     void Start () {
 
 
     }
 
     // Update is called once per frame
     public void Update () {
        if(Input.touchCount > 0 && Input.GetTouch(0).phase==TouchPhase.Began){
        Vector3 v = new Vector3(Finger_Move.inputX,
                                 Finger_Move.inputY,
                                       5);
         Vector3 ray = Camera.main.ScreenToWorldPoint(v);
         clone = Instantiate(trail, ray, transform.rotation) as GameObject;  
 
 
 
        }
 
     }
 }

Can anyone help me? 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

3 Replies

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

Answer by yezzer · Apr 14, 2012 at 05:56 PM

Note: You should only really use FixedUpdate() for physics.

Firstly, I'd just experiment with a sphere or cube. Keep it simple, then when the basics are working, expand on it.

ScreenToWorldPoint sounds correct from what you describe.

Remember that the z returned from ScreenToWorldPoint is in units away from the camera. Depending on your camera, 5 might appear to be quite far away, and if you're not using an orthographic camera, it could appear to be quite far into the screen.

Comment
Add comment · Show 1 · 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 Nikhil11 · Feb 01, 2018 at 11:14 AM 0
Share

Thank you this works

avatar image
0

Answer by Viktor.Eisenmann · Apr 14, 2012 at 06:43 PM

Well, I fixed. I started over, with a sphere, and it worked. I changed the instantiate script to this:

 using UnityEngine;
 using System.Collections;
 
 public class Create_Trail : MonoBehaviour
 {
 
     // Use this for initialization
     public Transform trail;
     public GameObject clone;
     
     void Start ()
     {
         
     
     }
     
     // Update is called once per frame
     public void FixedUpdate ()
     {
         if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) {
             Vector3 fingerPos = Input.GetTouch (0).position;
             fingerPos.z = 5;
             Vector3 objPos = Camera.main.ScreenToWorldPoint (fingerPos);
             clone = Instantiate (trail, objPos, Quaternion.identity) as GameObject;    
             
         }
     }
 }

Thank you!

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 yezzer · Apr 14, 2012 at 06:47 PM 0
Share

Why use FixedUpdate() though? This should be in Update() :)

avatar image HunterKrech · Mar 20, 2013 at 06:18 AM 0
Share

yeah, it should be used for rigidbodys mostly.

avatar image Eco-Editor · Apr 30, 2018 at 10:54 PM 0
Share
 clone = Instantiate (trail, objPos, Quaternion.identity) as GameObject; 


This line is not correct...

avatar image
0

Answer by money4honey · Nov 04, 2016 at 11:52 AM

In last version of unity you can use Trail Renderer for this

Comment
Add comment · Show 1 · 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 DurgeshV · Mar 03, 2017 at 10:00 AM 0
Share

Can you plz expland the code using Trail Renderer in above code

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

9 People are following this question.

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

Related Questions

Instantiate object at touch position problem 1 Answer

Why does "draggable" MonoBehavior cause the object to be moved in the opposite direction? 1 Answer

Touch controlled ice puck 0 Answers

Touch Drag GameObject Found 1 Answer

Moving selected unit to location in space rts based on screen touch 3 Answers


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