Trying to get Player with Mouse Look

Godot Version

4.1 Veris

Question

I just wanted to make the FPS first person camera with Mouse Look, so I did exact followed this Youtuber’s instructions, but the Godot 4.2 vers alarmed me that some red bar is incorrect. That’s Youtuber’s version has 4.1.1 stable.

elif Input.is_action_pressed(“crouch”) or top_cast.is_colliding():
move_speed = crouch_speed

||head.rotation_degrees.x = look_rot.x |
||rotation_degree.y = look_rot.y|

var target_height : float = crouch_height if not reverse else stand_height

collision_shape.shape.height = target_height
collision_shape.position.y = target_height * 0.5
head.position.y = lerp(head.position.y, target_height - 1, crouch_transition * delta)

I have looked back, everything looks the same as Nagi’s, however, when I pressed play, its either won’t appeared or play the game because of this.

extends CharacterBody3D


@export var speed = 8.0
@export var jump  = 4.5
@export var sensitivity = 1000
@export var accel = 16.0
@export var min_angle = -80
@export var max_angle = 90
@export var crouch_transition = 8.0

@onready var Camerapivot = $Camerapivot

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
var look_rot : Vector2
var stand_height : float

func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _physics_process(delta):
	if not is_on_floor():
		velocity.y -= gravity * delta
	else:
		if Input.is_action_just_pressed("jump"):
			velocity.y = jump
		elif Input.is_action_pressed("crouch") or top_cast.is_colliding():
			move_speed = crouch_speed
			crouch(delta)
		else:
			crouch(delta, true)


	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = jump
		
		
	# 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", "backward")
	var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if direction:
		velocity.x = lerp(velocity.x, direction.x * speed, accel * delta)
		velocity.z = lerp(velocity.z, direction.z * speed, accel * delta)
	else:
		velocity.x = lerp(velocity.x, 0.0, accel * delta)
		velocity.z = lerp(velocity.z, 0.0, accel * delta)

	move_and_slide()
	
	var plat_rot = get_platform_angular_velocity()
	look_rot.y += rad_to_deg(plat_rot.y * delta)
	
	head.rotation_degrees.x = look_rot.x 
	rotation_degree.y = look_rot.y
	
	var platform_angular_velocity = owner.get_platform_anglar_velocity()
	owner.rotate_y(platform_angular_velocity.y * delta)
	
	Camerapivot.rotation_degrees.x = look_rot.x
	rotation_degrees.y = look_rot.y

func _input(event):
	if event is InputEventMouseMotion:
		look_rot.y -= (event.relative.x * sensitivity)
		look_rot.x -= (event.relative.y * sensitivity)
		look_rot.x = clamp(look_rot.x, min_angle, max_angle)
		
func crouch(delta : float, reverse = false):
	var target_height : float = crouch_height if not reverse else stand_height
	
	collision_shape.shape.height = target_height
	collision_shape.position.y = target_height * 0.5
	head.position.y = lerp(head.position.y, target_height - 1, crouch_transition * delta)
	

1 Like

can you post the error so i can help

1 Like

Yes, here.

Godot Engine v4.1.1.stable.official (c) 2007-present Juan Linietsky, Ariel Manzur & Godot Contributors.
  modules/gltf/register_types.cpp:73 - Blend file import is enabled in the project settings, but no Blender path is configured in the editor settings. Blend files will not be imported.
--- Debug adapter server started ---
--- GDScript language server started ---
  res://Script/PlayerwithMouseFreeLook.gd:28 - Parse Error: Identifier "top_cast" not declared in the current scope.
  res://Script/PlayerwithMouseFreeLook.gd:29 - Parse Error: Identifier "move_speed" not declared in the current scope.
  res://Script/PlayerwithMouseFreeLook.gd:29 - Parse Error: Identifier "crouch_speed" not declared in the current scope.
  res://Script/PlayerwithMouseFreeLook.gd:55 - Parse Error: Identifier "head" not declared in the current scope.
  res://Script/PlayerwithMouseFreeLook.gd:56 - Parse Error: Identifier "rotation_degree" not declared in the current scope.
  res://Script/PlayerwithMouseFreeLook.gd:71 - Parse Error: Identifier "crouch_height" not declared in the current scope.
  res://Script/PlayerwithMouseFreeLook.gd:73 - Parse Error: Identifier "collision_shape" not declared in the current scope.
  res://Script/PlayerwithMouseFreeLook.gd:74 - Parse Error: Identifier "collision_shape" not declared in the current scope.
  res://Script/PlayerwithMouseFreeLook.gd:75 - Parse Error: Identifier "head" not declared in the current scope.
  res://Script/PlayerwithMouseFreeLook.gd:75 - Parse Error: Identifier "head" not declared in the current scope.
  modules/navigation/nav_region.cpp:126 - Navigation map synchronization error. Attempted to update a navigation region transform rotated 90 degrees or more away from the current navigation map UP orientation.

1 Like

You can find the code you missed at the GitHub link below. (Which I found in the description of the YouTube video you linked to.) Note that the tutorial code has more var lines in the beginning than yours does. Those lines are important! They tell Godot about all the different variables you’re going to be using in the rest of the script. If you leave them out, then you can get errors like the ones you got here.

2 Likes

Didn’t see that in their description. However, when I did copy from the GitHub link and pasted into Gscript, it still same result… Here are the errors.

E 0:00:01:0436 PlayerwithMouseFreeLook.gd:14 @ _ready(): Node not found: “Head” (relative to “/root/Spatial/Player”).
<C++ Error> Method/function failed. Returning: nullptr
<C++ Source> scene/main/node.cpp:1638 @ get_node()
PlayerwithMouseFreeLook.gd:14 @ _ready()

1 Like

Make sure to follow every step of the tutorial fully. This error is because you haven’t set everything up in the editor’s scene view for your player node yet. You’re going to be adding child nodes to the player that help define its behavior.

2 Likes

After a long 7 minutes of video, I can see what you mean. I’ve finally received the Mouse Look, however, with crouching my player goes through the floor. I’ve looked back at the Gscript and when I move my mouse, it’s rotating my player around the big capsule.

I also wanted my player to crouch for T-bag the enemy or crawl under the obstacle like Nagi has, however, I’ve slowly followed his instruction.

I’ve looked back at Gscript and video. Can’t find the problem with it.

Here is my Gscript below.

extends CharacterBody3D


@export var speed = 8.0
@export var crouch_speed = 4.0
@export var accel = 16.0
@export var jump = 8.0
@export var crouch_height = 2.4
@export var crouch_transition = 8.0
@export var sensitivity = 0.2
@export var min_angle = -80
@export var max_angle = 90

@onready var Camerapivot = $Camerapivot
@onready var collision_shape = $CollisionShape3D
@onready var top_cast = $TopCast

var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
var look_rot : Vector2
var stand_height : float


func _ready():
	stand_height = collision_shape.shape.height
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)


func _physics_process(delta):
	var move_speed = speed
	
	if not is_on_floor():
		velocity.y -= gravity * delta
	else:
		if Input.is_action_just_pressed("jump"):
			velocity.y = jump
		elif Input.is_action_pressed("crouch") or top_cast.is_colliding():
			move_speed = crouch_speed
			crouch(delta)
		else:
			crouch(delta, true)

	var input_dir = Input.get_vector("left", "right", "backward", "forward")
	var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if direction:
		velocity.x = lerp(velocity.x, direction.x * move_speed, accel * delta)
		velocity.z = lerp(velocity.z, direction.z * move_speed, accel * delta)
	else:
		velocity.x = lerp(velocity.x, 0.0, accel * delta)
		velocity.z = lerp(velocity.z, 0.0, accel * delta)

	move_and_slide()
	
	var plat_rot = get_platform_angular_velocity()
	look_rot.y += rad_to_deg(plat_rot.y * delta)
	Camerapivot.rotation_degrees.x = look_rot.x
	rotation_degrees.y = look_rot.y


func _input(event):
	if event is InputEventMouseMotion:
		look_rot.y -= (event.relative.x * sensitivity)
		look_rot.x -= (event.relative.y * sensitivity)
		look_rot.x = clamp(look_rot.x, min_angle, max_angle)


func crouch(delta : float, reverse = false):
	var target_height : float = crouch_height if not reverse else stand_height
	
	collision_shape.shape.height = lerp(collision_shape.shape.height, target_height, crouch_transition * delta)
	collision_shape.position.y = lerp(collision_shape.position.y, target_height * 0.5, crouch_transition * delta)
	Camerapivot.position.y = lerp(Camerapivot.position.y, target_height - 1, crouch_transition * delta)