Hi, I’m making a 2d game, basically when the enemy hits the player he has to die, standing still and the controls don’t go. I have a problem, I can’t understand why but once the player is dead he slips and continues to slide indefinitely. How can I make him stay still? thank you
If you can help me, I’ll pass the code to you if needed.
[RequireComponent(typeof(Rigidbody2D), typeof (CapsuleCollider2D), typeof(Animator))]
public class PlayerController : MonoBehaviour
{
public static class GlobalVariables{
public static bool isAlive = true;
}
[SerializeField] FixedJoystick _joystick;
[SerializeField] float _moveSpeed;
[SerializeField] Vector2 deathKick = new Vector2(20f, 20f);
Animator anim;
Transform transf;
CapsuleCollider2D myBodyCollider;
Rigidbody2D myRigidbody;
private void Start() {
anim = GetComponent<Animator>();
transf = GetComponent<Transform>();
myBodyCollider = GetComponent<CapsuleCollider2D>();
myRigidbody = GetComponent<Rigidbody2D>();
}
public void Attack(){
anim.SetBool("attack", true);
}
private void FixedUpdate() {
if(!GlobalVariables.isAlive) {return; }
run();
flipAnim();
Die();
}
void run(){
if(!GlobalVariables.isAlive) {return; }
myRigidbody.velocity = new Vector3(_joystick.Horizontal * _moveSpeed * Time.deltaTime, _joystick.Vertical * _moveSpeed * Time.deltaTime, myRigidbody.velocity.y * Time.deltaTime);
}
void flipAnim(){
if(!GlobalVariables.isAlive) {return; }
if(_joystick.Horizontal > 0){
anim.SetBool("run", true);
transf.localScale = new Vector3(_joystick.Horizontal < 0 ? 1 : 0.1751192f, 0.1842967f, 0.2667874f);
} else if(_joystick.Horizontal < 0 ) {
anim.SetBool("run", true);
transf.localScale = new Vector3(_joystick.Horizontal > 0 ? 1 : -0.1751192f, 0.1842967f, 0.2667874f);
} else {
anim.SetBool("run", false);
}
}
void Die(){
if(myBodyCollider.IsTouchingLayers(LayerMask.GetMask("Enemies"))){
GlobalVariables.isAlive = false;
anim.SetTrigger("dead");
myRigidbody.velocity = deathKick;
}
}