How do I move a KinematicBody (3D) along another KinematicBody look_at direction?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By fox1986

I am trying to make a NPC (KinematicBody (3D) firing a bullet to the direction the NPC is looking_at.
I successfully get my bullets spawning in front of the NPC, but instead of moving along the facing direction, they are always spawing and moving 90° from the look_at direction of the npc.

This is my code of the NPC setting look_at to the players position
(that part works fine so far)

func _process(delta):
var LookAtPosition = get_node("../Player").get_translation()
look_at(LookAtPosition, UP)
var follow_vect = (LookAtPosition-get_translation()).normalized()*delta*speed
move_and_slide(follow_vect)
fall()
shoot(delta)

My shoot Function is now instancing the bullet scene (KinematicBody Node)
and I set the bullets values to the current NPCs Position & Rotation_Degree

func shoot(delta):
if fire_delay < 0:
	var shot = bullet_scene.instance()
	shot.global_transform.origin = transform.origin
	shot.rotation_degrees = rotation_degrees

	get_tree().root.add_child(shot)
	fire_delay = max_fire_delay
else:
	fire_delay -= 1.0 * delta

In my Bullet Scene I do that:

func _process(delta):
if ttl < 0:
	queue_free()
else:
	ttl -= delta
	move_and_slide(get_global_transform().basis.x * speed)

As shown on the screen below, the bullets are not spawning and moving on the look_at angle, but 90 degress from the side of the npc.

enter image description here

What’s wrong here? I’m importing my objects from blender. Is there a certain rotation to be done to define which part of the object is the front, left and right? Is there a better way to simply say: take the look_at direction as a vector and let another kinematicbody move along that vector?

1 Like
:bust_in_silhouette: Reply From: gmaps

Is your character looking along the -z axis in it’s scene? Look at rotates the -z direction towards the point you want it to look at. If your mesh is looking towards x or -x, it will look like it’s 90 degrees rotated. So if that is the case, just rotate the mesh for 90 (or -90) degrees so it looks along the -z axis.

In godot, all meshes should by default look along the -z axis.

Hi gmaps,

I don’t know how to answer to that question. I am a little bit confused in Godot when it comes to the basic orientation along the axis when I import a model (.dae Blender → Better Collada Exporter). My NPS is rotating around the y-axis while looking at the player. There is no change in the other axis.

My imported model looks like this in his scene:

enter image description here

Here’s what is quite strange:
When I create an instance of that NPC in my Level-Scene and run the game, the NPC is looking with his “back-side” at the player, not with the front-side.
So I assume that Godot somehow predicts that the front of my imported model is that what I intended to be the back side.
To solve that, I tried to change rotation degrees by 180° on my NPC’s scene. And voila, the NPC is not looking with his front at my player.
But as I said, spawning a bullet with the same rotation as the NPC is just spawning 90° on the side of the NPC for no reason.

fox1986 | 2019-10-24 16:28

I see, what if you try changing

move_and_slide(get_global_transform().basis.x * speed)

to

move_and_slide(-get_global_transform().basis.z * speed)

And also, why do you use rotation_degrees, why dont you simply copy the whole transform e.g. → shot.set_global_transform(transform).
It copies the whole transform, so you don’t have to separately copy origin and rotation.

If that doesn’t work, post a screenshot of your ship scene node hierarchy.

If it does work, you can add 4 muzzle flash objects as children of your mesh, set their position at the end of each cannon. And show them only on the frame when guns shoot. Then you can also set the transform to be from (each) one of those 4 cannons, so it doesn’t shoot from the center of the ship.

gmaps | 2019-10-25 07:25