Godot Version
`4.2.2
Question
i want to know why my code works in one script but not another ive double checked the nodes, input map , and animations but for some reason i cant jump or `play my animations when prompted by an input. just gives my null errors or just doesnt work or crashes.
heres the code in question
@onready var Sprite =$CanvasLayer/charctersprite/AnimatedSprite2D
@onready var Raycast = $RayCast3D
@onready var SpringArmPivot = $SpringArmPivot
@onready var Camera = $SpringArmPivot/Camera3D
const SPEED = 5.0
const JUMP_VELOCITY = 5.0
const MOUSE_SENS = 0.3 # Mouse sensitivity (you can tweak this)
const MINPIN = -80 # Minimum vertical rotation
const MAXPIN = 80 # Maximum vertical rotation
# Variables to store the current rotation angles
var yaw : float = 0
var pitch : float = 0
var dead = false
# Get the gravity from the project settings to be synced with RigidBody nodes
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
func _input(event):
if dead:
return
if event is InputEventMouseMotion:
yaw -= event.relative.x * MOUSE_SENS # Horizontal rotation
pitch -= event.relative.y * MOUSE_SENS # Vertical rotation
# Clamp the pitch to prevent infinite rotation
pitch = clamp(pitch, MINPIN, MAXPIN)
# Apply the rotation to the camera and the character's body
SpringArmPivot.rotation_degrees.x = pitch # Vertical rotation for the camera
rotation_degrees.y = yaw # Horizontal rotation for the character body
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _unhandled_input(event):
if Input.is_action_just_pressed("exit"):
get_tree().quit()
if Input.is_action_just_pressed("restart"):
restart()
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir = Input.get_vector("left", "right", "forward", "back")
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = 0.0
velocity.z = 0.0
if Input.is_action_pressed("click"):
Sprite.play("punch")
move_and_slide()
func restart():
pass