|
|
|
|
Reply From: |
wombatstampede |
First, I made some post in the godot developers forum about Physics and Transforms. For general reading:
https://godotdevelopers.org/forum/discussion/18480/godot-3d-vector-physics-cheat-sheet
This part of the post might help you with your question:
mycam.look_at_from_pos(Vector3(mycam.get_translation().x, \
target.get_translation().y,mycam.get_translation().z), \
target.get_translation(),Vector3(0,1,0))
This is just the looking direction though. If you want to place the camera at some “2D” direction vector relative to the targets rotation you could split the targets transform.basis.
var target2DDir = Vector3(target.transform.basis.z.x,0, \
target.transform.basis.z.z)
This code takes the direction Vector3 of the Z-Axis of the target object. (Which I assume as its “view” direction). Then it extracts the actual x and z coordinates of this axis.
You can take the resulting vector (maybe * -1), normalize it (–> length 1), and multiply it by the required camera distance. Than calculate the camera position by adding this to the target.transform.origin (=translation). If the target looks directly “up” or “down” this would set the camera directly on the target though.
By the way: You can (& should) omit get_node(".")
when addressing own properties from the nodes own script. In places where you need an explicit reference to the instance/node (not here) you can use self
.
Hope I understood your question
Oh, and if you want to move the camera in a smoother way then there’s Tween
and follow_property()
as one possibility.
Cool thanks for the response and link. Dealing with Vector3 coordinates and 3D Transforms isn’t coming naturally to me to tell you the truth, I’ve gone back to 2D ones to try and get my head around things first.
nu-fulla | 2019-02-12 16:48
No problem. Dealing with 3D transforms I always find myself making silly pilot-like movements, brain gets messed up, trying to sketch something on paper until i (hopefully) get there.
wombatstampede | 2019-02-12 17:28
Sorry to dumb things way down but what is the proper syntax for .set_global_transform
?
When I do:
func _ready():
var GT = .get_global_transform()
var zero = GT[0]
var one = GT[1]
var two = GT[2]
var three = GT[3]
print("Global Transform is ",GT)
print("X is ", zero)
print("Y is ", one)
print("Z is ", two)
print("Origin is ", three)
I get:
Global Transform is 1, 0, 0, 0, 1, 0, 0, 0, 1 - 0, 1.83918, 2.4662
X is (1, 0, 0)
Y is (0, 1, 0)
Z is (0, 0, 1)
Origin is (0, 1.83918, 2.4662)
https://docs.godotengine.org/en/latest/classes/class_transform.html#class-transform
^ Says:
Transform | Transform ( Vector3 x_axis, Vector3 y_axis, Vector3 z_axis, Vector3 origin )
So I thought I would write (if I hypothetically wanted to move a camera to the same place):
get_node("/root/Camera").set_global_transform(Transform (1, 0, 0), (0,1, 0), (0, 0, 1), (0, 1.83918, 2.4662))
But I get an error saying Expected ')' in expression
I can’t find an example of someone actually using .set_global_transform
with a string of coordinates written.
It works if I just do :
get_node("/root/Camera").set_global_transform(GT)
But not if I enter the actual characters of GT
, either verbatim or in the syntax the docs say.
nu-fulla | 2019-02-13 06:15
get_node("/root/Camera").set_global_transform( \
Transform (Vector3(1, 0, 0), \
Vector3(0,1, 0), Vector3(0, 0, 1), Vector3(0, 1.83918, 2.4662) \
) \
)
But you can also set/change parts of a spatials (or camera) transform:
$"/root/Camera".global_transform.basis.x = Vector3(1,0,0)
(although setting a single basis vector generally makes no sense) or
$"/root/Camera".transform.origin.x += 1.5
wombatstampede | 2019-02-13 07:19
I started back at trying to fix this. If I do the following it prints the coordinates roughly were I want the camera to be, the same as the player but with only the x translate modified.
extends Camera
func _process(delta():
var playerlocation = get_node("/root/World/Player").get_global_transform()
print (playerlocation * Vector3 (2, 0, 0), Vector3 (0, 0, 0), Vector3 (1, 0, 0), Vector3 (0, 1, 0))
But I can’t create a variable of it to actually use and apply to a camera I keep getting “Expected ‘)’ in expression” no matter how I use the quotes.
var cameralocation = (playerlocation * Vector3 (2, 0, 0), Vector3 (0, 0, 0), Vector3 (1, 0, 0), Vector3 (0, 1, 0))
nu-fulla | 2019-03-15 22:17