torso turning and ray casting

This commit is contained in:
Leon Palluch 2020-02-01 12:07:46 +01:00
parent 9d0bf18030
commit 3235ac5a23
2 changed files with 44 additions and 32 deletions

View File

@ -5,6 +5,10 @@
[node name="Body Builder Menu" type="Spatial"] [node name="Body Builder Menu" type="Spatial"]
script = ExtResource( 2 ) script = ExtResource( 2 )
torsoPath = NodePath("Torso")
cameraPath = NodePath("GUI/HBoxC/ViewportContainer/Viewport/Camera")
rayCastPath = NodePath("GUI/HBoxC/ViewportContainer/Viewport/MouseLook")
viewPortPath = NodePath("GUI/HBoxC/ViewportContainer/Viewport")
[node name="GUI" type="MarginContainer" parent="."] [node name="GUI" type="MarginContainer" parent="."]
anchor_right = 1.0 anchor_right = 1.0
@ -66,4 +70,7 @@ render_target_update_mode = 3
[node name="Camera" type="Camera" parent="GUI/HBoxC/ViewportContainer/Viewport"] [node name="Camera" type="Camera" parent="GUI/HBoxC/ViewportContainer/Viewport"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 3 ) transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 3 )
[node name="MouseLook" type="RayCast" parent="GUI/HBoxC/ViewportContainer/Viewport"]
enabled = true
[node name="Torso" parent="." instance=ExtResource( 1 )] [node name="Torso" parent="." instance=ExtResource( 1 )]

View File

@ -1,20 +1,33 @@
extends Spatial extends Spatial
export(NodePath) var torsoPath
export(NodePath) var cameraPath
export(NodePath) var rayCastPath
export(NodePath) var viewPortPath
const ROT_MOD = 500 const ROT_MOD = 500
const ROT_DECLINE = 0.1 const ROT_DECLINE = 0.1
var _viewRot = false var _viewRot = false
var _prev_mouse_pos var _prev_mouse_pos
var _camera : Camera
var _rayCast : RayCast
var _torso : RigidBody
var _viewport : Viewport
var _velx = 0 var _velx = 0
var _vely = 0 var _vely = 0
var _torso : RigidBody
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
PhysicsServer.area_set_param(get_viewport().find_world().get_space(), PhysicsServer.AREA_PARAM_GRAVITY, 0) PhysicsServer.area_set_param(get_viewport().find_world().get_space(), PhysicsServer.AREA_PARAM_GRAVITY, 0)
_torso = get_node("Torso") _torso = get_node(torsoPath) as RigidBody
_camera = get_node(cameraPath) as Camera
_rayCast = get_node(rayCastPath) as RayCast
_viewport = get_node(viewPortPath) as Viewport
func _process(delta): func _process(delta):
if _viewRot: if _viewRot:
@ -40,32 +53,24 @@ func _process(delta):
_vely *= decline _vely *= decline
func _physics_process(delta): func _physics_process(delta):
pass if _viewRot:
# Get the camera var mouse_pos = _viewport.get_viewport().get_mouse_position()
#var camera = get_node("GUI/HBoxC/ViewportContainer/Viewport/Camera")
#var mouse_pos = get_viewport().get_mouse_position() #set origin of rayCast
var orig = _camera.project_ray_origin(mouse_pos)
# Project mouse into a 3D ray #set cast_to of rayCast
#var ray_origin = camera.project_ray_origin(mouse_pos) _rayCast.translation = orig
#var ray_direction = camera.project_ray_normal(mouse_pos) _rayCast.cast_to = orig + _camera.project_ray_normal(mouse_pos) * 1000.0
# Cast a ray if _rayCast.is_colliding():
#var from = ray_origin # collider will be the node hit
#var to = ray_origin + ray_direction * 1000.0 print(_rayCast.get_collider())
#var space_state = get_world().get_direct_space_state()
#var hit = space_state.intersect_ray(from, to)
#print("test")
#if hit.size() != 0:
# collider will be the node you hit
#print(hit.collider)
#pass
func _input(event): func _input(event):
if _viewport.get_viewport().get_mouse_position().x >= 0:
if event is InputEventMouseButton: if event is InputEventMouseButton:
if event.pressed: if event.pressed:
print("Mouse Click at: ", event.position) print("Mouse Click at: ", event.position)