J'ai créer en tant que débutante un petit jeu en 2d pixel art.
Dans celui-ci, j'aimerais que, si le joueur active un boxCollider2D en bas d'un escalier, le scripte le déplace au dessus de l'escalier en passant par 3 waypoints (c'est un escalier anguleux) pendant que le niveau au dessus s'affiche. une fois arrivé au dessus, j'aimerais que le joueur retrouve ses contrôles.
la collision est détectée
l’animation d'affichage du niveau fonctionne
mais je n'arrive pas à déplacer le joueur de waypoint en waypoint.
Pour simplifier et faire des tests, j'ai mis dans le script de mon personnage une variable bool "testmove" pour activé ou désactivé le déplacement scripter.
voici le script :
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PoupouleControle : MonoBehaviour
{
public float speed = 3.0f;
public int maxHealth = 6;
public GameObject mottePrefab;
public int health { get { return currentHealth; }}
int currentHealth;
public float timeInvincible = 2.0f;
bool isInvincible;
float invincibleTimer;
Rigidbody2D rigidbody2d;
float horizontal;
float vertical;
Animator animator;
Vector2 lookDirection = new Vector2(1,0);
public BoxCollider2D playerCollider;
public bool morte = false;
float timerGameOver = 3.0f;
bool tempo = false;
public AudioSource audiosource;
public AudioClip hitSound;
public AudioClip MotteSound;
//c'est ici que je déclare les variable de mon test
public Transform[] waypoints;
private Transform target;
private int destPoint = 0;
public bool testmove = false;
public static PoupouleControle Instance;
private void Awake()
{
if (Instance != null)
{
Debug.LogWarning("Il y a déjà une instance de PoupouleControle dans la scène");
return;
}
Instance = this;
}
void Start()
{
rigidbody2d = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
audiosource = GetComponent<AudioSource>();
currentHealth = maxHealth;
target = waypoints[0];
}
void Update()
{
if (morte)
{
return;
}
//c'est ici que commence le mouvement scripter
if (testmove)
{
Vector3 test = target.position - transform.position;
transform.Translate(test.normalized * speed * Time.deltaTime, Space.World);
// si le joueur s'approche très près du 1er waypoint, le scripte passe au suivant.
if (Vector3.Distance(transform.position, target.position) < 0.3f)
{
destPoint = (destPoint + 1) % waypoints.Length;
target = waypoints[destPoint];
}
// ici je fait en sorte que le perso regarde dans la bonne direction pendant le mouvement scripter
if (!Mathf.Approximately(test.x, 0.0f) || !Mathf.Approximately(test.y, 0.0f))
{
lookDirection.Set(test.x, test.y);
lookDirection.Normalize();
}
animator.SetFloat("Look X", lookDirection.x);
animator.SetFloat("Look Y", lookDirection.y);
animator.SetFloat("Speed", test.magnitude);
if (test.magnitude > 0.1f)
{
if (!audiosource.isPlaying)
{
audiosource.Play();
}
}
else
{
audiosource.Stop();
}
}
//ici se sont les mouvements contrôlé par le joueur
else
{
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
Vector2 move = new Vector2(horizontal, vertical);
if (!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f))
{
lookDirection.Set(move.x, move.y);
lookDirection.Normalize();
}
animator.SetFloat("Look X", lookDirection.x);
animator.SetFloat("Look Y", lookDirection.y);
animator.SetFloat("Speed", move.magnitude);
if (move.magnitude > 0.1f)
{
if (!audiosource.isPlaying)
{
audiosource.Play();
}
}
else
{
audiosource.Stop();
}
}
if (isInvincible)
{
invincibleTimer -= Time.deltaTime;
if (invincibleTimer < 0)
isInvincible = false;
}
if(Input.GetKeyDown(KeyCode.C))
{
Lancer();
}
if (tempo)
{
timerGameOver -= Time.deltaTime;
if(timerGameOver < 0)
{
GameOverManager.Instance.JoueurMort();
tempo= false;
}
}
}
void FixedUpdate()
{
if(!testmove)
{
Vector2 position = rigidbody2d.position;
position.x = position.x + speed * horizontal * Time.deltaTime;
position.y = position.y + speed * vertical * Time.deltaTime;
rigidbody2d.MovePosition(position);
}
}
public void ChangeHealth(int amount)
{
if (amount < 0)
{
if (isInvincible)
{
return;
}
currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
barredesanté.instance.SetValue(currentHealth / (float)maxHealth);
Debug.Log(currentHealth + "/" + maxHealth);
if (currentHealth <= 0)
{
mort();
return;
}
AudioManager.Instance.PlayClipAt(hitSound, transform.position);
animator.SetTrigger("Hit");
isInvincible = true;
invincibleTimer = timeInvincible;
}
else
{
currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
barredesanté.instance.SetValue(currentHealth / (float)maxHealth);
Debug.Log(currentHealth + "/" + maxHealth);
}
}
public void mort()
{
morte = true;
animator.SetTrigger("mort");
RigidbodyType2D bodyType = RigidbodyType2D.Kinematic;
playerCollider.enabled = false;
tempo = true;
}
public void respawn()
{
morte = false;
animator.SetTrigger("respawn");
RigidbodyType2D bodyType = RigidbodyType2D.Dynamic;
playerCollider.enabled = true;
currentHealth = maxHealth;
barredesanté.instance.SetValue(currentHealth / (float)maxHealth);
}
void Lancer()
{
animator.SetTrigger("Lancer");
AudioManager.Instance.PlayClipAt(MotteSound, transform.position);
GameObject motteObject = Instantiate(mottePrefab, rigidbody2d.position + Vector2.up * 0.1f, Quaternion.identity);
motte motte = motteObject.GetComponent<motte>();
motte.Lancer(lookDirection, 300);
}
//Debug.Log("ok");
}
les losanges mauves sont les waypoints de test, ils sont nommés testchekpoint, testchekpoint(1) et testchekpoint(2)
(oui la perso s'appelle poupoule, oui il y a du fran-glais à foison, oui il y a aussi pas mal de fantaisie dans le nom des variables. Me juger pas svp ^^' )
Pour l'instant que je démarre le jeu tout va bien, le joueur peut contrôlé le perso.
quant je coche la variable "testmove" dans l'inspecteur, le mouvement scripter s’enclenche , le perso regarde dans la bonne direction et se dirige ver le 1er checkpoint , le dépasse et continue.
normalement le script est censé passer au waypoint suivant mais il ne le fait pas.
si je décoche "testmove" le joueur ne récupère pas le contrôle de ses mouvements.
Je suis consciente d'être une débutante, et que je devrais trouver la solution par moi même mais après avoir passer tout mon dimanche dessus ... j'espère que vous pourriez m'apporter un peu d'aide.
merci d'avoir lu mon appel
et merci d'avance pour l'aide que vous allez m'apporter