Iām trying to get the Agents to collide with an object, or detect overlap, so I can eventually shoot projectiles at them to destroy/damage them.
The agent starts spawning and jumping from the movingObject when I attach a rigidbody to it so I think I need to accomplish this with only a collider on each object. Hereās my typescript that Iām attaching to the movingObject:
import { Behaviour, serializable, Collider } from ā@needle-tools/engineā;
import { Vector3, Clock } from āthreeā;
export class MovingObject extends Behaviour {
@serializable(Vector3)
direction: Vector3 = new Vector3(1, 0, 0); // Move along the X-axis
@serializable()
speed: number = 2; // Movement speed
@serializable()
distance: number = 5; // Distance to move before changing direction
private startPosition: Vector3 = new Vector3();
private movingForward: boolean = true;
private clock: Clock = new Clock();
start() {
this.startPosition.copy(this.gameObject.position);
}
update() {
const deltaTime = this.clock.getDelta();
const delta = this.direction.clone().multiplyScalar(this.speed * (this.movingForward ? 1 : -1) * deltaTime);
this.gameObject.position.add(delta);
if (this.gameObject.position.distanceTo(this.startPosition) >= this.distance) {
this.movingForward = !this.movingForward;
}
}
onTriggerEnter(other: Collider) {
const collidedObject = other.gameObject;
// Debugging to see if the trigger is detected
console.log("Trigger detected with:", collidedObject.name);
// Check the object's name
if (collidedObject.name === "Agent") {
console.log("Trigger with Agent detected.");
collidedObject.destroy();
console.log("Agent killed by moving object.");
}
}
}
Hi 0nederful! Iām an AI-powered assistant here to help with your questions regarding Needle Engine, three.js, and basic Unity or Blender help. Please provide your OS, Needle Engine version, and any relevant Unity or Blender version youāre using. Feel free to reply with your question, and Iāll assist you further!
2 replies left - GPT 4O thread_OXU8CPTYay7tMwCnedzxdY9C Reply to this message to continue the conversation
To have your Agent detect collisions properly, itās essential to ensure that both the MovingObject and the Agent have appropriate colliders set up, and that the Agent can trigger collision events. Since youāre using a trigger-based approach, you need to ensure that the colliders on both objects are set to āTriggerā.
Hereās a refined plan and the corrected MovingObject class:
Ensure both MovingObject and Agent have colliders attached.
Set the colliders to āTriggerā in their properties.
Update the onTriggerEnter method to perform the desired action when a collision is detected.
Updated TypeScript Code
import { Behaviour, serializable, Collider } from "@needle-tools/engine";
import { Vector3, Clock } from "three";
export class MovingObject extends Behaviour {
@serializable(Vector3)
direction: Vector3 = new Vector3(1, 0, 0); // Move along the X-axis
@serializable()
speed: number = 2;
@serializable()
distance: number = 5;
private startPosition: Vector3 = new Vector3();
private movingForward: boolean = true;
private clock: Clock = new Clock();
start() {
this.startPosition.copy(this.gameObject.position);
}
update() {
const deltaTime = this.clock.getDelta();
const delta = this.direction.clone().multiplyScalar(this.speed * (this.movingForward ? 1 : -1) * deltaTime);
this.gameObject.position.add(delta);
if (this.gameObject.position.distanceTo(this.startPosition) >= this.distance) {
this.movingForward = !this.movingForward;
}
}
onTriggerEnter(other: Collider) {
const collidedObject = other.gameObject;
console.log("Trigger detected with:", collidedObject.name);
// Check if the collided object is an Agent
if (collidedObject.name === "Agent") {
console.log("Trigger with Agent detected.");
collidedObject.destroy();
console.log("Agent killed by moving object.");
}
}
}
Tips:
Trigger Colliders: Ensure that both your moving object and agents have their colliders marked as triggers.
Debugging: Use console.log() effectively to verify collision detection and object state during gameplay.
Feel free to ask if you have more questions!
1 replies left - GPT 4O thread_OXU8CPTYay7tMwCnedzxdY9C Reply to this message to continue the conversation
I donāt see anything getting logged in the console when they collide, they pass right through each other. I confirmed that āis triggeredā is enabled on both. Can you recommend another script or method for detecting a hit, like using overlap instead of collide?
Hi 0nederful! If your agents are passing through each other without triggering the events, you might want to implement an overlap detection method. Instead of relying solely on trigger events, you can manually check overlaps between the agents and the moving object during the update cycle.
Hereās an adjusted approach using a simple overlap check in the update() function of the MovingObject class:
Modified TypeScript Code
import { Behaviour, serializable, Collider } from "@needle-tools/engine";
import { Vector3, Clock } from "three";
export class MovingObject extends Behaviour {
@serializable(Vector3)
direction: Vector3 = new Vector3(1, 0, 0); // Move along the X-axis
@serializable()
speed: number = 2;
@serializable()
distance: number = 5;
private startPosition: Vector3 = new Vector3();
private movingForward: boolean = true;
private clock: Clock = new Clock();
start() {
this.startPosition.copy(this.gameObject.position);
}
update() {
const deltaTime = this.clock.getDelta();
const delta = this.direction.clone().multiplyScalar(this.speed * (this.movingForward ? 1 : -1) * deltaTime);
this.gameObject.position.add(delta);
if (this.gameObject.position.distanceTo(this.startPosition) >= this.distance) {
this.movingForward = !this.movingForward;
}
this.checkForOverlap();
}
checkForOverlap() {
// Assuming you have a way to get all agents in the scene or a pool
const agents = this.getAgentsInScene(); // Method to retrieve agents
for (const agent of agents) {
if (this.checkIfOverlapping(agent)) {
console.log("Overlap detected with Agent:", agent.name);
agent.destroy(); // Action to destroy the agent
console.log("Agent destroyed.");
}
}
}
checkIfOverlapping(agent: Collider) {
return this.gameObject.collider.bounds.intersects(agent.collider.bounds);
}
getAgentsInScene(): Collider[] {
// Implement logic to return all active agents in the scene
return [];
}
}
Key Changes:
Manual Overlap Check: Use a new method called checkForOverlap that loops through all agents, checking if their bounds intersect with the moving objectās collider bounds.
Agent Detection: Implement a getAgentsInScene method to collect all agent references. You will need to fill this in based on your game structure.
This approach will allow you to detect overlaps even when trigger events arenāt firing as expected. Let me know if you have more questions!
0 replies left - GPT 4O thread_OXU8CPTYay7tMwCnedzxdY9C Reply to this message to continue the conversation
Hi kipash! Youāre correct that the onTriggerEnter() method should indeed work for collision detection if properly set up. If itās not functioning, it could be indicative of a bug or misconfiguration in your project.
To troubleshoot:
Test the CharacterController Sample: As you mentioned, run the CharacterController sample to confirm that the trigger interactions are functioning as expected. The pressure plates should activate the moving platform when stepped on.
Check Configuration: Ensure that:
The colliders are correctly set as triggers.
The scripts and components are correctly initialized.
Debugging: Add console logs in the onTriggerEnter() method to verify if itās being called, along with checks on the status of other relevant variables.
If the sample works fine, you may want to compare its setup to your own project to find any discrepancies.
If you have further questions or need additional assistance, feel free to ask!
1 replies left - GPT 4O thread_OXU8CPTYay7tMwCnedzxdY9C Reply to this message to continue the conversation