Search Unity

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

Discussion in 'Scripting' started by westergard, Dec 2, 2016.

  1. westergard

    westergard

    Joined:
    May 4, 2015
    Posts:
    91
    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?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouvementJoueur : MonoBehaviour {
    5.  
    6.     Vector3 velocite = Vector3.zero;
    7.     public Vector3 gravite;
    8.     public Vector3 pousseeVelocite;
    9.     public float vitesseMax=5f;
    10.     public float vitesseavant =1f;
    11.     bool poussee = false;
    12.  
    13.  
    14.     Animator animator;
    15.     bool dead = false;
    16.  
    17.     void Start () {
    18.         animator = transform.GetComponent <Animator>();
    19.     if(animator==null){
    20.         Debug.LogError("Ne trouve pas l'animateur!");
    21.  
    22.         }
    23.     }
    24.        
    25.     void Update(){
    26.         if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) ) {
    27.             poussee = true;
    28.         }
    29.     }
    30.  
    31.     void FixedUpdate () {
    32.         // somewhere in here
    33.         // this doesn't work...  What should i put instead and where        if (dead)return;
    34.  
    35.  
    36.         velocite += gravite * Time.deltaTime;
    37.         velocite.x = vitesseavant;
    38.         if(poussee == true) {
    39.             poussee = false;
    40.             if(velocite.y < 0)
    41.                 velocite.y = 0;
    42.             velocite += pousseeVelocite;
    43.         }
    44.  
    45.         velocite = Vector3.ClampMagnitude (velocite, vitesseMax);
    46.         transform.position += velocite * Time.deltaTime;
    47.  
    48.         float angle = 0;
    49.         if(velocite.y < 0) {
    50.             angle = Mathf.Lerp(0,-90, -velocite.y / vitesseMax);
    51.         }
    52.  
    53.         transform.rotation = Quaternion.Euler(0,0,angle);
    54.     }
    55.  
    56.     void OnCollisionEnter (Collision collision) {
    57.         animator.SetTrigger ("Dead");
    58.         dead=true;
    59.        
    60.     }
    61. }
    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??

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouvementCiel : MonoBehaviour {
    5.  
    6.     float speed = 0.9f;
    7.  
    8.     void FixedUpdate() {
    9.         Vector3 pos = transform.position;
    10.         pos.x += speed *Time.deltaTime;
    11.         transform.position = pos;
    12.     }
    13. }    
    thanks so much for your help

    Seb
     
  2. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    create a bool var for playerDead
    put a variable on FixedUpdate () to check.

    Code (CSharp):
    1. if (!playerDead){
    2.     //... do all stuffs while is alive
    3. }
    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:

    Code (CSharp):
    1. private IEnumerator DelayPlayerDeath(){
    2.  
    3. yield return new WaitForSeconds(3f);
    4. playerDead = true;
    5. }
    6.  
    so when you hit something and you are ready to die... you call this coroutine:

    Code (CSharp):
    1. void OnCollisionEnter (Collider other){
    2.  
    3. StartCoroutine (DelayPlayerDeath());
    4.  
    5. }
     
  3. JC_SummitTech

    JC_SummitTech

    Joined:
    Nov 1, 2016
    Posts:
    78
    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).
     
  4. westergard

    westergard

    Joined:
    May 4, 2015
    Posts:
    91
    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...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouvementCiel : MonoBehaviour {
    5.  
    6.     float speed = 0.9f;
    7.     GameObject player;                      
    8.     MouvementJoueur mouvementJoueur;
    9.  
    10.     void Awake()
    11.     {
    12.         player = GameObject.FindGameObjectWithTag ("Player");
    13.         mouvementJoueur = player.GetComponent <MouvementJoueur> ();
    14.     }
    15.  
    16.     void FixedUpdate() {
    17.         Vector3 pos = transform.position;
    18.         pos.x += speed *Time.deltaTime;
    19.         transform.position = pos;
    20.     }
    21. }    
    merci pour votre aide
     
  5. JC_SummitTech

    JC_SummitTech

    Joined:
    Nov 1, 2016
    Posts:
    78
    Dans ton Fixed update:
    if(!mouvementJoueur.playerDead)
    {
    ... le reste de ton code ici
    }
     
  6. westergard

    westergard

    Joined:
    May 4, 2015
    Posts:
    91
    bizzz... voici mon script... ça me donne une erreur que je n'ai jamais vue,

    Mouvementjoueur.dead is inaccessible due to its protection level

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouvementCiel : MonoBehaviour {
    5.  
    6.     float speed = 0.9f;
    7.     GameObject player;                      
    8.     MouvementJoueur mouvementJoueur;
    9.  
    10.     void Awake()
    11.     {
    12.         player = GameObject.FindGameObjectWithTag ("Player");
    13.         mouvementJoueur = player.GetComponent <MouvementJoueur> ();
    14.     }
    15.  
    16.     void FixedUpdate() {
    17.  
    18.         if (mouvementJoueur.dead)
    19.         {
    20.             return;
    21.         }
    22.         Vector3 pos = transform.position;
    23.         pos.x += speed *Time.deltaTime;
    24.         transform.position = pos;
    25.     }
    26. }    
    merci +++
     
  7. JC_SummitTech

    JC_SummitTech

    Joined:
    Nov 1, 2016
    Posts:
    78
    Assure toi que dead est public.
    si elle est private ou protected tu n'y as pas acces à partir des autres scripts
     
  8. westergard

    westergard

    Joined:
    May 4, 2015
    Posts:
    91
    tout simple... merci, maintenant ça marche. Mais j'ai une erreur récurrente qui revient dans ma console, soit
     
  9. westergard

    westergard

    Joined:
    May 4, 2015
    Posts:
    91
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouvementJoueur : MonoBehaviour {
    5.  
    6.     Vector3 velocite = Vector3.zero;
    7.     public Vector3 gravite;
    8.     public Vector3 pousseeVelocite;
    9.     public float vitesseMax=5f;
    10.     public float vitesseavant =1f;
    11.     bool poussee = false;
    12.  
    13.     public bool dead = false;
    14.     Animator animator;
    15.  
    16.  
    17.     void Start () {
    18.         animator = transform.GetComponent <Animator>();
    19.     if(animator==null){
    20.         Debug.LogError("Ne trouve pas l'animateur!");
    21.  
    22.         }
    23.     }
    24.        
    25.     void Update(){
    26.         if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) ) {
    27.             poussee = true;
    28.         }
    29.     }
    30.  
    31.     void FixedUpdate () {
    32.  
    33.         if (dead)
    34.             vitesseavant += 0f;
    35.             gravite.y += -0.000005f;
    36.  
    37.  
    38.  
    39.  
    40.         velocite += gravite * Time.deltaTime;
    41.         velocite.x = vitesseavant;
    42.         if(poussee == true) {
    43.             poussee = false;
    44.             if(velocite.y < 0)
    45.                 velocite.y = 0;
    46.             velocite += pousseeVelocite;
    47.         }
    48.  
    49.         velocite = Vector3.ClampMagnitude (velocite, vitesseMax);
    50.         transform.position += velocite * Time.deltaTime;
    51.  
    52.         float angle = 0;
    53.         if(velocite.y < 0) {
    54.             angle = Mathf.Lerp(0,-90, -velocite.y / vitesseMax);
    55.         }
    56.  
    57.         transform.rotation = Quaternion.Euler(0,0,angle);
    58.     }
    59.  
    60.     void OnCollisionEnter (Collision collision) {
    61.         animator.SetTrigger ("Dead");
    62.         dead=true;
    63.        
    64.     }
    65. }
    66.  
    67.  
    68.  
    thanks
     
  10. JC_SummitTech

    JC_SummitTech

    Joined:
    Nov 1, 2016
    Posts:
    78
    Il te manque une accolade ouvrante ({) a la fin de la ligne 33.
     
  11. westergard

    westergard

    Joined:
    May 4, 2015
    Posts:
    91
    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
     
  12. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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.

    Code (csharp):
    1.  
    2. if(...)
    3. {
    4. // controlled by if
    5. // controlled by if
    6. }
    7.  
    8. if(...)
    9. // controlled by if
    10. // not controlled by if
    11.  
    12.