0 votes

So I am trying to create a slash & hit effect for my game, whenever that the player tries to hit an enemy, the player would cast a ray, the effect would appear at the collision position. how ever, the player's attack would hit multiple enemies. this is where the problem comes in, they ray would only hit and be blocked by the first enemy or worse, something else.

here is my code:

func _on_Attack_coli_body_entered(body: Node) -> void:
    var slash_effect = preload("res://crs/effects/attackanim/swordslide.tscn").instance()
    var spacestate = get_world_2d().direct_space_state
    var result = spacestate.intersect_ray(global_position,body.global_position,["exclude every one but 'body'"])

    print(result)

    get_parent().add_child(slash_effect)
    slash_effect.position = result.position
    slash_effect.rotation = (position - body.position).angle()+1.5

how can I make the raycast only cast on a particular body?

in Engine by (26 points)

3 Answers

0 votes
Best answer

Nvm, someone on Reddit helped me figured it out :)

https://www.reddit.com/r/godot/comments/hzen7x/how_do_i_make_raycast_only_collide_with_one/

So what I did is I created an array for the body that needs to be excluded whenever the ray hits the wrong body, it adds that body to the array and re-raycast again until it hits the right body or hits nothing.

here's my code:

var exclude_body = [self]
var hit_pos = Vector2.ZERO
var empty = true

while empty:
    var spacestate = get_world_2d().direct_space_state
    var result = spacestate.intersect_ray(global_position,body.global_position,exclude_body)
    if result.collider != body:
        exclude_body.insert(0,result.collider)
    else:
        if result.collider == body:
            hit_pos = result.position
            print(result)
            break
        elif result == null:
            break
by (26 points)
0 votes

Hi, you can add a group on the body and use .is_in_group()

by (53 points)
0 votes

intersect_ray also takes in collision_mask which you can use to determine which layer of objects the ray is to collide with:

intersectray(from: Vector3, to: Vector3, exclude: Array = [ ], collisionmask: int = 0x7FFFFFFF, collidewithbodies: bool = true, collidewithareas: bool = false)

by (389 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.