[SOLVED] Function error with getPosition()

With the code below I get the error ‘TypeError: this.target.getPosition is not a function’ on the first line of the update, but I don’t know why …

var Selector = pc.createScript('selector');

// initialize code called once per entity
Selector.prototype.initialize = function() {
    this.camera = this.app.root.findByName("Camera");
    if (this.entity.parent.name === "Player") {
        this.target = this.entity.parent.findByTag("PlayerScript");

// update code called every frame
Selector.prototype.update = function(dt) {
    this.entity.rotateLocal(0, 180, 0);

Did you break on exceptions (or set a breakpoint) on that line to see the value of this.target?

I don’t know what you mean, but with console.log(this.target.getPosition()) i get undefined.

With console.log(this.target) i get this:

Hi @Albertos,

FindByTag returns an array, so to select the first entity returned you should do this:

this.target = this.entity.parent.findByTag("PlayerScript")[0];

Just make sure with a null check that the array has at least one entity returned.

Ah! Learned something again! It does work. Thanks!

I still have problems but I think that has to do with the lookAt function. The element that I use disappears every time when I move the camera in a different position. So I don’t think this is a good way.

Yeah, LookAt plus rotateLocal can be tricky, especially with plane models, since you may end up watching the back side (which usually isn’t rendered).

What it helps to nail the right angle is to temporary use a Cone model compoment. That pointy end can help you understand what and where you are looking at.

I use a empty entity with an element to use the input function of the element. But also without the translateLocal in script, and set the entity as a child and rotate it in the editor the element dissapears (and comes back) when moving the player or rotating the camera.

I try to create a select function without using the collider of the player because these is sometimes to small to select on mobile devices.

For this I also tried to use a 2D screen element before by placing it on the position of the player, but I did not manage to do that.

I found that the problem arose only with a camera directly above the player (90 degrees). So I solved it now by setting the maximum degrees of the camera to 85 degrees.

1 Like