Search Unity

problem with Eteeski Tutorial 1.5 Jumping the right way

Discussion in 'Scripting' started by jan2100, Sep 9, 2012.

  1. jan2100

    jan2100

    Joined:
    Jun 29, 2012
    Posts:
    15
    I'm following this tutorial and when I try to jump it doesn't work on the terrain but works on objects with collider. Can you help me?

    Code (csharp):
    1. var acceleracio : float = 5;
    2. var cameraPrincipal : GameObject;
    3. var velocitatMaxima : float = 20;
    4. @HideInInspector
    5. var movimentHoritzontal : Vector2;
    6.  
    7. var velocitatSalt : float = 20;
    8. @HideInInspector
    9. var grounded : boolean = false;
    10. var inclinacioMaxima : float = 60;
    11.  
    12. function Update () {
    13.  
    14.     movimentHoritzontal = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
    15.    
    16.     if(movimentHoritzontal.magnitude > velocitatMaxima){
    17.    
    18.         movimentHoritzontal = movimentHoritzontal.normalized;
    19.         movimentHoritzontal *= velocitatMaxima;
    20.    
    21.     }
    22.    
    23.     rigidbody.velocity.x = movimentHoritzontal.x;
    24.     rigidbody.velocity.z = movimentHoritzontal.y;
    25.    
    26.     transform.rotation = Quaternion.Euler(0, cameraPrincipal.GetComponent(rotacio).rotacioYActual, 0);
    27.    
    28.     rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * acceleracio, 0, Input.GetAxis("Vertical") * acceleracio);
    29.    
    30.     if(Input.GetButtonDown("Jump")  grounded){
    31.         rigidbody.AddRelativeForce(0, velocitatSalt, 0);
    32.     }
    33.  
    34. }
    35.  
    36. function OnCollisionStay(collision: Collision){
    37.  
    38.     for( var contact : ContactPoint in collision.contacts){
    39.        
    40.         if(Vector3.Angle(contact.normal, Vector3.up) > inclinacioMaxima)
    41.             grounded = true;
    42.    
    43.     }
    44.  
    45. }
    46.  
    47. function OnCollisionExit(){
    48.  
    49.     grounded= false;
    50.    
    51. }
    thanks
     
  2. KyleOlsen

    KyleOlsen

    Joined:
    Apr 3, 2012
    Posts:
    237
    Try increasing your "inclinacioMaxima" to a higher number. That's stopping you from becoming grounded if the angle of the surface you landed on is more steep than that number.
     
  3. jan2100

    jan2100

    Joined:
    Jun 29, 2012
    Posts:
    15
    it works on objects but in a terrain doesn't work
     
  4. KyleOlsen

    KyleOlsen

    Joined:
    Apr 3, 2012
    Posts:
    237
    Try debugging the number of contact.points, and the angle of the collision when colliding with the terrain. Then do the same for the colliders that work, see if anything changes.
     
  5. jan2100

    jan2100

    Joined:
    Jun 29, 2012
    Posts:
    15
    I solve the problem. I made a mistake in the code.

    thanks Kyle for the help