Jump to content

How to check when the character is facing away from camera?


Recommended Posts

I need a code to do an action when the character is facing away from the camera (e.g. when going upwards). This is kind of separate from my other problem with changing z-indexes of layers in my other topic. I have my current solution based on this topic:

 

The current buggy solution is:

In master_postinit:

inst:ListenForEvent("locomote", OnLocomote)

And the function itself:

local function OnLocomote(inst)
    local dir = inst.Transform:GetRotation()
    local camera_rot = TheCamera:GetHeadingTarget()
    local relative_dir = ((dir + camera_rot) % 360 + 360) % 360

    -- detect up to 90 degrees from the top
    if math.abs(relative_dir - 180) < 90 then
        print("facing away from camera")
    else
        print("facing to the camera")
    end
end

There are two problems with that:
1. When character is facing upwards and then doing some animation facing towards the camera. An example is using "examine" on an item - there is no locomote event before the talking animation.
2. This is not a problem for be, but noting this in case somebody needs the exact code. The above only detects when the character is facing more away from the camera than to the camera, so it produces false positives when the character's side is visible. I could adjust the angle to 45, but then I'd face some weird issues with the relative_dir above being like 0.001 degree different than actual, which happens kind of randomly depending on camera rotation, when using W+A or W+D keys. Plus, I can get a lot of false positives when user is using mouse to move the character (when there is an obstacle on the way).

Are there any events or maybe just properties of AnimState that detect if the sprites used are the ones for facing away from the camera?

EDIT: I found out about ThePlayer.AnimState:GetCurrentFacing() == 1 usually when facing upwards, but it is broken when doing emotes/talking. It seems that the character is facing upwards when examining stuff that is above, but the animation is playing with sprites facing the camera.

Edited by Xilexio
Link to comment
Share on other sites

I don't understand your issue. If you want a function to be executed when the character is facing up, the the GetCurrentFacing works. The fact that emotes are playing as if your character is facing down, it doesn't change the fact that you character is effectively facing up.

Then I probably miss what you want to achieve.

Link to comment
Share on other sites

I want to know how to detect if the sprites used are facing the camera. In Extended Sample Character terms, I want to know when headbase-2 is used instead of headbase-0 or headbase-1. Using that information, I can do some hacks with animation that are hard to achieve in the current model (such as showing different beard from behind and from the front).

GetCurrentFacing only says where the character is going/where is the thing the character is observing. But it does not change during emotes whose animation uses front sprites. And there is even a single fish catching animation that uses both front and back sprites, making it even harder to achieve. And I kinda don't want to get updated list of all animations and do a huge if.

Edited by Xilexio
Link to comment
Share on other sites

I understand now. The AnimState has a bunch of function to check the current... animation state :p

I would print the metatable of the AnimState and check out what are the available function, the names are self explanatory.

I should copy/paste that metatable somewhere once to have a copy without having to go ingame to do it :p

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...