More States

Make the collider deeper than the water

It’s too much information. I’m going to change this to a talk, but I’m sorry, I’m not going any further with this.

no worries, can I still ask about other states though? Not swimming or diving :slight_smile: - and not right now either

States covered in the tutorial? Yes. New States? No. At least not till the tutorial is finished.

fair enough :slight_smile:

[I’m just documenting my progress, feel free to ignore it - but I do have an important theoretical question in the end (last paragraph :slight_smile:)]

believe it or not, I got swimming, diving and reviving to work. I had a major issue with diving animations (which I fixed with a couple loose ends, definitely not a robust solution!), and for reviving my player wasn’t reviving his way out of water… HE WAS REVIVING HIS WAY TO SPACE (I fixed that by adding an invisible box collider (trigger is deactivated) that only activates when the reviving state is true, mixed with a raycast that keeps searching for anything above the players’ head. Because this is accessed only from the swimming state, it won’t be troubling anyone anytime soon). Reviving was my solution to my problem that he kept swimming his way down the water, and it works for what it was supposed to do :slight_smile:

Anyway, it all works beautifully now, with a third challenge on the way:

Because of the small size of the box collider above the players’ head that triggers the entire system, when I jump into the water from some height, the water collider, regardless of how big it is, completely misses the fact that we just dived into the water, so he plays the freelook state in the water, and now I need to think of how will I implement this. Once I got an idea, I’ll implement it too :stuck_out_tongue:

P.S: This is all done in ‘PlayerSwimmingState.cs’

I do have a question though, why is it that sometimes when we call “CrossFadeInFixedTime()” or “SetFloat()” to call animations, some of the animations just play for one frame and refuse to act properly? I learned this the hard way today :sweat_smile:

Edit: I fixed the “Jumping to water” issue as well (albeit using Raycasts being cast like crazy out of the ‘WaterFinder’ box collider was not the greatest idea, performance-wise)

Do you have it marked as looping? Is it in a state that is switched out of rapidly? Off the top of my head those are the most likely.

Yup

it takes ‘CrossFadeDuration’ (0.2s) seconds to switch between them. Anyway, I’m not touching the animation code again, let it work for now… this was a severe headache for me to get it to act right

I’m low-key excited though. I can’t believe I actually pulled the swimming state off, this is a whole other dimension in the game (although it will need a bit of smoothening out (switching from jumping to swimming for example is snappy… this one can have a bit of hydrodynamic physics for example, where you slowly fall into the water and then play the state there) in terms of transitions and what not, but for now… I’m just glad it works. This one was a miracle that was pulled off)

He can swim, he can dive, he can revive from underwater, and he can also jump into water (from the land. I’m not adding a Jump state from the water) and actually identify that it’s water, and swap to the swimming state :slight_smile: - I’ll call it a day for my states today, this one has been in constant work for a few days now… and somewhere down the line, I messed with my Jumping state. Time to go investigate and fix this…

Something to keep in mind regarding jumping into water, because this one took me a while to figure it out

When you’re jumping into water, you are NOT going from jumping state to freefall state. Do that, and you’ll unlock the bug of having infinite jumps available (trust me, I tried)

Instead, you are going from FREEFALL State (you are freefalling, remember…?!) to detecting water, and this is done by calling events that subscribe to your ‘WaterFinder’ (that event will switch from freefall to Swimming State in my case) in ‘Enter()’, and then unsubscribing from that event in exit

So ‘PlayerFreeFallState.cs’ now looks like this:

using RPG.States.Player;
using UnityEngine;

public class PlayerFallingState : PlayerBaseState
{
    private readonly int FallHash = Animator.StringToHash("Fall");
    private Vector3 momentum;

    public PlayerFallingState(PlayerStateMachine stateMachine) : base(stateMachine) {}

    public override void Enter()
    {
        Debug.Log("Player Falling State Entered");
        momentum = stateMachine.CharacterController.velocity;
        momentum.y = 0;
        stateMachine.Animator.CrossFadeInFixedTime(FallHash, stateMachine.CrossFadeDuration);

        // if you found a water target when free-falling, prepare to swap to Swimming state:
        stateMachine.WaterFinder.OnTargetAdded += InputReader_HandleSwimmingEvent;
    }

    public override void Tick(float deltaTime)
    {
        Move(momentum, deltaTime);
        
        if (stateMachine.CharacterController.isGrounded) SetLocomotionState();
        // FaceTarget(momentum, deltaTime);
    }

    public override void Exit() 
    {
        // if you found a water target when free-falling, immediately exit this function and swap to Swimming state:
        stateMachine.WaterFinder.OnTargetAdded -= InputReader_HandleSwimmingEvent;
    }

    private void InputReader_HandleSwimmingEvent(Water water)
    {
        stateMachine.SwitchState(new PlayerSwimmingState(stateMachine));
    }
}

I don’t know what’s up with events, but this is what worked for me personally. At request, I’m happy to share my code with anyone interested, as a way of saying thank you to this amazing community for standing by my side this far :slight_smile:

This whole system was a nightmare to build (for me personally at least)…

CrossFadeInFixedTime() changes the AnimationState. The float selects an animation within the State’s Blend Tree.

Sounds like you may have some animations set not to loop when they need to be looping.

Good day to you too kind sir :slight_smile: - all my animations are set to loop on the blend tree, and as far as I understood from my own try and error, CrossFadeInFixedTime() is only called ONCE in any state we transition to, and then it’s all about setting the float of the blend tree based on the state. I threw my entire swimming system into a single 1D Blend Tree (Strafing was handled by code, so I found no reason to try integrate that), and then kept calling them based on the threshold set by ‘Animator.SetFloat()’

However, some of them wouldn’t work unless we square the values for some reason… the stuff you see when you leave Bahaa alone with his project :stuck_out_tongue:

ANYWAY, another day another system to try develop

hello again @Brian_Trotter - short question on the fly:

How do you disable and re-enable the player state machine (player controller basically) through code when you’re in another state? (I’m trying to get my character to drive a ship/boat)

Edit: Figured it out, and developed an entirely brand new controller for my boat as I was going through it. Currently working on a solution to get the boat to be careful of terrain. It currently just goes through it like it doesn’t even exist, and I’m confused as of why…

If you are “in another state” then you need to have the state machine enabled. (Without the State Machine, you have no states at all.) If you are asking about the player controls, try adding a new controller map for the ShipDrivingState. I believe you can have multiple controller maps without any difficulty.

the way my horse riding system works, is that I literally deactivate my state machine entirely, with any connections it has, and re-enable it when I get off the horse. I believe the ship driving state should be similar… the problem is, I don’t even know how to disable it through code, mainly because Malbers took care of that for me back then

Oh well, another day, another “Bahaa will eventually figure it out” state we’re in :stuck_out_tongue:

I’ll see what I can come up with (still trying to figure out how in the world to change my camera to a new one, let alone controlling the boat. All I have so far, is a working rangeFinder, which successfully enters the ‘PlayerBoatDrivingState.cs’)

This one is a challenge though, mainly because there’s camera switching, transforming the player to the boat driving position, controlling the boat, getting him off the boat to a spot on a nearby shore, etc… I completely misjudged this one

[/quote]
the way my horse riding system works, is that I literally deactivate my state machine entirely, with any connections it has, and re-enable it when I get off the horse. I believe the ship driving state should be similar… the problem is, I don’t even know how to disable it through code, mainly because Malbers took care of that for me back then
[/quote]

If you have it working for riding then study what Malbers did. The answer is right in front of you. Learn from what you have been given.

lol I’ve been trying for 6 hours now to get the player to stand on a ship with a terrible mesh collider… :stuck_out_tongue: (almost there though, I’m just dealing with a few oscillating ship issues, but boy oh boy was my approach a hilarious one… I literally have an invisible plane sitting on the ship, and that’s the target of a Raycast striking downwards for the player to stand on. That’s my solution for the Y-axis. For the X and Z-axis, I’m aiming for the center of the boat box collider trigger, as a teleportation spot, with a bit of an offset (as the center of the ship is a pole))

I am yet to develop a solution for dismounting and finding an area of ground to safely land the player on. No clue how, yet to think about it

I probably won’t rely much on Malbers though. His approach is not using a State Machine, so I’m literally using my own knowledge to code my own systems right now. If it’s not Brian, Bixarrio or anyone who understands how our systems work, I’d rather not drag them into this :slight_smile: - it’ll be a huge time waste on the short term, and it’s not their job to understand our system anyway

Each asset I spend money on has a specific purpose, that’s it.

Malbers? Boss fights and mounting animals
PolarInt? Construction
etc…

you get the idea (I’m just developing the easy ones now so that when Brian is done with the tutorial, I can ask my hard questions (if I still got any by then)), the stuff I probably can’t figure out alone

ANYWAY, The best approach I personally found is when driving the boat, turn off the state machine and reactivate it when you exit the state. Works well for me :slight_smile: (ironically enough, nothing is in tick for that function, but start and exit are… well… FULL!)

Edit: 1 Day Later - Boat is done, it detects Terrain to avoid collisions (Raycasts), and you’re not allowed to mount a boat with an animal nearby (LayerMask detection - prevents a weird glitch of mounting both the horse and the boat simultaneously. You can guess how disastrous this can get). Off to the next problem :slight_smile:

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

Privacy & Terms