Adding lasers in cospaces


#1

I am wondering if it is possible to add guns and shooting in cospaces for the user. I am making a virtual reality nerf war game on cospaces, and I am wondering if this is possible to make.

Kind regards,

Johan, 15


#2

You can add the STL files from any 3d object into cospaces. create the illusion of firing with coded objects that appear when the gun is clicked and set out at a trajectory matching the aim of the weapon.


#3

Hi Johan,

yes, it’s totally possible to add guns, but you need to be at least 18!

Ok, all jokes aside :slight_smile: We don’t have any gun models in CoSpaces Edu. But you can make your own by grouping objects together or you can import 3D models into CoSpaces Edu as well.

The interesting question is more how to implement the shooting mechanics. At the moment you need to do some programming to implement something like this. We’re also working on so called “Smart Items”. Then we can pre-program such a gun which you can use in your own spaces. But this will be a future feature.

I experimented a little bit today and I found that the easiest solution is to implement a laser and when the laser is hitting another object, this object just disappears from the scene.

Here is the link to the space (you can shoot the laser by clicking the left mouse button): https://cospac.es/edu/TXk5

And here is the code. You can copy it into your editor to try it out.

const cam = Scene.getCamera();
let camPos, camDir;
cam.setName('cam');

const laser = Scene.createItem('Cuboid', 0, 0, 0);
let laserLength = 0.1;
laser.setColor(255, 0, 0);

let isLaserInitialized = false;

const laserStart = () => {
    laserLength = 100;
    laser.setSize(0.005, laserLength, 0.005);
    laser.setOpacity(0.8);

    isLaserInitialized = true;
};

const laserFire = () => {
    camPos = cam.getPosition();
    camDir = cam.getDirection();

    laser.setDirection(camDir.x, camDir.y, camDir.z);
    laser.setPosition(camPos.x, camPos.y, camPos.z - 0.2);
    laser.addLocalPosition(0.5, laserLength / 2, 0);
};

const laserStop = () => {
    laserLength = 0.1;
    laser.setSize(0.005, laserLength, 0.005);
    laser.setOpacity(0);

    isLaserInitialized = false;
};

laser.onCollisionEnter((otherObj) => {
    if(otherObj.name() !== 'cam') otherObj.deleteFromScene();
});

Scene.onButtonPressed(() => {
    if(!isLaserInitialized) laserStart();
    laserFire();
});

Scene.onButtonUp(() => {
    laserStop();
});

#5

I suggest NOT to add guns to CoSpaces’s library.
If someone needs one, let them import it themselves.


#6

Hi @arivera,

rest assured that we will never offer any sensitive material in our official library. Students can, under supervision of their teachers, upload items that are deemed important for knowledge sharing and illustrating key topics for a presentation or other homework.