Trouble with defining target object

var Raycast = pc.createScript('raycast');

Raycast.attributes.add('targetObject', {

    type: 'entity',

    title: 'Target',

});

// initialize code called once per entity

Raycast.prototype.initialize = function() {

this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.doRaycast, this);

};

Raycast.prototype.doRaycast = function(MouseEvent){

  var from = this.entity.getPosition();

  var to = this.entity.camera.screenToWorld(MouseEvent.x, MouseEvent.y, this.entity.camera.farClip);

  var result = this.app.systems.rigidbody.raycastFirst(from, to);

  if(result){

      this.targetPoint = result.point;

      this.xMovingSpeed = this.entity.position.x - targetObject.x;

      this.zMovingSpeed = this.entity.position.z - targetObject.z;

      this.yMovingspeed = this.entity.position.y - targetObject.y;

  }

};

// swap method called for script hot-reloading

// inherit your script state here

// Raycast.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:

// https://developer.playcanvas.com/en/user-manual/scripting/

https://playcanvas.com/editor/scene/1458391

I am trying to make my camera zoom in on the defined target when the target is clicked but I can’t figure out how to define targetObject it properly

Hi @MoonAlien822! If you want to access your attribute you need to use this.targetObject.

I have made this change and no errors are showing but my code stil doesn’t seem to work

What do you expect to happen?

I tried to make it so that, when I press the white square with the praying hand, the camera moves towards that square

I don’t know how to achieve that, but right now you don’t do anything to move the camera entity. You probably only set the needed properties.

Maybe the topic below can also help you.

isn’t there already code to move the camera within the raycast script?

I don’t think so. As far I can see you set some values for a speed, but you don’t apply these values anywhere. I expect to see something like below, but this will probably don’t give the right result.

this.entity.translateLocal(this.xMovingSpeed, this.yMovingSpeed, this.zMovingSpeed);

Where would I need to put that? also, why wouldn’t that work? it seems alright to me

You can put it in the update function (if the properties you use are defined in the initialize function) or you can put it in the raycast result (below your properties), but I guess none of them will give you the desired result.

why not?

Because I don’t think it’s the right way to do it. How did you get the code you shared?

which part?

this.xMovingSpeed = this.entity.position.x - targetObject.x;
this.zMovingSpeed = this.entity.position.z - targetObject.z;
this.yMovingspeed = this.entity.position.y - targetObject.y;

uhhhh… I don’t really remember. I come up with it myself at least.

Alright, then you know what the logic should be. What do you want to do with the the 3 values above?

Worst mistake ever, but I meant “I didn’t come up with it myself at least“

Unfortunately, I don’t know what the logic of the code should be, so I can’t help you further with it.