Getting Your Mouse Position in 3D Space! | Lets Learn Godot 3.4!

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 ม.ค. 2025

ความคิดเห็น • 60

  • @thebluetropics
    @thebluetropics 2 ปีที่แล้ว +2

    Impressive explanation! I already know about raycasting from the screen to world space, but there is no such helper function in godot. And you made me understand about this better!

  • @Marbul
    @Marbul ปีที่แล้ว +48

    👍Thank you so much for this. I had so much trouble finding anything explaining how to do this. Your method still works in Godot 4. I only had to change the last line with intersect_ray() because it requires a PhysicsRayQueryParameters3D parameter now -
    _Godot_ _3.4_
    var rayArray = spaceState.instesect_ray(rayOrigin, rayEnd)
    _Godot_ _4_
    var rayArray = spaceState.intersect_ray(PhysicsRayQueryParameters3D.create(rayOrigin, rayEnd))

    • @neil_from_future
      @neil_from_future ปีที่แล้ว +3

      thank you, you came up right after I find the create method.

    • @hawkgamedev
      @hawkgamedev 10 หลายเดือนก่อน +2

      you guys are God sent thanks!

    • @mediocreatbest1269
      @mediocreatbest1269 7 หลายเดือนก่อน

      I love you

  • @FinePointCGI
    @FinePointCGI  2 ปีที่แล้ว +1

    I'm sorry @battikh battikh I think TH-cam ate your comment!

  • @highscore99999
    @highscore99999 2 ปีที่แล้ว +9

    I like that you make tutorials on stuff that no one else has. Even if it's sometimes out of my range, I'm guaranteed to learn something new. Thank you.
    Do you know how to find the mouse position and or get input from the mouse when it is outside the game window?

    • @FinePointCGI
      @FinePointCGI  2 ปีที่แล้ว +4

      That is not possible it's not tracked by Godot at that point so if it's out of the game window it's gone from Godot's world if that makes sense

    • @highscore99999
      @highscore99999 2 ปีที่แล้ว

      @@FinePointCGI Bummer... But, good to know.. I suppose the same is true for screenshots? Can I capture the desktop or windows outside the game window?

    • @FinePointCGI
      @FinePointCGI  2 ปีที่แล้ว

      Yeah you can't as far as I know unless you maximize the window and then somehow do a passthrough.

  • @DestusG
    @DestusG 8 หลายเดือนก่อน +1

    How can we do this without hitting a 3d object in my game

  • @d.steinman
    @d.steinman 9 หลายเดือนก่อน

    Thank you! Unfortunately, I have a problem with this solution. I can't post a link here I think... I've described the problem on stackoverflow, it can be found by `Godot doesn't handle a ray-casting with HTerrain properly` search

  • @Wabiro
    @Wabiro 2 ปีที่แล้ว +3

    Would you mind doing a beginner tutorial when godot 4 comes out

    • @FinePointCGI
      @FinePointCGI  2 ปีที่แล้ว +5

      I plan to! I'm going to (hopefully) start a from scratch series once it gets feature complete and is stable.

    • @Wabiro
      @Wabiro 2 ปีที่แล้ว +1

      @@FinePointCGI Thank you

  • @Wonkabonka
    @Wonkabonka ปีที่แล้ว +1

    awesome tutorial. really helped with my top down. But ive got another problem i just cant figure out and its stressing me out.
    Is there a chance someone could explain to me how i can use this method along with a button to instantiate an object on my mouse position?
    ive got the ray, ive got the button, i just dont get how i can make the button instantiate at the mouse position. help is veeeery appreciated!

  • @mert_2577
    @mert_2577 ปีที่แล้ว +2

    Thank you so much for your tutorial, it was really helpful! I just have a question, is there any way to make the player look at the cursor without having to cast a ray until it collides? For example, in a platformer game ambientated in the void, the player can't look at the mouse when it doesn't collide with a platform and it is in the void (which in this case doesn't have any object with collision whatsoever). Is there any alternative solution?

    • @FinePointCGI
      @FinePointCGI  ปีที่แล้ว +2

      What I would do is wrap the world with a invisible sphere.. I'm not sure if there would be a way around that.

    • @mert_2577
      @mert_2577 ปีที่แล้ว

      @@FinePointCGI You were right! I found on a thread a way to do it:
      Basically you do the same thing, but instead of using the sphere as a collider you use a plane:
      _var dropPlane = Plane(Vector3(0, 1, 0), 0)_
      _var position3D = dropPlane.intersects_ray(_
      _$SpringArm/Camera.project_ray_origin(get_viewport().get_mouse_position()),_
      _$SpringArm/Camera.project_ray_normal(get_viewport().get_mouse_position()))_
      Anyway, thanks again for the help!

    • @FinePointCGI
      @FinePointCGI  ปีที่แล้ว +1

      Oh that makes great sense! No need to use a sphere when you can just use a plane!

  • @ChielusMaximus
    @ChielusMaximus 6 หลายเดือนก่อน

    I swear there used to be a build-in function for this... but I must be losing my mind. Anyway, thanks pointman! I was able to implement this thing after 20 seconds of watching because thats when I paused your vid to type over the function that does all of this.
    I was also going to thank you for uploading this code to github but it seems you've put a version up there explicitly with that bit missing, so uhhhhh yeah.
    EDIT: Found the master branch, my man!

    • @MohannadMEkram
      @MohannadMEkram 6 หลายเดือนก่อน

      Hey Man i need help. everything else is working except for the look_at(ScreenPointToRay(), Vector3.UP)

  • @peadamo
    @peadamo 9 หลายเดือนก่อน

    ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh fucking love you!!!!!!!
    Waste 3 hours trying shit math until I find your video and solve it in five minutes.
    Nice tutorial

  • @gauravprasher7611
    @gauravprasher7611 2 ปีที่แล้ว +1

    Thank you!

    • @FinePointCGI
      @FinePointCGI  2 ปีที่แล้ว

      Hey thanks! Im glad it worked for you!

  • @icomputo
    @icomputo ปีที่แล้ว +3

    Hello, it doesn't work in Godot 4

    • @FinePointCGI
      @FinePointCGI  ปีที่แล้ว +1

      Hey I'm updating this I hope to have it out Monday!

  • @digitalunity
    @digitalunity ปีที่แล้ว

    I tried this today for a 3D cursor, and for some reason it zooms from its position on the ground toward the camera when I do it. Is this caused by multiple collision points or what?

    • @myates9529
      @myates9529 11 หลายเดือนก่อน

      Take the collision off the 3D cursor, or change its layer and add a layer mask to the raycast to the ground.

  • @YouSupPop
    @YouSupPop ปีที่แล้ว

    How can I stop looking at the mouse intensively,

    • @YouSupPop
      @YouSupPop ปีที่แล้ว

      It works but the character flip

  • @karaliseklers2531
    @karaliseklers2531 2 ปีที่แล้ว

    Yes this is genuine hack and works as promised in my case. Thank you very much.

  • @VapidLinus
    @VapidLinus ปีที่แล้ว +1

    For C# it is really unusual that IntersectRay returns an untyped dictionary of values instead of a type-safe object. I love Godot but things like this makes me appreciate Unity's C# API a lot more :/

    • @FinePointCGI
      @FinePointCGI  ปีที่แล้ว +1

      Yeah that's definitely one of the big issues that I have with c# support in Godot. They do a lot of extended classes and a lot of variance which makes things really hard to work with... It's gotten a lot better with .net 6 but I feel like the community may need to ask for better c# support...

    • @melonman1276
      @melonman1276 10 หลายเดือนก่อน

      @@FinePointCGI using Godot;
      using System;
      public class YourScriptName : KinematicBody
      {
      private float gravity = 20;
      private float speed = 10;
      private Camera camera;
      public override void _Ready()
      {
      camera = GetActiveCamera(GetTree().Root) as Camera;
      }
      public override void _Process(float delta)
      {
      Vector3 vector = new Vector3(0, 0, 0);
      if (Input.IsActionPressed("ui_up"))
      vector.z = -1;
      if (Input.IsActionPressed("ui_down"))
      vector.z = 1;
      if (Input.IsActionPressed("ui_left"))
      vector.x = -1;
      if (Input.IsActionPressed("ui_right"))
      vector.x = 1;
      vector = vector.Normalized() * speed;
      vector.y -= gravity;
      MoveAndSlide(vector, Vector3.Up);
      // Rotate the KinematicBody to look at the mouse position
      LookAt(ScreenpointToRay(), Vector3.Up);
      }
      private Camera GetActiveCamera(Node node)
      {
      if (node.IsClass("Camera") && node.IsVisibleInTree())
      return node as Camera;
      foreach (Node child in node.GetChildren())
      {
      Camera camera = GetActiveCamera(child);
      if (camera != null)
      return camera;
      }
      return null;
      }
      private Vector3 ScreenpointToRay()
      {
      PhysicsDirectSpaceState spaceState = GetWorld().DirectSpaceState;
      Vector2 mousePos = GetViewport().GetMousePosition();
      if (camera != null)
      {
      Vector3 rayOrigin = camera.ProjectRayOrigin(mousePos);
      Vector3 rayEnd = rayOrigin + camera.ProjectRayNormal(mousePos) * 2000;
      Godot.Collections.Array rayArray = spaceState.IntersectRay(rayOrigin, rayEnd);
      if (rayArray.Count > 0 && rayArray["collider"] != null)
      return (Vector3)rayArray["position"];
      }
      return Vector3.Zero;
      }
      }
      my camera is not rotation properly with this code can you tell me the issue

  • @zooby123
    @zooby123 2 ปีที่แล้ว

    Hey can you help me !
    Question: how to refill life count in android while game is not open? Is there any background timer in godot which runs after the game is quit ?
    Thanks

    • @FinePointCGI
      @FinePointCGI  2 ปีที่แล้ว +2

      There isn't a timer however you can get system time and write it to a file and then check that time on load to see if enough time has passed and then refill their lives.

    • @mohammedaslam9175
      @mohammedaslam9175 2 ปีที่แล้ว

      @@FinePointCGI Thank you!!!

    • @mohammedaslam9175
      @mohammedaslam9175 2 ปีที่แล้ว

      That was helpful for me!

    • @mohammedakram5954
      @mohammedakram5954 2 ปีที่แล้ว

      @@FinePointCGI can you share me the code??

    • @FinePointCGI
      @FinePointCGI  2 ปีที่แล้ว

      My code for all of my projects are on GitHub github.com/finepointcgi/Point-to-ray-Tutorial/tree/master

  • @playstuff5782
    @playstuff5782 ปีที่แล้ว

    Any chance we can get an updated version of this for C#?

    • @melonman1276
      @melonman1276 10 หลายเดือนก่อน

      here:
      using Godot;
      using System;
      public class YourScriptName : KinematicBody
      {
      private float gravity = 20;
      private float speed = 10;
      private Camera camera;
      public override void _Ready()
      {
      camera = GetActiveCamera(GetTree().Root) as Camera;
      }
      public override void _Process(float delta)
      {
      Vector3 vector = new Vector3(0, 0, 0);
      if (Input.IsActionPressed("ui_up"))
      vector.z = -1;
      if (Input.IsActionPressed("ui_down"))
      vector.z = 1;
      if (Input.IsActionPressed("ui_left"))
      vector.x = -1;
      if (Input.IsActionPressed("ui_right"))
      vector.x = 1;
      vector = vector.Normalized() * speed;
      vector.y -= gravity;
      MoveAndSlide(vector, Vector3.Up);
      // Rotate the KinematicBody to look at the mouse position
      LookAt(ScreenpointToRay(), Vector3.Up);
      }
      private Camera GetActiveCamera(Node node)
      {
      if (node.IsClass("Camera") && node.IsVisibleInTree())
      return node as Camera;
      foreach (Node child in node.GetChildren())
      {
      Camera camera = GetActiveCamera(child);
      if (camera != null)
      return camera;
      }
      return null;
      }
      private Vector3 ScreenpointToRay()
      {
      PhysicsDirectSpaceState spaceState = GetWorld().DirectSpaceState;
      Vector2 mousePos = GetViewport().GetMousePosition();
      if (camera != null)
      {
      Vector3 rayOrigin = camera.ProjectRayOrigin(mousePos);
      Vector3 rayEnd = rayOrigin + camera.ProjectRayNormal(mousePos) * 2000;
      Godot.Collections.Array rayArray = spaceState.IntersectRay(rayOrigin, rayEnd);
      if (rayArray.Count > 0 && rayArray["collider"] != null)
      return (Vector3)rayArray["position"];
      }
      return Vector3.Zero;
      }
      }

  • @zoltanfurko8778
    @zoltanfurko8778 2 ปีที่แล้ว

    Hello, This is a great video again. I would suggest you a new video content: integrate GameMonetize SDK to Godot. It would be helpful for online game developer.

    • @FinePointCGI
      @FinePointCGI  2 ปีที่แล้ว +1

      I have added it to my trello list!

  • @melonman1276
    @melonman1276 10 หลายเดือนก่อน

    not working properly for me

    • @FinePointCGI
      @FinePointCGI  10 หลายเดือนก่อน

      What's it doing? What version of godot do you have

    • @melonman1276
      @melonman1276 10 หลายเดือนก่อน

      @@FinePointCGI 3.4 and here is my code:
      extends KinematicBody
      var gravity = 20
      var speed = 10
      var camera : Camera
      func _ready():
      camera = get_active_camera(get_tree().root) as Camera
      pass
      func _process(delta):
      var vector = Vector3(0, 0, 0)
      if Input.is_action_pressed("ui_up"):
      vector.z = -1
      if Input.is_action_pressed("ui_down"):
      vector.z = 1
      if Input.is_action_pressed("ui_left"):
      vector.x = -1
      if Input.is_action_pressed("ui_right"):
      vector.x = 1

      vector = vector.normalized() * speed
      vector.y -= gravity
      move_and_slide(vector, Vector3.UP)
      # Rotate the KinematicBody to look at the mouse position
      #look_at(ScreenpointtoRay(), Vector3.UP)
      func get_active_camera(node: Node) -> Camera:
      if node.is_class("Camera") and node.is_visible_in_tree():
      return node as Camera
      for child in node.get_children():
      var camera = get_active_camera(child)
      if camera != null:
      return camera
      return null
      func ScreenpointtoRay() -> Vector3:
      var spaceState = get_world().direct_space_state
      var mousePos = get_viewport().get_mouse_position()
      if camera != null:
      var rayOrigin = camera.project_ray_origin(mousePos)
      var rayEnd = rayOrigin + camera.project_ray_normal(mousePos) * 200
      var rayArray = spaceState.intersect_ray(rayOrigin, rayEnd)

      if rayArray.size() > 0 and rayArray["collider"] != null:
      return rayArray["position"]

      return Vector3.ZERO

  • @buhal5181
    @buhal5181 ปีที่แล้ว

    love you

  • @bernyghaor8734
    @bernyghaor8734 2 ปีที่แล้ว

    ᵖʳᵒᵐᵒˢᵐ

  • @MustangPanda
    @MustangPanda 8 หลายเดือนก่อน

    Tested the code and 99% of the time the ray does not hit anything, might want to cover other requirements on what the ray needs.

  • @noneofyourbuisness2539
    @noneofyourbuisness2539 7 หลายเดือนก่อน +1

    pleaseDontUseCamelCaseInGDScriptItsSuperAwful

    • @FinePointCGI
      @FinePointCGI  7 หลายเดือนก่อน +1

      I_dont_use_snake_case_it_is_super_awful_to_type.
      Though it is pretty good for readability

  • @melonman1276
    @melonman1276 10 หลายเดือนก่อน +1

    Your explanation is lackluster you keep typing code but don't explain everything that you are doing in code

    • @JudgeConviction
      @JudgeConviction 5 หลายเดือนก่อน

      not all tutorials are made for people who dont know how to code. These tutorials are made for people who know how to code and need references for techniques within a certain context.

  • @BrickWallYT
    @BrickWallYT 2 ปีที่แล้ว

    Thank you!