Hi! So, let me explain a little bit. I’m following a tutorial on fps mechanics and i’m on this part where the gun shoots and the hitscan, which is used to calculate the point where the “bullet” hits, calculates where the shot landed. Problem is, i’ve followed everything and the hitscan doesn’t work. The game isn’t crashing, it isn’t even showing an error, it just does nothing as it was intended to do…
func Get_Camera_Collision()->Vector3:
var camera = get_viewport().get_camera_3d()
var viewport = get_viewport().get_size()
var Ray_Origin = camera.project_ray_origin(viewport/2)
var Ray_End = Ray_Origin + camera.project_ray_normal(viewport/2)*Current_Weapon.Weapon_Range
var New_Intersection = PhysicsRayQueryParameters3D.create(Ray_Origin,Ray_End)
var Intersection = get_world_3d().direct_space_state.intersect_ray(New_Intersection)
if not Intersection.is_empty():
var Cel_Point = Intersection.position
return Cel_Point
else:
return Ray_End
func Hit_Scan_Collision(Collision_Point):
var Bullet_Direction = (Collision_Point - Bullet_Point.get_global_transform().origin).normalized()
var New_Intersection = PhysicsRayQueryParameters3D.create(Bullet_Point.get_global_transform().origin,Collision_Point+Bullet_Direction*2)
var Bullet_Collision = get_world_3d().direct_space_state.intersect_ray(New_Intersection)
if Bullet_Collision:
var Hit_Indicator = Debug_Bullet.instantiate()
var world = get_tree().get_root()
world.add_child(Hit_Indicator)
Hit_Indicator.global_translate(Bullet_Collision.position)
Hit_Scan_Damage(Bullet_Collision.collider)
func Hit_Scan_Damage(Collider):
if Collider.is_in_group("Target") and Collider.has_method("Hit_Successful"):
Collider.Hit_Successful(Current_Weapon.Damage)
It could be the code i’m using where something doesn’t really input the hit from the hitscan but i kinda don’t think it is.
Yes, and none returned something. There’s a print statement right by the function “Hit_Successful” of the object that i want to make my hitscan to collide with, but neither did this return something.
Oh, so basically the shot is going the opposite way i want it to?
In this new update of the code you provided, the minus sign before “(Collision_Point…)” is what i need to add? Or there’s something more?
That’s because in the tutorial it said, sometimes, the bullet doesn’t really go where you want it to. So this part was to properly set it up so it hits the target. But maybe it wasn’t so necessary?
Sorry I’m thinking I’m mistaken, I didn’t wrap my head around it fully. I think it should work fine since you are only using positions, and not explicit angles. It should be okay.
I still think the direction needs to be reversed so it will actually hit the body on the second query.
My final suggestion is to remove the weapon manager from the subviewport. or have the viewports share the world. I tested removing the world3d form the subviewport and can now hit the troll, but there are visual issues.
update
I guess as a hack, after using the exported camera you could do camera.get_viewport(...) then you wouldn’t need to reorganize your subviewport.
You need to manually assign the camera with this export variable.
Then when you go to use it
func Get_Camera_Collision()->Vector3:
var viewport = camera.get_viewport().get_size()
...
var Bullet_Collision = camera.get_world_3d().direct_space_state.intersect_ray(New_Intersection)
...
# then the rest of the code is the same
Whenever you need to use world3d use the camera form the export value.
Brother, the fact that everything that you said holds truth and there are no error or breaks, but still there seems that no bullet or hitscan signal is coming, lends me to the assumption that maybe i did something wrong and not enough code could solve it. I have an hypothesis tho:
See the “Bullet_Point”? This is the way that spawns the hitscan to calculate the direction and hit of the bullet. In the tutorial, it was clear 2 things:
1 - He assigned the bullet_point to the barrel of his guns, i really don’t think it makes any difference, but maybe i’ve done something wrong when positioning this node?
2 - As you can see below, this is the way he arranges things, which is different than mine, as the main difference is he uses a camera only for fps, but i use two cameras, one for rendering the world, and one only for rendering the guns (this being the view_model_cam)
I spent a couple hours digging through your code and I kept running into secondary code structure issues. I think your code is overdue for a major refactor.
I think the visual representation of the guns via a subviewport is fine, but you need to get the weapons_manager, hitscan, and bullet_point stuff out from under the subviewport.
My time is finite, sorry i couldn’t come up with a solution.
Ok, i think what you’re trying yo say is, that weapons_manager and all that needs to be a separate place? Well, i don’t know how i could do that and maintain it a son of view_model_cam aswell, but hey, you already helped me enough bro, thanks alot really!
Ohhh definitely gonna be checking that. If that really works, i can safely disclose all the subviewport stuff and things might get running again. Thanks again, highly appreciate all the help