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);
}
}
}
}