Player continues moving after death

37_ma_rpg
rpg

#1

Hi!

If I click on the terrain when my player dies, he begins to move slowly towards that point. How would you solve that @ben? I could get a reference to the AIC character inside Player.cs and change the agent speed to zero (agent.velocity = Vector3.zero;) when my player dies, does that sound good?

Thank you!


#2

Hi, did this approach work for you?

Note that @sampattuzzi is doing a LOT of good work on the RPG codebase right now ready for Part 2 to start shipping early in 2019. This may include refinements to the movement code.


#3

I ended up activating the isKinematic property of the rigid body of the player to avoid movement after death and activate the animation of death … not very elegant. If the problem persists after finishing the course, I will share my solution to this problem to discuss it (if I find one :wink: ).

Thank you!


#4

Another good way is to have the Health component disable the movement component.


#5

Good idea. When I posted this question, I was in the middle of section 3 and did not have a health system component or player movement, but now I will definitely implement it!

Thanks!


#6

Here my final solution (and it works :smile: ):

In character.cs:

void Update() {
            if (navAgent.remainingDistance > navAgent.stoppingDistance && isAlive) {
                Move(navAgent.desiredVelocity);
            }
            else {
                Move(Vector3.zero);
                if (!isAlive)
                    SetDestination(transform.position);
            }
        }

In HealthSystem:

 IEnumerator KillCharacter() {
            characterMovement.Kill();
            var playerComponent = GetComponent<PlayerControl>();

            audioSource.clip = deathSounds[UnityEngine.Random.Range(0, deathSounds.Length)];
            audioSource.Play();
            animator.SetTrigger(AnimationConstants.DEATH_TRIGGER);

            if (playerComponent) {
                //to prevent the player move when is dead and the user clicks on terrain
                FindObjectOfType<CameraRaycaster>().enabled = false;
            }
            else {
                //to prevent the enemy to follow the player while he is dead 
                GetComponent<EnemyAI>().enabled = false;
            }

            yield return new WaitForSecondsRealtime(audioSource.clip.length);

            if (playerComponent && playerComponent.isActiveAndEnabled)
                SceneManager.LoadScene(0);
            else
                Destroy(gameObject, deathVanishSeconds);

        }

Do you like it @sampattuzzi?

Regards!


#7

Careful with not using curly brackets here. Technically fine but you might forget to add them when you add another line.

Good solution.