help in scrips with if return and calling a script in another script

Hello guys,

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;
    }
}

thanks so much for your help

Seb

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:

private IEnumerator DelayPlayerDeath(){

yield return new WaitForSeconds(3f);
playerDead = true;
}

so when you hit something and you are ready to die… you call this coroutine:

void OnCollisionEnter (Collider other){

StartCoroutine (DelayPlayerDeath());

}

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;
    }
}

merci pour votre aide

Dans ton Fixed update:
if(!mouvementJoueur.playerDead)
{
… le reste de ton code ici
}

bizzz… voici mon script… ça me donne une erreur que je n’ai jamais vue,

Mouvementjoueur.dead is inaccessible due to its protection level

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() {

        if (mouvementJoueur.dead)
        {
            return;
        }
        Vector3 pos = transform.position;
        pos.x += speed *Time.deltaTime;
        transform.position = pos;
    }
}

merci +++

Assure toi que dead est public.
si elle est private ou protected tu n’y as pas acces à partir des autres scripts

tout simple… merci, maintenant ça marche. Mais j’ai une erreur récurrente qui revient dans ma console, soit

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 :frowning: Any idea? I put it in the beginning of the FixedUpdate function.

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;

    public bool dead = false;
    Animator animator;


    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 () {

        if (dead)
            vitesseavant += 0f;
            gravite.y += -0.000005f;




        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;
        
    }
}

thanks

Il te manque une accolade ouvrante ({) a la fin de la ligne 33.

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.

Merci vraiment pour votre aide

Sébastien

you’re adding 0, not setting to 0

He means that you’ve not used braces at all for that if, just indentation. Line 35 is always going to run, as it’s not controlled by the if.

if(...)
{
// controlled by if
// controlled by if
}

if(...)
// controlled by if
// not controlled by if