mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-07-12 18:43:49 +00:00
fix(*): Ensure that movement vaguely works in the new area and looks vaguely at the ball
This commit is contained in:
@@ -2,4 +2,4 @@ extends Button
|
||||
|
||||
|
||||
func _on_pressed() -> void:
|
||||
get_tree().change_scene_to_file("res://scenes/Game.tscn")
|
||||
get_tree().change_scene_to_file("res://scenes/game.tscn")
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
class_name Ship
|
||||
extends RigidBody3D
|
||||
|
||||
var speed = 10.0
|
||||
|
||||
@onready var camera : Camera3D = get_node("Camera3D")
|
||||
@onready var ball = get_parent().get_node("Ball")
|
||||
|
||||
func _physics_process(_delta):
|
||||
if ball and camera:
|
||||
# Adjust the offset based on your scene scale
|
||||
# Increasing Y component to raise the camera higher above the vehicle
|
||||
var offset = Vector3(0, 2, -2) # Adjust Y to change height
|
||||
|
||||
# Position the camera behind and above the vehicle
|
||||
var behind_vehicle = global_transform.origin - global_transform.basis.z * offset.z
|
||||
behind_vehicle += global_transform.basis.y * offset.y
|
||||
|
||||
camera.global_transform.origin = behind_vehicle
|
||||
camera.look_at(ball.global_transform.origin, Vector3.UP)
|
||||
|
||||
func _integrate_forces(state):
|
||||
# input_vector represents the movement input relative to the vehicle
|
||||
var input_vector = get_input_vector()
|
||||
|
||||
# Scale the input_vector by speed
|
||||
input_vector = input_vector.normalized() * speed
|
||||
|
||||
# Set the linear velocity based on the input
|
||||
state.linear_velocity = input_vector
|
||||
|
||||
# Rotate the ship to face the movement direction
|
||||
if input_vector.length() > 0.1: # Only rotate if there's significant movement
|
||||
var target_direction = input_vector.normalized()
|
||||
# Create a transform that looks in the target direction
|
||||
var look_transform = Transform3D()
|
||||
look_transform.origin = global_transform.origin
|
||||
look_transform = look_transform.looking_at(global_transform.origin + target_direction, Vector3.UP)
|
||||
|
||||
# Apply the rotation to the ship
|
||||
state.transform.basis = look_transform.basis
|
||||
|
||||
func get_input_vector() -> Vector3:
|
||||
var input_vector = Vector3.ZERO
|
||||
|
||||
if camera:
|
||||
var camera_transform = camera.global_transform
|
||||
var forward_direction = -camera_transform.basis.z
|
||||
var right_direction = camera_transform.basis.x
|
||||
var y_movement = input_vector.y
|
||||
|
||||
if Input.is_action_pressed("move_forward"):
|
||||
input_vector += forward_direction
|
||||
if Input.is_action_pressed("move_backward"):
|
||||
input_vector -= forward_direction
|
||||
if Input.is_action_pressed("move_left"):
|
||||
input_vector -= right_direction
|
||||
if Input.is_action_pressed("move_right"):
|
||||
input_vector += right_direction
|
||||
|
||||
# Zero out the Y component to restrict movement to the XZ plane
|
||||
input_vector.y = 0
|
||||
|
||||
# Add back the preserved Y movement
|
||||
input_vector.y = y_movement
|
||||
|
||||
if Input.is_action_pressed("move_up"):
|
||||
input_vector.y += 1
|
||||
if Input.is_action_pressed("move_down"):
|
||||
input_vector.y -= 1
|
||||
|
||||
return input_vector.normalized()
|
||||
else:
|
||||
print("Camera not found")
|
||||
return Vector3.ZERO
|
||||
@@ -0,0 +1 @@
|
||||
uid://dyq1n7q1bqjps
|
||||
Reference in New Issue
Block a user