Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need help setting maximum slope angle* for jumping

Discussion in 'Scripting' started by Mystique, Nov 22, 2014.

  1. Mystique

    Mystique

    Joined:
    Nov 19, 2014
    Posts:
    40
    I watched a tutorial and followed it step for step, got everything working up until the Vector3.Angle( , ) part.
    Whats suppose to happen is if the angle difference between contact.normal and vector3.up is less than 1 then I should be grounded, but it keeps saying I'm grounded when I jump onto walls even if they're completely vertical.

    So I checked Debug.Log to see what contact.normal says and what Vector3.up says and i get this for Vector3.up: (0,1,0)
    and for contact.normal I get (x, y, z) numbers always ranging from -10 to 10.

    Does anyone know why this isn't working, and if not can anyone give insight to a different way to provide limits on angles that can be jumped from.


    I removed a few variables and implemented numbers in their place, so that the code is easier to read.


    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Code (JavaScript):
    1. private var grounded : boolean;
    2. function Update () {
    3. if (Input.GetButtonDown("Jump") && grounded) {
    4. rigidbody.AddForce (0, 25, 0);
    5. }
    6. Debug.Log (grounded);
    7. }
    8. function OnCollisionStay(collision : Collision)
    9. {
    10. for (var contact : ContactPoint in collision.contacts)
    11. {
    12. { if (Vector3.Angle(contact.normal, Vector3.up) < 1);      // having trouble with this line
    13. grounded = true;
    14. }
    15. }
    16. }
    17. function OnCollisionExit ()
    18. {
    19. grounded = false;
    20. }
     
    Last edited: Nov 22, 2014
  2. Stoven

    Stoven

    Joined:
    Jul 28, 2014
    Posts:
    171
    The code would be much easier to read if you use Code tags (it should be the icon that looks like a paper with writing on it).

    Also, can you give a link to the tutorial? There might be a subtlety about it that can explain why you're experiencing this behavior.
     
    prietokevin likes this.
  3. Mystique

    Mystique

    Joined:
    Nov 19, 2014
    Posts:
    40
    Last edited: Nov 22, 2014
  4. Stoven

    Stoven

    Joined:
    Jul 28, 2014
    Posts:
    171
    You have a semicolon right after your condition in the if statement, so it looks like you're always considered grounded even when you're colliding into a surface that doesn't have a relatively close normal angle to Vector3.up

    Code (CSharp):
    1.  if (Vector3.Angle(contact.normal, Vector3.up) < 1)      //remove the semicolon
    2.     grounded = true;
    Try that and see if it works out for you.
     
  5. Mystique

    Mystique

    Joined:
    Nov 19, 2014
    Posts:
    40
    Thanks - I didn't notice that semicolon after a bit of tweaking it's all up and working now
     
  6. prietokevin

    prietokevin

    Joined:
    Dec 19, 2016
    Posts:
    3
    thank you- but... jumped only once, after nothing

    www.youtube.com : FPS1.5 Jumping the Right Way. Unity3D FPS Game Design Tutorial.

    this is the code
    Code (JavaScript):
    1. var walkAcceleration : float = 5 ;
    2. var CameraObject  : GameObject;
    3. var maxWalkSpeed : float = 10;
    4.  
    5. var horizontalmovement: Vector2;
    6. var jumpVelocity : float = 20;
    7.  
    8. var grounded : boolean = false;
    9.  
    10.  
    11. var maxSlope: float =300;
    12.  
    13. function Update()
    14.         {
    15.         var rb : Rigidbody;
    16.         rb=GetComponent.<Rigidbody>();
    17.  
    18.         horizontalMovement = new Vector2(rb.velocity.x,rb.velocity.z);
    19.         if (horizontalmovement.magnitude > maxWalkSpeed)
    20.         {
    21.             horizontalmovement = horizontalmovement.normalized * maxWalkSpeed;
    22.         }
    23.  
    24.         rb.velocity = new Vector3(horizontalmovement.x,rb.velocity.y,horizontalmovement.y);
    25.  
    26.         //rb.velocity.x = horizontalMovement.x;
    27.         //rb.velocity.z = horizontalMovement.y;
    28.  
    29.         transform.rotation = Quaternion.Euler(0,CameraObject.GetComponent(mouselookscript).currentyrotation,0);
    30.  
    31.         rb.AddRelativeForce(Input.GetAxis("Horizontal")*walkAcceleration,0,Input.GetAxis("Vertical")*walkAcceleration);
    32.  
    33. ////////////////////////////////////////////////// ////////////////////////////////
    34.  
    35.    
    36.  
    37.         if(Input.GetButtonDown("Jump")&&grounded)
    38.         {
    39.             rb.AddForce(0,jumpVelocity,0);
    40.         }
    41.  
    42.     Debug.Log (grounded);
    43.  
    44.         }
    45.  
    46. function OnCollisionStay (collision : Collision)
    47.     {
    48.         for (var contact : ContactPoint in collision.contacts)
    49.     {
    50.         if (Vector3.Angle(contact.normal,Vector3.up)< maxSlope)
    51.         Grounded = true;  
    52.     }
    53.  
    54.     }
    55.  
    56. function OnCollisionExit()
    57.         {
    58.             grounded = false;
    59.         }
    60.  
    61.        
     
    Last edited: Jan 25, 2017
  7. AndyGainey

    AndyGainey

    Joined:
    Dec 2, 2015
    Posts:
    216
    Code is case-sensitive. When you try to set grounded to true, you used an uppercase 'G', which essentially refers to a completely different variable, so grounded remains false while Grounded (which is never accessed when you're checking if you're grounded) is true. Fix that to a lowercase 'g' and see if that gets you grounded again after the first jump.

    The general lesson is that code is very picky. Never underestimate just how precise you have to be to make the code do what you want. :)
     
    prietokevin likes this.
  8. prietokevin

    prietokevin

    Joined:
    Dec 19, 2016
    Posts:
    3
    Thank you very much for the advice :D