@Kreshi the interpolation is set to “none”. I also tried every interpolation configuration.
@Digital Ape :
i put a Debug.break() where there is the problem…
the game freezes when my player is in the air, but i don’t know why or what to do with that…
Plus now, i have a problem wit my collider… (i post in another topic for that issue)
here’s my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class Joueur : MonoBehaviour
{
// References
public GameObject player;
public Animator animator;
public Rigidbody2D rigidbody2D;
public Transform groundCheck;
public Transform ceilingCheck;
public LayerMask whatIsGround;
public Collider2D crouchDisableCollider; // When player would crouch, the collider of the top of the body would be disabled.
public Transform NewPosition; // Child to the player, at his feet at idle position / at his hand in last frame of climbing ledge animation
// variables
public float speed = 40.0f;
public float crouchSpeed = 10.0f;
public float verticalJumpForce = 3400.0f;
private float horizontalInput = 0.0f;
const float groundedRadius = .2f; // Radius of the overlap circle to determine if grounded
const float ceilingRadius = .2f; // Radius of the overlap circle to determine if the player can stand up
private float fallingTime = 0f; // used to kill the player if he's falling too long
public float deadlyHeight = 3f; // >the player dies
// bools
private bool facingRight = true;
public bool isGrounded = true;
public bool isWalking = false;
public bool isJumping = false;
public bool isCrouching = false;
public bool canHang = false; // when the player collides a climbable wall, allows to climb
public bool isHanging = false; // when the player is hanging to a ledge
public bool isDead = false;
//
[Header("Events")]
[Space]
public UnityEvent OnLandEvent;
[System.Serializable]
public class BoolEvent : UnityEvent<bool> { }
public BoolEvent OnCrouchEvent;
private bool wasCrouching = false;
//-------------------------------------------------------------------------------------------------//
private void Awake()
{
rigidbody2D = GetComponent<Rigidbody2D>();
if (OnLandEvent == null)
OnLandEvent = new UnityEvent();
if (OnCrouchEvent == null)
OnCrouchEvent = new BoolEvent();
}
//-------------------------------------------------------------------------------------------------//
void Update()
{
if (isGrounded && !isDead)
{
// COMMANDES (Inputs)
// BOUTON AVANCER
horizontalInput = Input.GetAxisRaw("Horizontal") * speed;
if (horizontalInput > 0 || horizontalInput < 0)
{
isWalking = true;
}
else if (horizontalInput == 0)
{
isWalking = false;
}
// BOUTON SE BAISSER
if (Input.GetButtonDown("Crouch"))
{
isCrouching = true;
speed = crouchSpeed;
}
else if (Input.GetButtonUp("Crouch"))
{
isCrouching = false;
ResetSpeed();
}
// BOUTON SAUT
if (Input.GetButtonDown("Jump"))
{
Jump();
}
// S'ACCROCHER A UN REBORD
if (canHang && Input.GetButtonDown("Jump"))
{
Hang();
}
}
if (!isDead)
{
// BOUTON ESCALADER UN REBORD
if (isHanging && Input.GetButtonDown("Up"))
{
Climb();
}
// MORT : CHUTE MORTELLE
//DeadlyFall();
}
}
//-------------------------------------------------------------------------------------------------//
void FixedUpdate() // Moving a Rigidbody only in fixedUpdate
{
// GROUNDED
bool wasGrounded = isGrounded;
isGrounded = false;
// On sait que le joueur touche le sol, "isGrounded", quand le gameObject groundCheck qui lui est parenté au niveau de ses pieds avec un collider, touche tout ce qui est identifié par calque comme étant le sol
Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheck.position, groundedRadius, whatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
isGrounded = true;
if (!wasGrounded)
OnLandEvent.Invoke();
}
}
if (!isDead && isGrounded)
{
// AVANCER
transform.Translate(horizontalInput * Time.deltaTime, 0, 0);
animator.SetFloat("Mouvement", Mathf.Abs(horizontalInput));
// DIRECTION
if (horizontalInput > 0 && !facingRight)
{
Flip();
}
else if (horizontalInput < 0 && facingRight)
{
Flip();
}
// SE BAISSER
if (isCrouching)
{
if (!wasCrouching)
{
wasCrouching = true;
OnCrouchEvent.Invoke(true);
}
// Disabled the collider when crouching
if (crouchDisableCollider != null)
crouchDisableCollider.enabled = false;
}
else
{
// enable the collider when done crouching
if (crouchDisableCollider != null)
crouchDisableCollider.enabled = true;
if (wasCrouching)
{
wasCrouching = false;
OnCrouchEvent.Invoke(false);
}
}
if (!isCrouching)
{
// if there is a ceiling, the player can't get up and stay crouched
if (Physics2D.OverlapCircle(ceilingCheck.position, ceilingRadius, whatIsGround))
{
isCrouching = true;
}
}
}
}
//-------------------------------------------------------------------------------------------------//
void Flip() // Fonction pour se retourner
{
// Switch the way the player is labelled as facing.
facingRight = !facingRight;
// flip the player by *-1
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
//-------------------------------------------------------------------------------------------------//
public void OnLanding() // Landing after a jump
{
isJumping = false;
animator.SetBool("Saute", false);
}
//-------------------------------------------------------------------------------------------------//
public void OnCrouching(bool isCrouching) // stay crouching
{
animator.SetBool("SeBaisse", isCrouching);
}
//-------------------------------------------------------------------------------------------------//
private void ResetSpeed()
{
speed = 40.0f;
}
//-------------------------------------------------------------------------------------------------//
private void Jump()
{
isJumping = true;
rigidbody2D.AddForce(new Vector2(0f, verticalJumpForce)); // insuffle une force verticale au rigidbody du joueur
animator.SetBool("Saute", true);
}
//-------------------------------------------------------------------------------------------------//
private void Hang()
{
isJumping = false;
isHanging = true;
canHang = false;
speed = 0f;
verticalJumpForce = 0f;
animator.SetBool("PeutSaccrocher", true);
Hanged();
}
//-------------------------------------------------------------------------------------------------//
private void Hanged()
{
speed = 0f;
verticalJumpForce = 0f;
animator.SetBool("EstAccroche", true);
}
//-------------------------------------------------------------------------------------------------//
private void Climb()
{
speed = 0f;
verticalJumpForce = 0f;
canHang = false;
isHanging = false;
animator.SetBool("Monte", true);
}
//-------------------------------------------------------------------------------------------------//
private void HasClimbed()
{
Debug.Log("Has Climbed");
animator.SetBool("Monte", false);
animator.SetBool("EstAccroche", false);
animator.SetBool("PeutSaccrocher", false);
player.transform.position = NewPosition.transform.position;
Debug.Break();
canHang = false;
}
//-------------------------------------------------------------------------------------------------//
// COLLISIONS
void OnCollisionEnter2D(Collision2D collision)
{
/// Pour se suspendre aux plateformes
if (collision.gameObject.tag == "MursGrimpables")
{
canHang = true;
animator.SetBool("EnCollisionAvecUnMurGrimpable", true);
//when hitting a wall, player's speed is 0, he stop walk against the wall. but if the wall is climbable, he can climb
speed = 0f;
// after a while his speed get back, he could walk further
Invoke("ResetSpeed", 0.05f);
}
if (collision.gameObject.tag == "Murs")
{
animator.SetBool("EnCollisionAvecUnMur", true);
// the same with a simple wall
speed = 0f;
//
Invoke("ResetSpeed", 0.05f);
}
}
void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.tag == "MursGrimpables")
{
canHang = false;
animator.SetBool("EnCollisionAvecUnMurGrimpable", false);
}
if (collision.gameObject.tag == "Murs")
{
animator.SetBool("EnCollisionAvecUnMur", false);
}
}
void OnCollisionStay2D(Collision2D collision)
{
if (collision.gameObject.tag == "EauMortelle") // player drowns if he touches deadly Water
{
speed = 0f;
isDead = true;
animator.SetBool("MortNoyade", true);
}
if (collision.gameObject.tag == "Sols" && isDead) // player dies if he falls from too high
{
animator.SetBool("ChuteMortelle", true);
}
}
//-------------------------------------------------------------------------------------------------//
// DEATHS
void DeadlyFall()
{
if (isGrounded)
{
fallingTime = 0f;
}
if (!isGrounded)
{
fallingTime += Time.deltaTime;
}
if (fallingTime >= deadlyHeight)
{
isDead = true;
Debug.Log("Chute Mortelle !");
}
}
//-------------------------------------------------------------------------------------------------//
private void Drowning()
{
animator.speed = 0f;
speed = 0f;
isDead = true;
}
//-------------------------------------------------------------------------------------------------//
}