[Fully Solved] How do I make a gun shoot a newer sandbox piece to the floor?

How do I make a C# script of a character holding a cube the shoots any sandbox piece that appears in the ground? Then shoot again in a different spot of the ground? When you shoot the second time the first ghost shape is gone then the second one appears to let players know you switch spots before instantiating.

Then move the sandbox piece with the Numpad. I already know how to make sandbox pieces move with the Numpad then instantiate. But I have no idea how to switch to a newer ghost shape when shooting in the ground before instantiating. And the old ghost shape is deleted when switching spots by shooting the ground many times.

Also make a Undo and hammer script to delete the shape that was mistakenly instantiated?

I figured it out I don’t destroy game object when shoot the ground to respawn it.

I use:

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

Here’s the code that I used:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class ShootGhostShapeV03 : MonoBehaviour

{

public GameObject bullet;

public GameObject ghostShapeSpawnLocation;

Vector3 targetPosition;

public AudioSource gameShapeBulletSound;

private float counter = 0;

public float delayTime = 8;

// Start is called before the first frame update

void Start()

{

targetPosition = transform.position;

gameShapeBulletSound = GetComponent();

}

void Update()

{

if (Input.GetMouseButtonDown(0))

{

Debug.Log(name);

counter = 0;

gameShapeBulletSound.Play();

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

RaycastHit hit;

Instantiate(bullet, transform.position, transform.rotation);

if (Physics.Raycast(ray, out hit))

{

targetPosition = hit.point;

ghostShapeSpawnLocation.transform.position = targetPosition;

}

}

}

}

But I still want the ghost shape selector to shoot and position and perfect number value? How do I do that?

Instead of positions: x: 1.223 y: 3.58 z: 2.34?
It should be: x:1 y:3 z:2

[SOLVED COMPELETLY!] I full got it solved by myself. When you shoot the ground the selector GameObject needs a script. Call it PositioningV01 and code:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class PositioningV01 : MonoBehaviour

{

void Update()

{

Vector3 snapPositions = transform.position;

snapPositions.x = Mathf.Round(snapPositions.x);

snapPositions.y = Mathf.Round(snapPositions.y);

snapPositions.z = Mathf.Round(snapPositions.z);

transform.position = snapPositions;

}

}