je travaille depuis peu sur un projet de jeux (donjon-rpg/2D vue du dessus) avec Unity.
J'ai créé un script pour le déplacement de mon perso et j'ai peu apres voulu ajouter la possibilité de faire des "dash" (rapide déplacement sur le coté).
Cependant depuis que j'ai rajouté cette option, mon jeu semble saccader lors des déplacement.
Je n'arrive pas a résoudre ce problème
voici mon script:
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
private Animator anim;
private Rigidbody2D myRigidbody;
private bool playerMoving;
public Vector2 LastMove;
private static bool playerExists;
//dash
private Rigidbody2D rb;
public float dashspeed;
private float dashTime;
public float startDashTime;
private int direction;
private bool DashOn = false;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
myRigidbody = GetComponent<Rigidbody2D>();
if(!playerExists)
{
playerExists = true;
DontDestroyOnLoad(transform.gameObject);
}
else
{
Destroy(gameObject);
}
//dash
dashTime = startDashTime;
}
// Update is called once per frame
void Update () {
playerMoving = false;
if (Input.GetKeyDown(KeyCode.LeftShift))
{
DashOn = true;
}
else if (Input.GetKeyUp(KeyCode.LeftShift))
{
DashOn = false;
}
if ((Input.GetAxisRaw("Horizontal")>0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)&& (DashOn == false))
{
myRigidbody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed,myRigidbody.velocity.y);
playerMoving = true;
LastMove = new Vector2(Input.GetAxisRaw("Horizontal"),0.0f);
}
if ((Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)&& (DashOn == false))
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, Input.GetAxisRaw("Vertical")*moveSpeed);
playerMoving = true;
LastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
}
if((Input.GetAxisRaw("Horizontal") < 0.5f && Input.GetAxisRaw("Horizontal")> -0.5f) && (DashOn == false))
{
myRigidbody.velocity = new Vector2(0f, myRigidbody.velocity.y);
}
if ((Input.GetAxisRaw("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f) && (DashOn == false))
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, 0f);
}
if (DashOn == false)
{
anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
anim.SetBool("PlayerMoving", playerMoving);
anim.SetFloat("LastMoveX", LastMove.x);
anim.SetFloat("LastMoveY", LastMove.y);
}
if ((direction == 0)&&(DashOn==true))
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
direction = 1;
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
direction = 2;
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
direction = 3;
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
direction = 4;
}
}
else
{
if (dashTime <= 0)
{
direction = 0;
dashTime = startDashTime;
myRigidbody.velocity = Vector2.zero;
}else
{
dashTime -= Time.deltaTime;
if (direction == 1)
{
myRigidbody.velocity = Vector2.left * dashspeed;
}
else if (direction == 2)
{
myRigidbody.velocity = Vector2.right * dashspeed;
}
else if (direction == 3)
{
myRigidbody.velocity = Vector2.up * dashspeed;
}
else if (direction == 4)
{
myRigidbody.velocity = Vector2.down * dashspeed;
}
}
}
}
}