My PlayerDetection.gd script isn't working as intended

I reached the Player detection episode, I followed the code Mr.Yan had written, but my camera’s aren’t turning red when the player is inside them. they do turn red, but I have no idea why they’re doing it.

I’ve pasted my Player Detection script placed on my camera arm node below:

extends "res://characters/TemplateCharacter.gd"

const FOV_TOLERANCE = 20
const RED = Color(1,0.25,0.25)
const WHITE = Color(1,1,1)

var Player

func _ready():
	Player = get_node("/root").find_node("Player",true, false)

func _process(delta):
	if player_in_FOV():
		$Torch.color = RED
	else:
		$Torch.color = WHITE
		
func  player_in_FOV():
	var npc_facing_direction = Vector2(1,0).rotated(global_rotation)
	var direction_to_Player = (Player.position - global_position).normalized()
	
	if abs(direction_to_Player.angle_to(npc_facing_direction)) < deg2rad(FOV_TOLERANCE):
		return true
	else:
		return false

I’ve also uploaded a recording of how my camera nodes behave. I have no idea why what’s happening does:

I’m willing to provide more recordings and code to help figure out what the issue is, any help in finding the issue is appreciated.

its possible its not a coding related issue. would you be able to zip the project folder and put online somewhere? dont mind having a look later on if you want.

im on the train heading to work for the next couple of hours and cant get youtube so cant watch whats happening :frowning:

I can definitely do that,

let me know if this link to the file works, and if you’re able to figure out what I did wrong,

Thanks in advance :+1:

ok, so been having a look, and some of the rotations within the camera scene are throwing things out of whack.

firstly, go into the camera scene.
and the top node in there, set that to 0 degrees rotation
image

next, camerabody, set that to 0 degrees rotation
image
then set Torch to 0 degrees rotation
image
also set the torches Y transform from 16 to 0, to get it back straight
image
then set sprite to 90 degrees rotation
image

the next bits going to be a bit tedious im afraid, now that its all zeroed out and all pointing to the right by default. now you should redo the animation keyframes since they are in local space so theyre out of whack now by 90 degrees.

so you could click on each of the keyframe points and take away 90 from each (ie 30 takeaway 90 would be -60 or just type -90 at the end of whats there in the inspector as shown and it will work it out). when you change them, remember to press enter. visualy it wont change until you move the animation scrubber(blue line on animation player.

or redo the animation from scratch, depends what you find easier.

thats sorts it out, as they were all off by 90 degrees due to the visual rotations within the scene. and as default the FOV is taken from 0 degrees which i
s facing right vector2(1,0) , but if its visually off by 90 degrees it will look funny.

that should sort it tho. hope it helps

1 Like

Thank you so much, I changed the rotations of the camera nodes and edited the animations

everything now works as intended, and I wasn’t expecting such a high quality and detailed solution guide including screenshots! :grin: You really went above and beyond when it came to it!

1 Like

No worries. Glad to help. Youre back up and running. Thats the main thing :wink:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms