this post was submitted on 24 Jul 2025
10 points (100.0% liked)

Godot

6914 readers
15 users here now

Welcome to the programming.dev Godot community!

This is a place where you can discuss about anything relating to the Godot game engine. Feel free to ask questions, post tutorials, show off your godot game, etc.

Make sure to follow the Godot CoC while chatting

We have a matrix room that can be used for chatting with other members of the community here

Links

Other Communities

Rules

We have a four strike system in this community where you get warned the first time you break a rule, then given a week ban, then given a year ban, then a permanent ban. Certain actions may bypass this and go straight to permanent ban if severe enough and done with malicious intent

Wormhole

!roguelikedev@programming.dev

Credits

founded 2 years ago
MODERATORS
 

I'm working on a two player 3D game that involves both player character models facing toward one another most of the time.

I worked out that i can use translate_object_local(Vector3.MODEL_<side>) for movement around a point that a model faces toward (there's probably a better way to do that, but that's not what i'm focused on for now), but i'm struggling to make that focus point an object instead of just coordinates.

I've tried a few things inside func _physics_process(delta): look_at($Beacon.position.x, Vector3.UP, $Beacon.position.z) to rotate around the Y axis and face the object Beacon (CharacterBody3D placeholder for player 2) crashes with the error "Invalid access to property or key 'position' on a base object of type 'null instance'."

Putting var look_here = position in the script for the beacon and then look_at($"../Beacon".look_here) in TestChar (test character) makes the character rotate around the world origin.

Adding a Vector3 variable look_here_P1 to Beacon's script and then adding or subtracting to its X and Y positions with beacon's movement keys should make TestChar face a point that's in the same place as Beacon, but i don't know how to either make TestChar use that that variable from Beacon's script, or move the variable to TestChar and make Beacon update it now that it's in another script.

Making Beacon emit a signal every time it moves in a particular direction and then using those signals to update a Vector3 variable in TestChar's script doesn't seem to work, i think either it doesn't update the variable's position or look_at() in _physics_process doesn't check for a new target to look at.

Is there a simple way to make an object always look at another object? I'm sure there must be but i can't seem to figure it out. Thanks in advance.

top 4 comments
sorted by: hot top controversial new old
[–] Windex007@lemmy.world 6 points 4 days ago

You certainly want look_at.

I use c# for scripting so I'm not super familiar with the gdscript nomenclature but it looks like youre not referring to beacon properly.

Keep in mind look_at uses global coords. You probably want to be looking at the globalposition of beacon. This might be why you find yourself looking where you don't expect. "Position" is relative a nodes parent node. Globalposition is absolute position in worldspace

[–] markz@suppo.fi 4 points 4 days ago* (last edited 4 days ago)

on a base object of type 'null instance'."

Null instance means there's no instance. If $Beacon gives you this, it means that such node doesn't exist.

As a side note, I dislike hardcoding nodes like this. Consider getting the beacon instance beforehand with @onready var or such. If the beacon doesn't exist, you'll then see the error immediately instead of later when the script tries to use it.

makes the character rotate around the world origin.

You're using position, which is the relative position to the parent node. I don't know how your node structure look like, but chances are that you should be using global_position (absolute world position) instead.

and then adding or subtracting to its X and Y positions with beacon's movement keys

oh boy. When find yourself building something like your own position tracking code instead of using builtin systems, it's best to scrap it and rethink what you need.

how to either make TestChar use that that variable from Beacon's script

Same was as position: look_at(beacon.look_here_P1). Add a class name for the beacon script and type your variable as such (var beacon: Beacon = ...) to get editor hints.

Protip: speaking from experience, you might want to enable this setting:

screenshot

Making Beacon emit a signal every time it moves in a particular direction and then using those signals to update a Vector3 variable in TestChar's script doesn't seem to work

Rube Goldberg machine much? It should work though. Your problem is probably still relative vs absolute position.

Hope this helps

[–] ConditionalCoder@mastodon.gamedev.place 0 points 4 days ago (1 children)

@IndigoGollum Tell each object that the other object is prettier than they are.

[–] IndigoGollum@lemmy.world 1 points 1 day ago

Sure, what's the syntax for that⸮