PointAt Other Entity after event?


#1

I tried to get my entity to point to another after hearing an event, but I can not. I think I’ve written everything well, but I just do not get it

var PointAt = pc.createScript('pointAt');

PointAt.attributes.add("targetEntity", {type: "entity", title: "Target Entity"});
PointAt.prototype.initialize = function() {
 
    this.app.on("single", this.onSingle,Event,this);
    this.defaultForwardDirection = this.entity.forward.clone();
    this.directionToTarget = new pc.Vec3();
};

PointAt.prototype.onSingleEvent = function(dt) {
    var targetPosition = this.targetEntity.getPosition();
    var pointerPosition = this.entity.getPosition();
    var pointerScale = this.entity.getLocalScale();

    var distance = pointerPosition.sub(targetPosition).length();
    pointerScale.z = distance;
            
    { this.entity.setLocalScale(pointerScale);
    
    // Always face the target postition
    this.entity.lookAt(targetPosition);}


#2

Well if that’s the code you copy and pasted, you would added an extra “,” on the line

this.app.on("single", this.onSingle,Event,this);

should be

this.app.on("single", this.onSingleEvent,this);

Because your method is called that way at the bottom, so it looks, like it’s targeting the wrong method. Also make sure to fire the event with this.app.fire(“single”); if you want to catch it globaly


#3


Cannot read property getPosicion of null :hushed:


#4

Sorry I was on Holiday…

I guess you’ve already fixed it, but you do not ever initialize “targetEntity” then of course it will be null… If you’re trying to do it on let’s say a click or a collision on an entity, then you need to pass this entity as parameter with your fired event, and then use that parameter in your method.

Hope this helps