Use the button to reset the object position of the scene

Hello, can you help me see I use this code attached to the cube, and then use the reset () method of button click call, why no reaction

Hi Josh! I’m an AI-powered assistant here to help you with questions about Needle Engine, three.js, and basic Unity or Blender issues. Please reply with the necessary details like your OS, Needle Engine version, and whichever Unity or Blender version you are using, and I’ll assist you further!

2 replies left - GPT 4O
thread_5xnyJpRL8uSFPJmoZKfPfBK1
Reply to this message to continue the conversation

import { Behaviour, Renderer, Rigidbody, serializable } from “@needle-tools/engine”;
import { Vector3, Euler } from “three”;

export class ResetPosition extends Behaviour {

@serializable()
resetPosition: boolean = true;
@serializable()
resetRotation: boolean = false;
@serializable()
resetVelocity: boolean = false;
private pos: Vector3 = new Vector3();
private rot: Euler = new Euler();
private rb: Rigidbody | null = null;
private startFrame: number = 0;
start() {
    this.pos.copy(this.gameObject.position.clone());
    this.rot.copy(this.gameObject.rotation.clone());
    this.rb = this.gameObject.getComponent(Rigidbody);
    this.startFrame = this.context.time.frame;
}
public reset() {
    if (this.resetPosition)
        this.gameObject.position.copy(this.pos);

    if (this.resetRotation)
        this.gameObject.rotation.copy(this.rot);

    if (this.rb && this.resetVelocity && this.context.time.frame !== this.startFrame)
        this.rb.resetVelocities();
}

}

Hello Josh,

can you confirm that the reset method is actually called? How did you setup your scene?

Here is a basic script with the same behaviour on stackblitz that works:

Hello, I am using button UI in unity, use click function for reset
17
but after I click the button, it not working

Its working now, thank you, if you have time can you help me check the other question about the layout of buttons I put last week :grinning:

What did you change?

I put in the reset item object in the click function, also attach reset script in reset item,