• 3D
  • How get position of the RayCast node collision_point on the walls/floors?

Hi,...I'm working on my simple FPS template for my possibly upcoming FPS projects.... ...is based on the TwistedTwigleg FPS tutorial (Thanks for that great work!).....when I followed the first part and simplified the player script for my needs on a simpler fps. ....so I have a shoot function where I shooting ray to the walls and floor...to spawn some sparks (instanced particle node) .....ray shoot great...StaticBodies/walls/floors are recognized reliably....but sparks particles appears always in the center of the wall or floor and not where the ray projectile hit wall/floor. I'm using simple script

    func shoot(delta):
        var point = ray_projectile.get_collision_point().normalized()
        var collider
        if Input.is_action_just_pressed("shoot"):
             ray_projectile.set_cast_to(Vector3(0, 0, -150))
             if ray_projectile.is_colliding():
                  collider = ray_projectile.get_collider()
                  var hit_new = hit.instance() #sparks particle
                  collider.add_child(hit_new)
                  print(collider)

....so far half-works but not at the ray hit position :( ....Thanks for your help guys.



Hey @Schuster!

I made some changes and added some comments explaining what I changed. Hopefully it helps :smile:

    func shoot(delta):
	var collider
	if Input.is_action_just_pressed("shoot"):
		ray_projectile.set_cast_to(Vector3(0, 0, -150))
		# While this is not necessarily needed, it is probably a good idea to force
		# the raycast to update before checking if it has collided.
		# (you may only need this If ray_projectile.enabled is false)
		ray_projectile.force_raycast_update()
		if ray_projectile.is_colliding():
			collider = ray_projectile.get_collider()
			var hit_new = hit.instance() #sparks particle
			# Side note: If you add the particles as a child of the collider, when the collider moves so will
			# the particles. It is not per say an issue, but it is something to keep in mind :)
			collider.add_child(hit_new)
			# Because we have updated and changed the raycast's properties, we'll need to get
			# the position of the raycast collision here if we want to have the most up to date
			# collision position.
			# Also, get_collision_point returns the position of the raycast collision in global space, so
			# we'll use it to set the global position of the sparks.
			var point = ray_projectile.get_collision_point()
			# Set the position of the particles to the position of the raycast collision
			hit_new.global_transform.origin = point
			print(collider)

Side note: I'm super happy that the FPS tutorial has helped! It is always cool seeing that it has helped someone, as when you are writing tutorials you really have no idea whether it will be helpful or not.

I'm glad the tutorial has been helpful, and I hope your FPS template goes well! :smile:

Big Thanks @TwistedTwigleg for this great help....works great now...your help is amazing. I am happy that you are here in this community and your tutorials are very great and excellently done......totally helpful. I'm planning a shorter fps project inspired by film and book from Herbert George Wells The Island of Dr.Moreau.....mainly the atmosphere of the seemingly abandoned island and the mad scientist and his experiments.

@Schuster said: Big Thanks @TwistedTwigleg for this great help....works great now...your help is amazing.

Great! I'm glad it is working now! As for helping, it is my pleasure!

I am happy that you are here in this community and your tutorials are very great and excellently done......totally helpful.

Thanks, I really appreciate it!

I am very thankful and happy that I get to be part of such a great community composed of so many great people from all over. You, and everyone else here, all make the community such a great place and I am ever thankful that I can be a part of it :smiley:

I'm planning a shorter fps project inspired by film and book from Herbert George Wells The Island of Dr.Moreau.....mainly the atmosphere of the seemingly abandoned island and the mad scientist and his experiments.

Sounds cool!

I have not read The Island of Dr. Moreau, but to get an idea I took a look at the Wikipedia page for the book. The plot on the Wikipedia article sort of reminds me of the Amnesia series of games. All in all, it looks like interesting material to work with.

I wish you the best of luck working on it! If you encounter any roadblocks and need help, let me know and I will see what I can do to try to help :smile: