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.
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.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 usingglobal_position
(absolute world position) instead.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.
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
Rube Goldberg machine much? It should work though. Your problem is probably still relative vs absolute position.
Hope this helps