From 9aeae85b81364535e4244226f17e079bbaba5154 Mon Sep 17 00:00:00 2001 From: Raffael Di Pietro <maxou4170@gmail.com> Date: Wed, 31 Mar 2021 00:40:55 +0200 Subject: [PATCH] CORRECTION DU BUG DE LAG DANS LES DEPLACEMENTS !!(youhouu) --- Assets/Prefabs/GUI/BarreDenature.prefab | 2 +- Assets/Scripts/Player/PlayerMovement.cs | 64 ++++++++++++++----------- 2 files changed, 37 insertions(+), 29 deletions(-) diff --git a/Assets/Prefabs/GUI/BarreDenature.prefab b/Assets/Prefabs/GUI/BarreDenature.prefab index f5f616d..514c4d4 100644 --- a/Assets/Prefabs/GUI/BarreDenature.prefab +++ b/Assets/Prefabs/GUI/BarreDenature.prefab @@ -802,7 +802,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: m_Material: {fileID: 0} - m_Color: {r: 1, g: 1, b: 1, a: 0.44705883} + m_Color: {r: 1, g: 1, b: 1, a: 0.42745098} m_RaycastTarget: 1 m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} m_Maskable: 1 diff --git a/Assets/Scripts/Player/PlayerMovement.cs b/Assets/Scripts/Player/PlayerMovement.cs index da1b0b9..6dcf98a 100644 --- a/Assets/Scripts/Player/PlayerMovement.cs +++ b/Assets/Scripts/Player/PlayerMovement.cs @@ -15,7 +15,7 @@ public class PlayerMovement : MonoBehaviour { public float speed; private Rigidbody2D myRigidbody; - private Vector3 change; + private Vector2 moveDirection; private Animator animator; public PlayerState currentState; public int stage = 0; @@ -39,22 +39,52 @@ public class PlayerMovement : MonoBehaviour // Update is called once per frame void Update() { - - change = Vector3.zero; - change.x = Input.GetAxisRaw("Horizontal"); - change.y = Input.GetAxisRaw("Vertical"); + ProcessInputs(); if(currentState == PlayerState.walk) { UpdateAnimationAndMove(); } - Vector3 pos = Camera.main.WorldToViewportPoint(transform.position); pos.x = Mathf.Clamp01(pos.x); pos.y = Mathf.Clamp01(pos.y); transform.position = Camera.main.ViewportToWorldPoint(pos); + gameIntelligence(); + } + + private void FixedUpdate() + { + moveDirection.Normalize(); + myRigidbody.velocity = new Vector2(moveDirection.x * speed, moveDirection.y * speed); + } + + private void ProcessInputs() + { + float moveX = Input.GetAxisRaw("Horizontal"); + float moveY = Input.GetAxisRaw("Vertical"); + moveDirection = new Vector2(moveX, moveY); + } + + void UpdateAnimationAndMove() + { + if (moveDirection != Vector2.zero) + { + + animator.SetFloat("moveX", moveDirection.x); + animator.SetFloat("moveY", moveDirection.y); + + animator.SetBool("moving", true); + } + else + { + animator.SetBool("moving", false); + } + } + + void gameIntelligence() + { if (this.GetComponent<PlayerNature>().currentNature >= 100 && stage == 0) { GenerateMap.instance.generateBoutique(0, 1, "chaussure"); @@ -96,26 +126,4 @@ public class PlayerMovement : MonoBehaviour stage++; } } - - void MoveCharacter() - { - change.Normalize(); - myRigidbody.MovePosition(transform.position + change * speed * Time.deltaTime); - } - - void UpdateAnimationAndMove() - { - if (change != Vector3.zero) - { - MoveCharacter(); - animator.SetFloat("moveX", change.x); - animator.SetFloat("moveY", change.y); - - animator.SetBool("moving", true); - } - else - { - animator.SetBool("moving", false); - } - } } -- GitLab