[SOLVED] How to set raycast to draw from one entity to another

I’m making a fps game and I need a ray cast to go from the gun top to the pointer entity 200 x in front. But it’s not working right

Hi @Granted,

Try sharing your current progress (code and if possible a sample project). And if it helps the way you tried to do it and what’s not working right.

current code

var Newgun = pc.createScript('newgun');
Newgun.attributes.add('rayends',{type:'entity'});
Newgun.attributes.add('begin',{type:'entity'});

// initialize code called once per entity
Newgun.prototype.initialize = function() {
    
};

// update code called every frame
Newgun.prototype.update = function(dt) {
    if(this.app.mouse.wasPressed(pc.MOUSEBUTTON_LEFT)){
    const start = new pc.Vec3();
    const end = new pc.Vec3();
    var posp=this.rayends.getPosition();
    var pos=this.begin.getPosition();
    start.set(pos.x,pos.y,pos.x);
    end.set(posp.x,posp.y,posp.x);
    // Render the ray used in the raycast
    this.app.drawLine(start, end);
    const result = this.app.systems.rigidbody.raycastFirst(start, end);
    if (result) {
        console.log(result);
    }
    }
};

// swap method called for script hot-reloading
// inherit your script state here
// Newgun.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

Im so stupid. I forgot to set the z correctly. Whoops

1 Like