Search Unity

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

Discussion in 'Scripting' started by SuperCrow2, Jan 27, 2020.

  1. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    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.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class HotKeys : MonoBehaviour
    7.  
    8. {
    9.     [SerializeField]
    10.     private GameObject placeableObjectPrefab;
    11.  
    12.  
    13.     [SerializeField]
    14.     private KeyCode newObjectHotkey = KeyCode.Z;
    15.  
    16.     private GameObject currentPlaceableObject;
    17.  
    18.  
    19.  
    20.  
    21.     private void Update()
    22.     {
    23.         //check to see if the player presses the hot key...
    24.         //...if they do, we will create a new object that we can place in the world
    25.  
    26.         HandleNewObjectHotKey();
    27.  
    28.         if (currentPlaceableObject != null)
    29.         //if this is not null
    30.         {
    31.             MoveCurrentPlaceableObjectToMouse();
    32.             ReleaseifClicked();
    33.         }
    34.     }
    35.  
    36.     private void ReleaseifClicked()
    37.     {
    38.      
    39.             if (Input.GetMouseButtonDown(0)) //left click
    40.         {
    41.                 currentPlaceableObject = null;
    42.         }
    43.     }
    44.  
    45.  
    46.  
    47.  
    48.     private void MoveCurrentPlaceableObjectToMouse()
    49.     {
    50.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    51.         RaycastHit hitInfo; //this will store our info if we hit with our raycast
    52.  
    53.         if (Physics.Raycast(ray, out hitInfo)) //if it hits something, a collider, we will get info about what it hit
    54.  
    55.         {
    56.             currentPlaceableObject.transform.position = hitInfo.point; //move object along where ever we have the mouse
    57.                                                                        //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
    58.         }
    59.     }
    60.  
    61.     private void HandleNewObjectHotKey()
    62.     {
    63.         if (Input.GetKeyDown(newObjectHotkey))  //if they pressed the hot key
    64.  
    65.         {
    66.             if (currentPlaceableObject == null) //(if we dont have one yet)  //check to see if we are currently placing it, if we have one already
    67.             {                           //if not, send to new one
    68.  
    69.                 currentPlaceableObject = Instantiate(placeableObjectPrefab);
    70.                 //this will create a new instance of whatever this prefab is
    71.             }
    72.  
    73.             else
    74.             {
    75.  
    76.                 Destroy(currentPlaceableObject);
    77.             }
    78.         }
    79.     }
    80. }

     
    Last edited: Jan 27, 2020
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Assuming your "MoveCurrentPlaceableObjectToMouse" works as intended, you just need to call that function right after your hotkey created object is instantiated
     
  3. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    Just tried it, it didnt work
     
  4. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    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
     
  5. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    By 2d, are you using sprites and 2D physics, or canvas UI?
     
  6. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    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.