Hi,<br /><br />I'm working on a player controller similar to the one used in the game "Alien Shooter".<br />I got the movement working quite well but I can't figure out how to get the rotation right, so that the character is facing towards the mouse.<br />This is the closest I could get so far:<br />

<br />onready var body = get_node("body")<br /><br />func _ready():<br /> set_process_input(true)<br /><br />func input(ie):<br /> if ie.type == InputEvent.MOUSE_MOTION:<br /> if last_position == null:<br /> last_position = ie.pos<br /> var delta = ie.pos - last_position<br /> last_position = ie.pos<br /> body.rotate_y(-delta.x * 0.01)<br />

<br /><br />My node tree looks like:<br />

<br />- KinematicBody<br />  - Camera<br />  - Body<br />

&lt;br /&gt;The script is attached to KinematicBody.&lt;br /&gt;&lt;br /&gt;I read a lot about vectors and trigonometry and tried to adapt a third-person-controller-demo, but I just don't get it right. <img alt="" height="20">&lt;br /&gt;&lt;br /&gt;Hope someone can give me a nudge in the right direction!?

You could use the [tt]look_at()[/tt] function. It takes a target vector, where you can put in the mouse position (from [tt]get_viewport().get_mouse_pos()[/tt]) and a vector which defines the up direction.[b]edit:[/b] I just tested that. the object moves with the mouse, but not very accurate. :-X I should test this before posting next time.

[quote]I just tested that. the object moves with the mouse, but not very accurate. :-X I should test this before posting next time.[/quote]How did you turn the Vector2 from get_mouse_pos() into a Vector3 that can be passed to look_at?I tried to do something like [code]var mouse_pos = Camera.get_viewport().get_mouse_pos()body.look_at(Vector3(mouse_pos.x, 1, mouse_pos.y), Vector3(0,1,0))[/code]and got pretty weired results.I think that is because a mouse position of (0,0) is the top left corner of the screen, but (0,1,0) is the center of the local space of the Spatial that I want to rotate. Maybe there is a way to transform the &quot;point on screen&quot; to &quot;point in local space&quot;!?

I did the same thing except I used a 0 for y. I think you&#039;re right with the mouse position on the screen. That is probably useless, unless you can get the zero position of the screen to the player object.To be honest, I don&#039;t know much about mouse interaction in 3d, but there should be an easy way to do something with look_at, i think. You should take a look at the gui-in-3d demo. I think there is the answer for this problem. The 3d space of the area node is converted to 2d space.

It&#039; works but I&#039;m not sure if I did it the you had in mind:I added a Area to the KinematicBody and made the CollisionShape (added to Area), slightly bigger than the size of the viewport.My node tree now looks like[code]- KinematicBody (script attached)&nbsp; - Camera&nbsp; - Body&nbsp; - Area[/code]The script:[code]extends KinematicBodyonready var body = get_node(&quot;body&quot;)func _on_area_input_event(camera, event, click_pos, click_normal, shape_idx):&nbsp; &nbsp; if event.type == InputEvent.MOUSE_MOTION: click_pos.y = 1 body.look_at(click_pos, Vector3(0,1,0))[/code]

Nice, if it works!&nbsp; :D I couldn&#039;t really get your code to work for me, but I&#039;ve played a little with the navigation mesh demo and found an easy solution. I&#039;ll try to reconstruct that and upload it.

[quote author=del link=topic=15427.msg15890#msg15890 date=1461353025]Nice, if it works!&nbsp; :D I couldn&#039;t really get your code to work for me[/quote] I put together a minimal project, it should just work out of the box:http://pub.atze.libra.uberspace.de/mouse_pos_in_3d.zip[quote]but I&#039;ve played a little with the navigation mesh demo and found an easy solution. I&#039;ll try to reconstruct that and upload it.[/quote]I would really like to see that.Thank you very much for your help.

Nice work! It works really good!I&#039;ve had some trouble with making a the nav mesh, but i think I&#039;ve got it now. will try to make a demo on the weekend.

So here is my [url=https://github.com/delstuff/Demos-Prototypes/archive/master.zip]demo[/url].Basically I parented a Navigation node (with a navmesh) to the Camera (which follows the player) and the player looks at the position, where a ray (cast by the mouse motion) hits the navmesh (the code for this is in the navi.gd).But it seems that somehow the navmesh doesn&#039;t follow the player yet. It works only if you stay on the ground plane.With this method it should also be possible to aim at points that are on a higher or lower position then the player.

Thanks for putting this together. I&#039;m not sure if I&#039;ll make use of it somehow. It seems way too much code and nodes for a simple task like this.Also I didn&#039;t notice any issues with the &quot;put-the-player-in-a-large-Area-that-covers-whole-screen-solution&quot; so far so I&#039;ll stick with it for now and try to get some useful results with GridMap but that&#039;s another topic.

10 months later

func _fixed_process(delta): var body = get_node("body") var m = get_viewport().get_mouse_pos() var s = get_viewport().get_rect().size var body_y = body.get_global_transform().origin.y var center = (m-s/2) body.look_at(Vector3(center.x,body_y,center.y),Vector3(0,1,0)) This is the easiest way i could think of. In total, over the code I spent five hours. Programming is painful. And here I created an example.

6 years later