[Solved] How rotate entity with a event listener?


#1

I try rotate a entity when ‘rotation’ event Is listener. But i don’t know what Is wrong.
This Is code for entity listener

var EventReceiver = pc.createScript(‘eventReceiver’);
EventReceiver.prototype.initialize = function() {
this.app.on(“rotation”, this.onRotationEvent, this);
};
EventReceiver.prototype.onRotationEvent = function(dt) {
this.entity.rotate(0, 10*dt, 0);
};

//Thais Is code for emmiter event

var EventEmitter = pc.createScript(‘eventEmitter’);
// initialize code called once per entity
EventEmitter.prototype.initialize = function() { var self = this;
self.app.fire(“rotation”); };


#2

I thought I replied to this :sweat_smile:

On my phone so bare with me. The event listener you have written expects a parameter to be passed when the event is fired.

As you are not passing a parameter, dt is undefined.

So you can either remove the parameter in the event listener and just use a hardcoded value or pass a value when you fire the event.


#3

I think he wants to ‘animate’ the rotation of the entity when an event is fired.
So his code will not work. You need to tween your entity.
Here is the project with the tween for you.
https://playcanvas.com/project/595690/overview/how-rotate-entity

BUT there is a problem I can’t understand again. When I tween the position it works fine but when I tween the rotation it rotates AND scales up. Very very strange :confused:


#4

this works for me but the movement is not continuous

var EventReceiver= pc.createScript(‘eventReceiver’);

// initialize code called once per entity
EventReceiver.prototype.initialize = function() {

this.app.on('rotation', this.onRotationEvent, this);

};

EventReceiver.prototype.onRotation = function (event) {
// Use the camera component’s screenToWorld function to convert the
// position of the mouse into a position in 3D space

    var angle = 0;
 {angle = -5;} 
{angle = 5;}


/*
 * Notice that pressing and holding the space bar makes the block
 */


// Update the spinning cube
this.entity.rotateLocal(0, angle, 0);

};


#5

What are you trying to do as an end result? It sounds like you are trying to solve a larger problem and this is just a small part of it.


#6

I tried to do something in augmented reality. the idea was that an entity receiving the event would turn every time a broadcasting entity (daughter of an augmented reality marker) appeared on the scene. That is, while arcamera approaches the marker, the issuing entity will appear and the receiving entity will begin to rotate. and it will stop if the marker stops being seen and the issuer disappears


#7

In summary, perhaps, the events are not necessary. it only takes an entity to start spinning each time the daughter entity of the marker appears on the scene


#8

Looking at the code for the AR Starter Kit, the easiest method is to check if the ArMarker entity is ‘active’ or not.

eg https://playcanvas.com/editor/scene/685058

Code here: https://playcanvas.com/editor/code/597019?tabs=16519877


#9

Solved. Thank You @Yaustar Is Perfect.
Apparently I was getting too complicated