0 votes

The old question seems outdated. I ask a new one
This the Player Bullet's Code

extends KinematicBody2D
var pos=Vector2.ZERO
func _physics_process(delta):

it goes in hexagon line, not straight line. and get slower too because of closer distance

in Engine by (28 points)

1 Answer

0 votes
Best answer

when moving a KinematicBody2D it's not good practice to set the position directly, rather you should use moveandslide or moveandcollide so you get the benefit of the collision detection.

There is an intro tutorial here

In your case you could do something like this

extends KinematicBody2D

var speed = 10

func _physics_process(delta):
    var vec_to_player = (GV.PlayerBullet.position - position).normalized()
    rotation = vec_to_player.angle()
    var collision = move_and_collide( vec_to_player * speed)
    if collision:
        print("Collided with ", collision.collider.name)

This calculates the vector from player and normalizes it, so that gives you a direction.
Then, from that direction you can set your rotation.
Multiply that vector by the speed to get a scaled velocity vector, and call moveandcollide
This will return a collision object if it collided, which you need to handle

I don't know how your game will work, however this model will update the direction if the GV.PlayerBullet.position moves. If that's not what you want, you need to store the vectoplayer and not update it each frame. Also, you might prefer moveandslide to moveandcollide depending on your design. Check out the tutorial above for advice.

by (541 points)
selected by

It worked for me, thanks

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 Frequently asked questions and 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 [email protected] with your username.