How do I make shooting in 3D properly?

0 favourites
  • 12 posts
From the Asset Store
Shoot balls to destroy as many blocks as possible, at each stage the game will become more difficult.
  • Hey. With C3's recently added 3D camera feature I started experementing with it's possibilities and stuck on making shooting FPS mechanic.

    Imagine an enemy, standing on the high ground. How do I reach him from below?

    How do I make a bullet that will fly up and down in the direction of my view?

    Thanks

  • tan(angle) will get you the slope of your camera angle. Multiply that by the distance and zscale to get the z height at any given distance. Remember to also add the z height of the origin/camera as well.

    Generally speaking though, Construct is still a 2d engine. A 2d engine with 3d elements features does not make it equivalent to a 3d engine. Making an fps is mostly an exercise in futility. You're only going to run into further road blocks down the line without 3d collisions, volumetric fog/lighting, sloped vertical surfaces ect. This is similar to the limitations of the Doom 2/Wolfenstein era, where you notably could not look up and down. Your effort would likely have better return if you spent it learning how to use an actual 3d engine like Godot or Unity instead if your dream is to make a first person shooter.

    But if you do want to continue, I highly recommend looking in to some basic trigonometry and geometry. It will be invaluable for working in 3d space, and you're not going to be able to get very far without it.

  • Hey. With C3's recently added 3D camera feature I started experementing with it's possibilities and stuck on making shooting FPS mechanic.

    Imagine an enemy, standing on the high ground. How do I reach him from below?

    How do I make a bullet that will fly up and down in the direction of my view?

    Thanks

    This guy has done some wizardry with 1st and 3rd person 3D cameras in C3, though when I really tried to dig into it I decided to stick with the simpler approach in the C3 demos for my retro (early) 90s FPS, which I should be releasing very soon.

    Subscribe to Construct videos now
  • tan(angle) will get you the slope of your camera angle. Multiply that by the distance and zscale to get the z height at any given distance. Remember to also add the z height of the origin/camera as well.

    Thanks for your response! Your solution will work with raytrace, but what I am trying to achieve is to make flying visible bullet, which may be a little harder to implement.

    I tried multiplying bullet's Z elevation by 3DCamera's LookZ every tick, but it seems to be wrong. Any ideas?

  • do you want to shoot the bullet in the direction the camera is facing or do you want to shoot towards the enemy no matter what?

    For both first you need to add three instance variables to the bullet sprite for the velocity. I like to use vx,vy and vz.

    Then to make the sprite move you'd add an event like this:

    every tick
    -- sprite: set x to self.x+self.vx*dt 
    -- sprite: set y to self.y+self.vy*dt 
    -- sprite: set zelevation to self.zelevation+self.vz*dt 

    Now to shoot forward you'd set the velocities like this when you create the bullet:

    on click
    -- create sprite at (3dcamera.cameraX, 3dcamera.cameraY)
    -- sprite: set zelevation to 3dcamera.cameraZ
    -- sprite: set vx to 100*3dcamera.forwardX
    -- sprite: set vy to 100*3dcamera.forwardY
    -- sprite: set vz to 100*3dcamera.forwardZ 

    The 100 is the speed.

    To just shoot toward the enemy you'd do this:

    global number factor=0
    
    on click
    -- create sprite at (3dcamera.cameraX, 3dcamera.cameraY)
    -- sprite: set zelevation to 3dcamera.cameraZ
    -- sprite: set vx to enemy.x-3dcamera.cameraX
    -- sprite: set vy to enemy.y-3dcamera.cameraY
    -- sprite: set vz to enemy.zelevation-3dcamera.cameraZ
    -- set factor to 100/sqrt(sprite.vx^2+sprite.vy^2+sprite.vz^2)
    -- sprite: set vx to self.vx*factor
    -- sprite: set vy to self.vy*factor
    -- sprite: set vz to self.vz*factor
  • do you want to shoot the bullet in the direction the camera is facing or do you want to shoot towards the enemy no matter what?

    For both first you need to add three instance variables to the bullet sprite for the velocity. I like to use vx,vy and vz.

    Then to make the sprite move you'd add an event like this:

    every tick
    -- sprite: set x to self.x+self.vx*dt 
    -- sprite: set y to self.y+self.vy*dt 
    -- sprite: set zelevation to self.zelevation+self.vz*dt 

    Now to shoot forward you'd set the velocities like this when you create the bullet:

    on click
    -- create sprite at (3dcamera.cameraX, 3dcamera.cameraY)
    -- sprite: set zelevation to 3dcamera.cameraZ
    -- sprite: set vx to 100*3dcamera.forwardX
    -- sprite: set vy to 100*3dcamera.forwardY
    -- sprite: set vz to 100*3dcamera.forwardZ 

    The 100 is the speed.

    To just shoot toward the enemy you'd do this:

    global number factor=0
    
    on click
    -- create sprite at (3dcamera.cameraX, 3dcamera.cameraY)
    -- sprite: set zelevation to 3dcamera.cameraZ
    -- sprite: set vx to enemy.x-3dcamera.cameraX
    -- sprite: set vy to enemy.y-3dcamera.cameraY
    -- sprite: set vz to enemy.zelevation-3dcamera.cameraZ
    -- set factor to 100/sqrt(sprite.vx^2+sprite.vy^2+sprite.vz^2)
    -- sprite: set vx to self.vx*factor
    -- sprite: set vy to self.vy*factor
    -- sprite: set vz to self.vz*factor

    Thanks for your response! It seems like ForwardX/Y/Z does not change for me when I move my camera. I even tried ths expressions in Scirra's "3D Platformer" example, but unsuccessfully.

  • Oh strange. I didn’t test it but that sounds like a bug then.

    Guess you could move the camera forward by one, save it’s position to variables. Then move back one and subtract the position from the same variables. That would give you a forward vector xyz.

  • Oh strange. I didn’t test it but that sounds like a bug then.

    Guess you could move the camera forward by one, save it’s position to variables. Then move back one and subtract the position from the same variables. That would give you a forward vector xyz.

    Thanks again. I apologize if I do not understand something, but this solution does not work either.

  • No that looks right. It would be going 1pixel/sec though. Instead of going forward and back by 1 you can move forward and back by the speed you want it to move.

    If that still doesn’t work then I’m misunderstanding something with how c3 does 3D.

  • r271 still haven't fixed this issue.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • This guy has done some wizardry with 1st and 3rd person 3D cameras in C3, though when I really tried to dig into it I decided to stick with the simpler approach in the C3 demos for my retro (early) 90s FPS, which I should be releasing very soon.

    Subscribe to Construct videos now

    Just wanted to point out that the implementation from this video (project file linked in description) appears to work flawlessly.

  • Thanks! That actually worked.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)