Trying to figure out 3D shooting
Asked Answered
B

4

0

I am trying to write a little 3D Game where I drive a tank around and shoot things but I just can't figure out the proper way to handle projectiles and collisions. The driving is working great so that's not an issue.

First problem is using an actual Node as a projectile (Area Node with mesh and collisionshape). I can't seem to align the projectile with my cannon (I have the Z set so at least the shell isn't pointing upwards. I instance the shell scene and use

		Shot = Shell.instance()
		Shells.append(Shot)
		Shot.transform = player.ShotStart.global_transform
		Shot.PrevCam = playerCam
		self.add_child(Shot)

to instantiate and place the shell at the end of my cannon. Then I use

	var globdir = Vector3(0,0,1)
	var local_direction = globdir.rotated(Vector3(0,1,0), rotation.y)
	velocity = local_direction
	var col = move_and_collide(velocity)
	if col:
		print("Collision " + str(col.collider))
		queue_free()

to propel the shell. The problems are #1 Shell points sideways instead of the same direction the cannon was facing and #2 shell flies straight and LEVEL instead of at the cannon elevation angle.
Problem #3 is that it reports collision with the ground but nothing about collisions with other things (even though when the shell hits the targets go flying)

Yes I have layers set. Both 3D Render and 3D Physics have 1 - Ground, 2 - Projectiles, 3 - Enemy, and 4 - Scenery

I set the Shell layer to 2, terrain to 1, and the target I'm trying to shoot as 4. The shell flags are 1,3,4.

Even if I move to where the only possible collision would be a 4 I always get layer 1 (terrain) as the collider.

Any help? Or if someone knows of a good example somewhere I'd LOVE to see it.

Barm answered 7/2, 2023 at 21:35 Comment(0)
H
0

Barm Or if someone knows of a good example somewhere I'd LOVE to see it.

This isn't a simple example, but it does 3D shooting and works correctly:
https://docs.godotengine.org/en/3.3/tutorials/3d/fps_tutorial/index.html

There's a link near the end for downloading the completed project.

Heckle answered 7/2, 2023 at 23:41 Comment(0)
B
0

Figured out a couple of things. First placement on the shell at the end of the cannon with the proper rotations required me to set both the Y and Z rotations of the placement Position3D to 90
the placement itself is done

shot.global_transform = shotStart.global_transform

Then to move the shell I used

velocity = shell.transform.basis.z
var col = move_and_collide(shell.transform.basis.y)

Now the shell appears where I expect and travels in the direction the cannon is pointing (not just straight and level). Still working on the collisions though

Barm answered 7/2, 2023 at 23:42 Comment(0)
B
0

That's what I get for scanning quickly - I saw the FPS demo but focused on the rifle which is raycast so thought it wouldn't help. This time I looked closer and noticed grenades as rigidbodies! Switching to a rigidbody works perfectly and does things the way I was thinking in the first place too.

Thanks for answering this noob!

Barm answered 8/2, 2023 at 0:19 Comment(0)
H
0

There's also the pistol, which fires bullets (Area's).

Heckle answered 8/2, 2023 at 0:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.