Godot Version
v4.2.2.stable.official [15073afe3]
Question
I have been trying to make a simple game for a sphere character that rolls but does not jump. It is when I tried to implement camera follow and movement based on the direction the camera is facing that I started having problems. I have two scripts; one is for the Node3D (camera.gd) the camera is the child of (it acts as the anchor point so that the camera rotates in an orbit around the player instead of inside it) and the other is for the players movement (ball.gd). I have attached the scripts and the ball scene structure below.
Currently, there are two aspects which I can’t figure out. The player can move and the camera can move, but after a while, the up down motion of the mouse stops changing the rotation of the camera at this line “anchor.rotate(anchor.get_global_transform().basis.x, deg_to_rad(-event.relative.y * sensitivity))” and the error says (while the game still runs) that the Vector3 for the axis is not normalized. I tried normalizing it, but it messed with the behavior and rotated erratically after that.
The second problem I am having is that I want the ball to rotate relative to the direction that the camera is facing (so the angular velocity for the z is rotating in the direction the camera is facing and the x angular velocity is perpendicular to the cameras facing direction). The camera does not even work how I want yet and that is the main thing but I figured that I would save some time asking this question too because I don’t know what to do about that. I don’t think there is angular velocity on a custom axis (such as the local x axis of the anchor point for the camera) so I don’t know what to do. If anyone wants to help but needs more information or clarification; first of all, sorry. Second of all, just hit me up, I would be happy to try to explain anything you need to know.
Ball/Character scene structure:
Rigidbody (Ball)
| CollisionShape3D
| MeshInstance3D
| Node3D (CameraAnchorPoint)
| | Camera3D
camera.gd:
`extends Node3D
@export var sensitivity = 0.3
@onready var anchor = $“.”
func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func _process(_delta):
if Input.is_action_just_pressed(“Quit”):
get_tree().quit()
func _input(event):
if event is InputEventMouseMotion:
anchor.rotate_y(deg_to_rad(-event.relative.x * sensitivity))
anchor.rotate(anchor.get_global_transform().basis.x, deg_to_rad(-event.relative.y * sensitivity))
anchor.rotation_degrees.x = clamp(anchor.rotation_degrees.x, deg_to_rad(-3605), deg_to_rad(3605))`
ball.gd:
`extends RigidBody3D
@export var rollingForce = 50
@export var opposingForceMult = 2
func _ready():
$CameraAnchorPoint.set_as_top_level(true)
func _physics_process(delta):
var oldCameraPos = $CameraAnchorPoint.global_transform.origin
var ballPos = global_transform.origin
var newCameraPos = lerp(oldCameraPos, ballPos, 0.1)
$CameraAnchorPoint.global_transform.origin = newCameraPos
var forwardMotion = Input.get_axis("Forward", "Backward")
var sidewaysMotion = Input.get_axis("Right", "Left")
var movement = Vector2(rollingForce*delta*forwardMotion, rollingForce*delta*sidewaysMotion)
# Allows for more quick direction changes, increasing control
if (movement.x>0) == (0>angular_velocity.x):
movement.x *= opposingForceMult
if (movement.y>0) == (0>angular_velocity.z):
movement.y *= opposingForceMult
angular_velocity.x += movement.x
angular_velocity.z += movement.y`