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

Question Fixing bouncing down slopes?

Discussion in 'Scripting' started by androidac67, Oct 9, 2023.

  1. androidac67

    androidac67

    Joined:
    Jan 3, 2023
    Posts:
    77
    I solved this bouncing issue by altering the value of my vertical velocity:
    verticalVelocity = -6.5f;
    and putting it inside my Update. However I only just noticed that it creates other problems, so I tried to move that line of code into my
    Vector3.SignedAngle
    code so that only when I am on an angled surface (slope), my
    verticalVelocity
    goes from its default -2f to the -6.5f shown above in order to fix bouncing. But no matter where I put it in the code below, it does not work and the velocity remains the default -2f.

    Code (CSharp):
    1. // Update
    2. RaycastHit hit;
    3.  
    4. if (controller.isGrounded && Physics.Raycast(transform.position, Vector3.down, out hit, 2f))
    5. {
    6.         hitPointNormal = hit.normal;
    7.         float signedAngle = Vector3.SignedAngle(hit.normal, Vector3.up, transform.right);
    8.  
    9.         if (signedAngle < 0.1f)
    10.         {
    11.             if (isOnAngledSurface)
    12.             {
    13.                 Debug.Log("Angle <"); // moving downhill, boost speed
    14.                 speed = 8f;
    15.                 isOnAngledSurface = false;
    16.             }
    17.         }
    18.         if (signedAngle > 30f)
    19.         {
    20.             if (!isOnAngledSurface)
    21.             {
    22.                 Debug.Log("Angle >"); // moving up hill, decrease speed
    23.                 speed = 2.5f;
    24.                 isOnAngledSurface = true;
    25.             }
    26.            
    27.         }
    28.     }
    29.     else
    30.     {
    31.         if (isOnAngledSurface)
    32.         {
    33.             // Transitioning from angled surface to flat surface
    34.             speed = 4f;
    35.             isOnAngledSurface = false;
    36.         }
    37.     }
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    541
    Code (CSharp):
    1.         Vector3 moveDirection=new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
    2.         if (cc.isGrounded)
    3.         {
    4.             Physics.SphereCast(transform.position,0.5f,Vector3.down,out RaycastHit hit,5f);
    5.             moveDirection=Vector3.ProjectOnPlane(moveDirection,hit.normal);
    6.         }
     
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,355
    Add a Debug.Log for signedAngle to see if it is what you think it should be. Also, put Debug.Log messages inside your if statements to see if they are ever entered.