[SOLVED] Raycasting from a gun

Hi,
I am hoping someone could point me in the right direction.
raycastFirst takes a start and end position.
Calculating the start position is easy as that is just the position of the gun which is trivial.
The end position I am not sure how to get to?
I have the orientation of the gun as a Quaternion (q) and I am guessing that I use some distance factor (d). And somehow I imagine I can calculate the end position based on the start position, orientation and distance but i don’t know the maths. Could anyone give me a steer?

You can get the ‘forward’ direction of an entity from entity.forward. Forward is defined as facing along negative Z.

The longer version…

var distance = 10.0;
var start = entity.getPosition();
var end = new pc.Vec3();
var ray = new pc.Vec3();

// create a vector in the direction of the entity forward of length distance.
ray.copy(entity.forward).scale(distance)
// get end point as start + ray
end.copy(start).add(ray);

Shortened code, less clear, less allocation

var distance = 10.0;
var start = entity.getPosition();
var end = new pc.Vec3();
end.copy(entity.forward).scale(distance).add(start);

awesome, thanks dave much appreciated!

1 Like

A post was split to a new topic: How to make a spark spawn at the end of a gun