1
\$\begingroup\$

I'm a complete noob with Unity, and I want to make myself a small Tower Of Hanoi type game.

I could use some advice on how to do this, but for now, I've settled for a drag and drop system - the player can just drag the discs (or in this case, a panda) sideways from one tower (bamboo) to another.

I looked up some resources online, and also came across the video from qull18creates, but I wasn't sure if using a canvas was the way to go, so I eventually settled on this video by K2niceStudio

Since there will be multiple pandas, I thought it best to put the script in the camera. Here's what I have so far:

using UnityEngine;
using System.Collections;

 public class DraggingScript : MonoBehaviour
 {
     public GameObject objectToDrag;
     public Vector3 objectCenterOriginal, objectCenterNew, objectMouseOffset, mousePosition;

     RaycastHit hit;

     public bool isDragging = false;

     // Use this for initialization
     void Start()
     {
     }

     // Update is called once per frame
     void Update()
     {
         // Click to Drag
         if (Input.GetMouseButtonDown(0))
         {
             Debug.Log("Button Down");
             Debug.Log(Input.mousePosition);
             // If left mouse button is clicked

             Ray lookForObject = Camera.main.ScreenPointToRay(Input.mousePosition);

             if (Physics.Raycast(lookForObject, out hit))
             {
                 Debug.Log("hit!");   // <------ I've never seen this printed out
                 objectToDrag = hit.collider.gameObject;
                 objectCenterOriginal = objectToDrag.transform.position;
                 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                 objectMouseOffset = mousePosition - objectCenterOriginal;
                 isDragging = true;
             }

             if (isDragging)
             {
                 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                 objectCenterNew = mousePosition;
                 objectToDrag.transform.position = new Vector3(objectCenterNew.x, objectCenterNew.y, objectCenterOriginal.z);
             }
         }

         if (Input.GetMouseButtonUp(0))
         {
             Debug.Log("Button Up");
             isDragging = false;
         }
     }
 }

Unless I made a mistake somewhere, I think it's mostly identical to the video's, with the only exception (AFAIK) being there's already three images in my scene (background, foreground, panda), whereas the video only has one.

However, the video's code works. Mine doesn't. Putting debug logs in the code makes me believe the panda is not being fetched, even if I click on it directly.

enter image description here

Where did I go wrong? I'm also open to advice, if there's a better way to do this overall. Thanks.

Here's a shot of my sprites in the Scene:

enter image description here

EDIT:

Apparently, having the camera set back to -10 Z position messed with the selection. I can now select the panda, though it doesn't move at all.

\$\endgroup\$
  • \$\begingroup\$ Do your "Pandas" have collider components attached? That's required for raycasting to work. The collider defines the shape which is sensitive to raycast hits (and other collision events). \$\endgroup\$ – Philipp Aug 12 '16 at 13:08
  • \$\begingroup\$ Unity has built in drag and drop I think \$\endgroup\$ – Evorlor Aug 12 '16 at 13:12
  • \$\begingroup\$ @Philipp well there's only one Panda now, I'll figure out how to multiply them later, but yes, the panda does have a Box Collider 2D. I've also tried setting the public variable ObjectToDrag to the panda sprite through the inspector, but no dice. \$\endgroup\$ – zack_falcon Aug 12 '16 at 13:17
  • 1
    \$\begingroup\$ So, if you followed the video, you should actually be using Physics.Raycast not Physics2D.Raycast (contrary to @Jon) as they used a BoxCollider and not a BoxCollider2D. You should think about using the 2D ones though if this is going to be a 2D game (as the engine will be doing slightly fewer calculations which might not even matter for you tbh). Also, it seems in both cases as though you never actually hit an object. Make sure that your layermask is setup properly, the your object isn't on some weird layer, and the Camera.main is actually the camera you're controlling. \$\endgroup\$ – Coburn Aug 12 '16 at 16:52
  • 1
    \$\begingroup\$ @Coburn, my Layers are set to default, though I set the Sky to Ignore Raycast, didn't do much. I updated the question with a picture of the sprites, maybe that will help. \$\endgroup\$ – zack_falcon Aug 12 '16 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.