I’m trying to create a drag and drop system for items. I want to use a stamp object and a ghost object. The ghost object shows placable space and the stamp object is moved with drag interactions.
Now I want to instantiate a new object at the ghost position and after placing I want the ghost and stamp object to return to their original place.
I have this custom script:
Hello there – we have an experimental AI bot that might be able to help you with your question. Would you like to try this out?
How it works:
Reply to the messages of the bot that end with a thread_id to interact with it and ask follow-up questions. You can also mention the bot directly in this channel to get help.
The ghost object is an empty gimmick with a transparent material. I want to add the option to turn red if it hits other alread placed objects in the future.
2.OnPointerUp should trigger the placement of the item if possible and return the stamp and ghost.
I want to include that the stamps are only draggable when the player has enough money for placing the stamp object.
It has a predefinec boundary, they have the same parent so it’s local.
I want to include that if the ghost hits another tower or path collider the tower should not be able to spawn. Also the money thing later.
Given these specifications, you can create a custom component in Needle Engine to implement the drag and drop system with the described behaviors. Here’s a pseudocode outline for the custom script to achieve the drag and drop functionality with the requirements specified:
typescript
import { Vector3, Raycaster, Color } from “three”;
import { Behaviour, GameObject, AssetReference, RaycastOptions } from “@needle-engine/engine”;
export class DragAndDropComponent extends Behaviour {
private initialStampPosition: Vector3; // Initial position of the stamp object
private ghostObject: GameObject; // Reference to the ghost object
private stampObject: GameObject; // Reference to the stamp object
awake() {
this.initialStampPosition = this.gameObject.worldPosition.clone(); // Store the initial stamp position
this.ghostObject = /* Reference to the ghost object /;
this.stampObject = / Reference to the stamp object */;
}
onPointerUp(event: PointerEvent) {
if (/* Check if the player has enough money /) {
const raycaster = new Raycaster();
// Define the ray direction based on the event input (e.g., mouse position)
raycaster.setFromCamera(/ Define ray direction based on event input /, this.context.mainCamera);
const intersections = this.context.physics.raycast(raycaster, new RaycastOptions(/ Define layers for filtering if necessary */));
if (intersections.length > 0) {
// Check if the intersected object is a valid placement surface
const isPlacementValid = /* Check if the placement is valid based on the intersection */;
if (isPlacementValid) {
// Instantiate a new object at the ghost position
const newObject: AssetReference = /* Instantiate a new object based on the ghost position */;
newObject.instantiate().then((instance) => {
// Handle the successful instantiation
});
// Return the stamp and ghost objects to their original positions
this.stampObject.worldPosition.copy(this.initialStampPosition);
this.ghostObject.worldPosition.copy(this.initialStampPosition);
} else {
// Handle invalid placement
}
}
}
}
}
You can adapt this pseudocode to your specific project structure, object references, and raycast logic to implement the drag and drop system with the described behaviors in Needle Engine.
0 messages leftGPT 3.5 thread_TtM1uMcKc3MKe5m4EMXqfMXX Thanks for your feedback!
If you parent the object with this code to anything that is not in the root of your scene the “worldPosition” will not be correct. What do you mean by it still didnt work? What exactly?
I’m sorry for the confusion. I can’t really explain it well. But it seems to work almost like I want now. I use the duplicate component and I made a custom component to spawn a ghost if in boundary of field.
Now when I release my duplicate copies the position of the ghost exactly like I want.