- Home /
Drag item on screen once spawned.
This is a 2d game.
I am having one hell of a time trying to figure out how to move the GameObject around. Right now, when I touch the screen, the gameobject will spawn. When I release the screen, the game object drops. HOWEVER, I CAN NOT get the gameobject to follow my finger around on the screen. See the case TouchPhase.Moved:. The way the code is set up, right now when I tap the screen, the game object will load but go away very quickly. If I comment out everything inside of the case TouchPhase.Moved: it works as stated above.
ANY help would be great appreciated!!!
using UnityEngine; using System.Collections;
 public class SpawnPuck : MonoBehaviour {
 
     public GameObject spawnPuck;
     public bool didPuckSpawn;
 
     public Vector2 startPos;
     public Vector2 direction;
     public bool directionChosen;
 
 
 
     // Use this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
 
 
         // Track a single touch as a direction control.
         if (Input.touchCount > 0)
         {
             var touch = Input.GetTouch(0);
 
             // Handle finger movements based on touch phase.
             switch (touch.phase)
             {
                 // Record initial touch position.
                 case TouchPhase.Began:
                     startPos = touch.position;
                     directionChosen = false;
                     Vector2 objPos = Camera.main.ScreenToWorldPoint(Input.touches[0].position);
                     spawnPuck = Instantiate(spawnPuck, objPos, Quaternion.identity) as GameObject;
                     Physics2D.gravity = Vector2.zero;
                     break;
 
                 // Determine direction by comparing the current touch position with the initial one.
                 case TouchPhase.Moved:
                     direction = touch.position - startPos;
                     Vector3 curPos = new Vector3(Input.touches[0].position.x, Input.touches[0].position.y, transform.position.z);
                     Vector3 worldPos = Camera.main.ScreenToWorldPoint(curPos);
                     spawnPuck.transform.position = worldPos;
                     break;
 
                 // Report that a direction has been chosen when the finger is lifted.
                 case TouchPhase.Ended:
                     directionChosen = true;
                     Physics2D.gravity = new Vector2(0.0f, -25.81f);
                     //didPuckSpawn = true; //drop then move to destory
                     break;
             }
         }
         if (directionChosen)
         {
             // Something that uses the chosen direction...
         }
     }
 }
Your answer
 
 
             Follow this Question
Related Questions
Inverting Position on One Axis 0 Answers
How to drag and drop in 2D game 0 Answers
Drag n Drop in a 3D orthographic environment 1 Answer
problem with rotation 2D 1 Answer
Random spawn outside viewport - Wrap around screen 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                