Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

can you help with my code?

Discussion in 'Scripting' started by ratg97, Aug 5, 2014.

  1. ratg97

    ratg97

    Joined:
    Aug 4, 2014
    Posts:
    65
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ControladorPersonaje : MonoBehaviour {
    5.    
    6.     public float fuerzaSalto = 100f;
    7.  
    8.     public float distanceTraveled;
    9.     //public TextMesh marcador;
    10.  
    11.    
    12.     private bool enSuelo = true;
    13.     public Transform comprobadorSuelo;
    14.     private float comprobadorRadio = 0.07f;
    15.     public LayerMask mascaraSuelo;
    16.    
    17.     private Animator animator;
    18.    
    19.     private bool corriendo = false;
    20.     public float velocidad = 1f;
    21.    
    22.     void Awake(){
    23.         animator = GetComponent<Animator>();
    24.     }
    25.  
    26.     void Start () {
    27.         NotificationCenter.DefaultCenter().AddObserver(this, "PersonajeHaMuerto");
    28.  
    29.     }
    30.    
    31.     void PersonajeHaMuerto(Notification notificacion){
    32.         if(distanceTraveled > EstadoJuego.estadoJuego.puntuacionMaxima){
    33.  
    34.             EstadoJuego.estadoJuego.puntuacionMaxima = distanceTraveled;
    35.             EstadoJuego.estadoJuego.Guardar();
    36.  
    37.         }
    38.     }
    39.    
    40.     void FixedUpdate(){
    41.         if(corriendo){
    42.             rigidbody2D.velocity = new Vector2(velocidad, rigidbody2D.velocity.y);
    43.         }
    44.         animator.SetFloat("VelX", rigidbody2D.velocity.x);
    45.         enSuelo = Physics2D.OverlapCircle(comprobadorSuelo.position, comprobadorRadio, mascaraSuelo);
    46.         animator.SetBool("isGrounded", enSuelo);
    47.     }
    48.  
    49.     private void GameStart () {
    50.        
    51.         distanceTraveled = 0f;
    52.         GUIManager.SetDistance (distanceTraveled);
    53.  
    54.     }
    55.  
    56.     //void ActualizarMarcador(){
    57.     //    marcador.text = distanceTraveled.ToString();
    58.     //}
    59.  
    60.     // Update is called once per frame
    61.     void Update () {
    62.  
    63.         distanceTraveled = transform.localPosition.x;
    64.         GUIManager.SetDistance(distanceTraveled);
    65.  
    66.         if(Input.GetMouseButtonDown(0)){
    67.             if(corriendo){
    68.                 // Hacemos que salte si puede saltar
    69.                 if(enSuelo){
    70.                     audio.Play();
    71.                     rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, fuerzaSalto);
    72.                     //rigidbody2D.AddForce(new Vector2(0, fuerzaSalto));
    73.                 }
    74.             }else{
    75.                 corriendo = true;
    76.                 NotificationCenter.DefaultCenter().PostNotification(this, "PersonajeEmpiezaACorrer");
    77.             }
    78.         }
    79.     }
    80. }
    81.  
    i want that my player have 3 jumps but i dont know
     
  2. Dave-xP

    Dave-xP

    Joined:
    Nov 8, 2013
    Posts:
    106
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ControladorPersonaje : MonoBehaviour {
    5.  
    6.     public float fuerzaSalto = 100f;
    7.  
    8.     public float distanceTraveled;
    9.     //public TextMesh marcador;
    10.  
    11.     private bool enSuelo = true;
    12.     public Transform comprobadorSuelo;
    13.     private float comprobadorRadio = 0.07f;
    14.     public LayerMask mascaraSuelo;
    15.  
    16.     // Y U NO Speak English?
    17.     public int jumpsHave = 3;
    18.  
    19.     private Animator animator;
    20.  
    21.     private bool corriendo = false;
    22.     public float velocidad = 1f;
    23.  
    24.     void Awake(){
    25.         animator = GetComponent<Animator>();
    26.     }
    27.  
    28.     void Start () {
    29.         NotificationCenter.DefaultCenter().AddObserver(this, "PersonajeHaMuerto");
    30.  
    31.     }
    32.  
    33.     void PersonajeHaMuerto(Notification notificacion){
    34.         if(distanceTraveled > EstadoJuego.estadoJuego.puntuacionMaxima){
    35.  
    36.             EstadoJuego.estadoJuego.puntuacionMaxima = distanceTraveled;
    37.             EstadoJuego.estadoJuego.Guardar();
    38.  
    39.         }
    40.     }
    41.  
    42.     void FixedUpdate(){
    43.         if(corriendo){
    44.             rigidbody2D.velocity = new Vector2(velocidad, rigidbody2D.velocity.y);
    45.         }
    46.         animator.SetFloat("VelX", rigidbody2D.velocity.x);
    47.         enSuelo = Physics2D.OverlapCircle(comprobadorSuelo.position, comprobadorRadio, mascaraSuelo);
    48.         animator.SetBool("isGrounded", enSuelo);
    49.     }
    50.  
    51.     private void GameStart () {
    52.      
    53.         distanceTraveled = 0f;
    54.         GUIManager.SetDistance (distanceTraveled);
    55.  
    56.     }
    57.  
    58.     //void ActualizarMarcador(){
    59.     //    marcador.text = distanceTraveled.ToString();
    60.     //}
    61.  
    62.     // Update is called once per frame
    63.     void Update () {
    64.  
    65.         distanceTraveled = transform.localPosition.x;
    66.         GUIManager.SetDistance(distanceTraveled);
    67.  
    68.         if(Input.GetMouseButtonDown(0)){
    69.             if(corriendo){
    70.                 // Hacemos que salte si puede saltar
    71.                 if(enSuelo && jumpsHave > 0){
    72.                     jumpsHave--;
    73.                     audio.Play();
    74.                     rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, fuerzaSalto);
    75.                     //rigidbody2D.AddForce(new Vector2(0, fuerzaSalto));
    76.                 }
    77.             }else{
    78.                 corriendo = true;
    79.                 NotificationCenter.DefaultCenter().PostNotification(this, "PersonajeEmpiezaACorrer");
    80.             }
    81.         }
    82.     }
    83. }
     
  3. ratg97

    ratg97

    Joined:
    Aug 4, 2014
    Posts:
    65
    WITH THIS CODE WHat can i do?
     
  4. ratg97

    ratg97

    Joined:
    Aug 4, 2014
    Posts:
    65
    i do it and i dont see the diference of my old code...
     
  5. Dave-xP

    Dave-xP

    Joined:
    Nov 8, 2013
    Posts:
    106
    you can only jump 3 times now
    or isnt that what you wanted?
     
  6. ratg97

    ratg97

    Joined:
    Aug 4, 2014
    Posts:
    65
    i want... it is a android game... and endleess runer... and it spawns 3 objcts in diferrents heights.. and i need 3 differents jump .... the height of jumps is how you hold touch the screen
     
  7. MysteriX

    MysteriX

    Joined:
    Apr 8, 2014
    Posts:
    54
    You should learn how to write posts...

    Here is an idea how to do it:
    detect touch on display and save the time when you touch in a variable. when you end the touch you have to compare the time with the time you saved at the beginning of touch. Now you have to say for example:

    if timeDifference<= 0.3f then jump(0.5f)
    else if timeDifference <= 0.6f && timeDifference > 0.3f then jump(0.75f)
    else if timeDifference > 0.6f then jump(1f)
     
  8. ratg97

    ratg97

    Joined:
    Aug 4, 2014
    Posts:
    65
    and the code?
     
  9. Kokumo

    Kokumo

    Joined:
    Jul 23, 2010
    Posts:
    416
    ratg97, this community want to help... but in order to do it, you must show some compromise.
    You can't ask for help, and after get the ideas, ask for the code.

    Try to do it yourself; this will enforce us to guide you if you get stuck.
     
    TheSignal likes this.
  10. ratg97

    ratg97

    Joined:
    Aug 4, 2014
    Posts:
    65
    uppppppp
     
  11. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    Your not going to get much help if your going to be immature and repost the same thing in 4 different topics. Nore are you going to get people to write your code for you.

    Just try it yourself and come back if you get stuck for a while, and ask for help about that problem your stuck on, not on the script as a whole.