Hotkey works, but it always go to the same spot and does not attach to mouse

I made a new thread because there were other issues I had to get addressed first, once those were addressed, I saw this problem.

im using Unity 2d.

After creating it with “z”, it does not attach to the mouse cursor like what the guy has in the video, instead, it always goes to the same spot on the map, and hitting left click with the mouse places it no matter where the cursor is. its being created where I last had it on the map. I had to put the image on the map first so I can adjust the size.

Aside from not adding rotating and changing the hotkey, I did exactly what the guy did in the video verbatim, yet his works perfectly. it goes to the mouse when he presses his hotkey and it gets placed only where the cursor is.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HotKeys : MonoBehaviour

{
    [SerializeField]
    private GameObject placeableObjectPrefab;
 

    [SerializeField]
    private KeyCode newObjectHotkey = KeyCode.Z;

    private GameObject currentPlaceableObject;




    private void Update()
    {
        //check to see if the player presses the hot key...
        //...if they do, we will create a new object that we can place in the world

        HandleNewObjectHotKey();

        if (currentPlaceableObject != null)
        //if this is not null
        {
            MoveCurrentPlaceableObjectToMouse();
            ReleaseifClicked();
        }
    }

    private void ReleaseifClicked()
    {
      
            if (Input.GetMouseButtonDown(0)) //left click
        {
                currentPlaceableObject = null;
        }
    }




    private void MoveCurrentPlaceableObjectToMouse()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo; //this will store our info if we hit with our raycast

        if (Physics.Raycast(ray, out hitInfo)) //if it hits something, a collider, we will get info about what it hit

        {
            currentPlaceableObject.transform.position = hitInfo.point; //move object along where ever we have the mouse
                                                                       //currentPlaceableObject.transform.position = Quaternion.FromToRotation(Vector3.up, Vector3,hitInfo.normal);  //position object cortrecrly if placed on a side of a mountain.  not sure if i will need this line for this game
        }
    }

    private void HandleNewObjectHotKey()
    {
        if (Input.GetKeyDown(newObjectHotkey))  //if they pressed the hot key

        {
            if (currentPlaceableObject == null) //(if we dont have one yet)  //check to see if we are currently placing it, if we have one already
            {                           //if not, send to new one

                currentPlaceableObject = Instantiate(placeableObjectPrefab);
                //this will create a new instance of whatever this prefab is
            }

            else
            {

                Destroy(currentPlaceableObject);
            }
        }
    }
}

Assuming your “MoveCurrentPlaceableObjectToMouse” works as intended, you just need to call that function right after your hotkey created object is instantiated

Just tried it, it didnt work

I didnt kow I needed to use a legit mesh collider. For some reason, the cube one didnt work.

There must be someway you can still get it to work using 2d though

By 2d, are you using sprites and 2D physics, or canvas UI?

I have to once again make a new thread, only because I found a different tutorial which worked perfectly, except for one problem, but I dont want to confuse anyone here.