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

Character slides down hill when stopped or moves slow when going up hills

Discussion in 'Scripting' started by Vedrit, Apr 21, 2016.

  1. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    Hey all, I'm not really sure if this should be here in Scripting or in Physics, and I'll explain later.
    I'm using a character controller script by Renaissance Coders on YouTube, but I have an issue where if the character is on a slope, they slide down. If needed, I'll post my script when I get home.
    If I set the rigidbody on the character to not use gravity, and instead force the player down to the ground via script, then they no long slide down slopes. However, they now move much faster going down hill and much slower going up hill, compared to level terrain.

    Neither of these is really acceptable, and I'm not sure if the fix is in the Physics side, or if it's something I'll need to program in via scripting, or a combination of both. Any help would be appreciated.
    Just to note, my character is currently a simple sphere with the character controller and rigid body components.
     
  2. SD2020_

    SD2020_

    Joined:
    Nov 3, 2015
    Posts:
    107
    That sounds like the script is code to react with the rigid body physics, I wouldn't be to fimilear with this format but try debug the code to find physics involved, and if not it could just be the rigid odd itself. Disabling use gravity will cause the player to glide away but you can try ground him via script.
     
  3. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    Like I said, though, if I disable use gravity, then the gliding stop, but slopes have greater affect on speed than I'd like
     
  4. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    Here's the script I'm using for my Character Controller
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using UnityEngine.Networking;
    5. using System;
    6.  
    7. public class CharacterController : NetworkBehaviour
    8. {
    9.  
    10.     public float inputDelay = 0.1f;
    11.     public float forwardVel = 12;
    12.     public float rotateVel = 100;
    13.     public float fallVel = -9.81f; //Gravity replacement. Without it, the character falls very slowly
    14.     public float sprintMod;
    15.     bool canMove;
    16.     public bool inDialogue;
    17.     public bool statusEffected;
    18.     int MaxStamina;
    19.     public float CurStamina;
    20.     Vector3 properVel;
    21.     CharacterInfo charInfo;
    22.     public bool sprinting = false;
    23.     public float stopSprint = 0;
    24.     Image stamBar;
    25.     public bool canSprint = true;
    26.    
    27.  
    28.     Quaternion targetRotation;
    29.     Rigidbody rBody;
    30.     float forwardInput, turnInput;
    31.  
    32.     public Quaternion TargetRotation
    33.     {
    34.         get { return targetRotation; }
    35.     }
    36.  
    37.     void Start()
    38.     {
    39.         stamBar = GameObject.Find("StaminaBar").GetComponent<Image>();
    40.         charInfo = FindObjectOfType<CharacterInfo>();
    41.         properVel = new Vector3(0, fallVel, 0);
    42.         targetRotation = transform.rotation;
    43.         if (GetComponent<Rigidbody>())
    44.             rBody = GetComponent<Rigidbody>();
    45.  
    46.         forwardInput = turnInput = 0;
    47.     }
    48.  
    49.     void GetInput()
    50.     {
    51.         if (canMove == true)
    52.         {
    53.             forwardInput = Input.GetAxis("Vertical");
    54.             turnInput = Input.GetAxis("Horizontal");
    55.         }
    56.         else
    57.         {
    58.             turnInput = Input.GetAxis("Horizontal");
    59.             forwardInput = 0;
    60.         }
    61.     }
    62.  
    63.     void Update()
    64.     {
    65.         if (isLocalPlayer)
    66.         {
    67.             if(inDialogue)
    68.             {
    69.                 canMove = false;
    70.             }
    71.             else if(!inDialogue && !statusEffected)
    72.             {
    73.                 canMove = true;
    74.             }
    75.             GetInput();
    76.             Turn();
    77.             if (charInfo != null)
    78.             {
    79.                 MaxStamina = charInfo.stamina;
    80.             }
    81.  
    82.             if (sprinting == false && (Time.time - stopSprint) >= 1)
    83.             {
    84.                 CurStamina = Mathf.Round((CurStamina + ((1 + (charInfo.stats[0] / 2)) / 60f)) * 100) / 100;
    85.             }
    86.             if (sprinting && (!Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S)))
    87.             {
    88.                 sprinting = false;
    89.                 stopSprint = Time.time;
    90.             }
    91.             stamBar.fillAmount = CurStamina / MaxStamina;
    92.             if (CurStamina > MaxStamina)
    93.             {
    94.                 CurStamina = MaxStamina;
    95.             }
    96.             if (CurStamina <= 0)
    97.             {
    98.                 canSprint = false;
    99.                 CurStamina = 0;
    100.             }
    101.             if (canSprint == false && stamBar.fillAmount > 0.1)
    102.             {
    103.                 canSprint = true;
    104.             }
    105.         }
    106.     }
    107.     void FixedUpdate()
    108.     {
    109.         Run();
    110.     }
    111.  
    112.     void Run()
    113.     {
    114.         if (isLocalPlayer)
    115.         {
    116.             if (Mathf.Abs(forwardInput) > inputDelay)
    117.             {
    118.                 if (canSprint && Input.GetKey(KeyCode.LeftShift) && CurStamina >= 0.02)
    119.                 {
    120.                     sprintMod = 0.50f;
    121.                     CurStamina = CurStamina - (Mathf.Round((5 / 60f) * 100) / 100f);
    122.                     sprinting = true;
    123.                 }
    124.                 else
    125.                 {
    126.                     sprintMod = 0;
    127.                     if (Input.GetKeyUp(KeyCode.LeftShift) && canSprint)
    128.                     {
    129.                         stopSprint = Time.time;
    130.                     }
    131.                     sprinting = false;
    132.                 }
    133.                 rBody.velocity = properVel + (transform.forward * (forwardInput) * (forwardVel * (1 + sprintMod)));
    134.             }
    135.             else
    136.             {
    137.                 rBody.velocity = properVel;
    138.             }
    139.         }
    140.     }
    141.  
    142.     void Turn()
    143.     {
    144.         targetRotation *= Quaternion.AngleAxis(rotateVel * turnInput * Time.deltaTime, Vector3.up);
    145.         transform.rotation = targetRotation;
    146.     }
    147. }
    148.  
     
  5. SD2020_

    SD2020_

    Joined:
    Nov 3, 2015
    Posts:
    107
    Therigidbody variable line 29, could possibly be your problem.
     
  6. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    What do you mean? That's just declaring a variable, and gets set in line 44, after checking if there is a rigidbody (which there is)
     
  7. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    Anyone have a suggestion?
     
  8. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    I'm still needing help on this.
     
  9. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    Any ideas on how I could fix this?
     
  10. smacbride

    smacbride

    Joined:
    Jan 10, 2015
    Posts:
    50
    The character controller that comes with the standard unity assets goes over this in great detail. I started with that script and then modified it to suit my needs.
     
  11. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    It does? I guess I'll take a look at it and learn something. Thanks
     
  12. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    We must be looking at different standard assets, because I saw nothing about anything regarding slopes.

    EDIT: I did see something in the RigidbodyFirstPersonController, but there were 0 details about it, such as what it does, why it does it, or when it is applied. Not exactly detailed
     
    Last edited: May 4, 2016
  13. Flunkee

    Flunkee

    Joined:
    Mar 6, 2015
    Posts:
    17
    Does your character rely on physics in any way besides gravity? If not just toggle Is Kinematic in your characters rigid body.
     
  14. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    Since I'm moving the character by changing it's velocity, this unfortunately isn't an option. A work-around was to unfreeze or freeze rigidbody constraints when my controller script checks if the player is pressing a movement key or not.
    I don't think it's the best work-around, but... for now.
     
  15. stokatyan

    stokatyan

    Joined:
    Aug 11, 2016
    Posts:
    2
    Add a material to the capsule collider attached to your rigidbody. That fixed it for me.
     
    ayhaab-pasha likes this.
  16. ayhaab-pasha

    ayhaab-pasha

    Joined:
    Dec 6, 2015
    Posts:
    5
    I used the Max Friction and it worked for me! Thanks for the heads up!