I have code where I can hit 1, 2, or 3 to instantiate different sized cubes where the mouse position is. Thee problem I’m having is that when they instantiate they like zoom from where they instantiate toward the screen. I’m guessing it has to do with version change since I’m following a tutorial from 2017([Tutorial][1]).
[SerializeField]
private GameObject[] placeableObjectPrefabs;
private GameObject currentPlaceableObject;
private float mouseWheelRotation;
private int currentPrefabIndex = -1;
private void Update()
{
HandleNewObjectHotkey();
if (currentPlaceableObject != null)
{
MoveCurrentObjectToMouse();
RotateFromMouseWheel();
ReleaseIfClicked();
}
}
private void HandleNewObjectHotkey()
{
for (int i = 0; i < placeableObjectPrefabs.Length; i++)
{
if (Input.GetKeyDown(KeyCode.Alpha0 + 1 + i))
{
if (PressedKeyOfCurrentPrefab(i))
{
Destroy(currentPlaceableObject);
currentPrefabIndex = -1;
}
else
{
if (currentPlaceableObject != null)
{
Destroy(currentPlaceableObject);
}
currentPlaceableObject = Instantiate(placeableObjectPrefabs*);*
currentPrefabIndex = i;
}
break;
}
}
}
private bool PressedKeyOfCurrentPrefab(int i)
{
return currentPlaceableObject != null && currentPrefabIndex == i;
}
private void MoveCurrentObjectToMouse()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, 1000))
{
currentPlaceableObject.transform.position = hitInfo.point;
currentPlaceableObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
}
}
private void RotateFromMouseWheel()
{
Debug.Log(Input.mouseScrollDelta);
mouseWheelRotation += Input.mouseScrollDelta.y;
currentPlaceableObject.transform.Rotate(Vector3.up, mouseWheelRotation * 10f);
}
private void ReleaseIfClicked()
{
if (Input.GetMouseButtonDown(0))
{
currentPlaceableObject = null;
}
}
[1]: Let your players place objects or turrets in Unity (RTS / Base Building games) - YouTube