I’m trying to recreate a flappybird game and i need help with two things:
First, in this script, when my player is dead, i want the FixedUpdate fonction to stop, so i try using the if(dead) return script, but it stops everything. I would like for my gravity and velocite.x to continue just a little… Here is the script, what should i use and where in the script?
using UnityEngine;
using System.Collections;
public class MouvementJoueur : MonoBehaviour {
Vector3 velocite = Vector3.zero;
public Vector3 gravite;
public Vector3 pousseeVelocite;
public float vitesseMax=5f;
public float vitesseavant =1f;
bool poussee = false;
Animator animator;
bool dead = false;
void Start () {
animator = transform.GetComponent <Animator>();
if(animator==null){
Debug.LogError("Ne trouve pas l'animateur!");
}
}
void Update(){
if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) ) {
poussee = true;
}
}
void FixedUpdate () {
// somewhere in here
// this doesn't work... What should i put instead and where if (dead)return;
velocite += gravite * Time.deltaTime;
velocite.x = vitesseavant;
if(poussee == true) {
poussee = false;
if(velocite.y < 0)
velocite.y = 0;
velocite += pousseeVelocite;
}
velocite = Vector3.ClampMagnitude (velocite, vitesseMax);
transform.position += velocite * Time.deltaTime;
float angle = 0;
if(velocite.y < 0) {
angle = Mathf.Lerp(0,-90, -velocite.y / vitesseMax);
}
transform.rotation = Quaternion.Euler(0,0,angle);
}
void OnCollisionEnter (Collision collision) {
animator.SetTrigger ("Dead");
dead=true;
}
}
Secondly, i want another script, called MouvementCiel, to stop when my player is dead in the MovementJoueur script. Here is the small script, what should i put in to say when the player is dead, stop??
using UnityEngine;
using System.Collections;
public class MouvementCiel : MonoBehaviour {
float speed = 0.9f;
void FixedUpdate() {
Vector3 pos = transform.position;
pos.x += speed *Time.deltaTime;
transform.position = pos;
}
}
create a bool var for playerDead
put a variable on FixedUpdate () to check.
if (!playerDead){
//... do all stuffs while is alive
}
then, when you collide with some enemy or something else, you only change the bool “playerDead” to true and the FixedUpdate code will stop automatically
if you want to keep gravity a bit more (lets say 3 seconds)… then you can add a coroutine:
English follows, but since he is obviously French…
Dans ton MouvementCiel, tu dois acceder a ton MouvementJoueur.
Pour ce faire, Tu peux rajouter une variable
public MouvementJoueur mouvementJoueur;
et l’assigner dans l’inspecteur.
ensuite, dans l’update de ton MouvementCiel tu peux vérifier si mouvementJoueur.estVivant et agir en conséquence (par exemple ne pas effectuer le mouvement si le joueur est mort.
In MouvementCiel, you need to access MouvementJoueur.
To do that you may add a variable
public MouvementJoueur mouvementJoueur;
and assign it in the inspector
Then, in MouvementCiel’s update, you can check for mouvementJoueur.estVivant and act accordingly (for example, not moving the sky when the player is dead).
Merci beaucoup. Dans mon script, j’ai appelé le script MouvementJoueur mais maintenant, je ne sais pas trop comment appelé la portion dead de mon script, pour que mon MouvementCiel arrête. Voir script modifié ci-bas…
using UnityEngine;
using System.Collections;
public class MouvementCiel : MonoBehaviour {
float speed = 0.9f;
GameObject player;
MouvementJoueur mouvementJoueur;
void Awake()
{
player = GameObject.FindGameObjectWithTag ("Player");
mouvementJoueur = player.GetComponent <MouvementJoueur> ();
}
void FixedUpdate() {
Vector3 pos = transform.position;
pos.x += speed *Time.deltaTime;
transform.position = pos;
}
}
very simple, works pretty much the way i’d like… The only thing i’m now unable to work with is my vitesseavant variable. I’d like it to turn to 0 when my dead collision occurs, but it doesn’t work Any idea? I put it in the beginning of the FixedUpdate function.
Merci… Mais j’ai beau essayer de fermer l’accolade n’importe où, ou bien ça ne change rien du tout, ou bien ça change complètement la dynamique de mon player, mais ça ne fait pas arrêter la variable vitesseavant. Il faudrait aussi que je trouve un moyen de terminer la fonction poussee, la ramener à poussee = false.