Godot Version
4.4
Question
`I’m making a top down shooter and need a script for a CharacterBody2D that moves towards the player. That’s it. All I need.
4.4
`I’m making a top down shooter and need a script for a CharacterBody2D that moves towards the player. That’s it. All I need.
Hi,
I suggest you have a look at this (timecode attached):
That part of the tutorial does exactly what you’re looking for.
Well guess what it doesn’t work. I’m not looking for youtube tutorials, I just need code that makes a sprite move towards another sprite.
I don’t mean to be rude, but when asking help on the forum, you share your code, what you’ve tried and what isn’t working as intended.
It’s not a place to demand others to write your code for you, especially when a perfectly valid answer has been provided.
I agree with @BFranse, please share your current progress or nobody is going to try giving you a fully valid code to just copy and paste.
Also, your bio says:
Complete beginner to Godot. Would love tips on how to learn it.
Here’s one: you cannot learn how to use a game engine by copying and pasting code. And YouTube tutorials, especially from GDQuest channel, are a great way to start.
I’m sorry about that. I’ve been getting frustrated over this. I’ve done the code by myself and now have a fully working AI.Thank you for your help :).
YouTube tutorials don’t work for me, cuz a lot of them are step by step and you just mindlessly write the code he writes, so that’s also copy and pasting code basically, so I don’t see how that would be better?
There is a psychological element to writing code rather than copy and pasting it. By writing the code your brain is actually working with the code and understanding it, when copy and pasting it won’t go through those same pathways of understanding. That’s the main reason why, especially for a beginner, it’s best to write code rather than copy and paste.
But if you don’t know anything about code, then your brain doesn’t work with the code or understand it. I don’t under stand any of this:
extends Area2D
var speed: float = 800
var damage: float = 5.0
var direction: Vector2
func init(parent: Node2D)-> void:
global_position = parent.global_position
global_rotation = parent.global_rotation
direction = Vector2.RIGHT.rotated(rotation)
parent.add_child(self)
func _process(delta: float) → void:
position += direction * speed * delta
func _on_body_entered(body: Node2D) → void:
print(“hi”)
queue_free()
func _ready():
add_to_group(“bullets”)
and that was from a tutorial
That’s why a good tutorial will explain why it does what it does so that, with practice, you will be able to have a bit of memory of what to do. If this tutorial didn’t explain why it has you do that then it’s declaring variables to be used later. the direction is a Vector2 which means it carries 2 sets of numbers and is used for things like position and stuff.
the init is having it declare where it is in the scene tree, go into the rotated direction, and add it to the main scene.
the process is moving it in the direction with the speed. multiplying it by delta makes it not framerate based and will be a constant speed
the body entered is saying when it collides with something remove it
the ready says when it enters the scene add it to a group called bullets
Or I’m just too dumb. That’s a possibilty
That’s like saying that a kid dropped in a calculus class is too dumb to learn calculus. It’s because they skipped half a dozen other math classes.
Your first post makes it abundantly clear that you want shortcuts. If that’s what you want, you are going to inevitably fail. If you really want to makes games with Godot, you’re going to have to start at the beginning at some point.
Whether you write code “mindlessly” or not is up to you. I’ve been programming for many decades. I started at the beginning with Godot just like everyone else. I may have learned it faster due to other experience, but I still had to start at the beginning.
If you really want to really want to learn how to do this, start at the beginning. Your first 2D game — Godot Engine (stable) documentation in English Then after you’ve done that tutorial, I recommend you also do the 3D one. Your first 3D game — Godot Engine (stable) documentation in English
If you don’t want to start at the beginning, then I suggest you learn about vibe coding, let AI build your games and just deal with the limitations of that.
We can help you here, but as @sixrobin and @Joybit_Games explained to you, the people here aren’t here to write your game for you.
TBH that looks like code written by someone who doesn’t understand Godot or by an LLM AI. Even if you did understand it, it wouldn’t help you much. It’s unlikely to do what you want without more issues in the future.
Hi! Well, I recommend you take a look at the CharacterBody2D documentation, but to explain it simply, I recommend you assign the player object within the enemy code, get the player’s distance using something like:
var vel_multiplier = 5
self.look_at(player.position)
var target = (player.global_position - self.global_position).normalized()
velocity.x = target.x * vel_multiplier
velocity.y = target.y * vel_multiplier
This is just an example, please study the documentation and programming logic further to delve deeper into the subject and better understand how it works. The best way to learn is by trial and error.
delete me
im too stupid and dumbb
Ok dude you don’t have to be so harsh about it
Everything people are saying is use tutorials but I’m already kinda stuck in tutorial hell, and you’re kinda making it worse
Take a look at the documentation in the Getting Started section.
There is no better place to start with Godot.
Take the time and work your way through it, it’ll be worth it!
It includes everything you need to know to get started, including learning GDScript (via interactive web app).
And at the end of that section you create a simple 2D and 3D game.
When you’re done, you will, for example, understand everything from the code you posted. ![]()
Edit:
People are saying use the documentation (not tutorials). Or like back in the old days: RTFM.
Apologies. I’m not trying to be harsh, just trying to help set your expectations.
As @trizZzle said, I linked you to the documentation. And yes, I linked you to two tutorials. My point was that if you are feeling lost, it’s not your fault. You just have to start at the beginning. And the two I recommended I recommend a lot to people because it orients you to how the Godot editor works and hopefully you won’t feel like you’re drowning anymore by the time you are done. You will also know enough to start asking questions that will get you through whatever else is confusing you.
Good luck.
I wish I knew this existed when I started learning Godot