Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question problem with Charactercontroller go up and down

Discussion in 'Editor & General Support' started by Fuocartex, Jun 17, 2020.

  1. Fuocartex

    Fuocartex

    Joined:
    Jun 2, 2020
    Posts:
    13
    this is miìy script for character controller but
    when hen he climbs a hill if he is more inclined than 10 ° he does not go forward, he gets stuck and falls under the ground, instead, when he has to go down the hill he does it in jerks how can I solve

    thanks
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.Diagnostics;
    4. using UnityEngine;
    5. using UnityEngine.UIElements;
    6.  
    7. public class CharacterController : MonoBehaviour
    8. {
    9.  
    10.     [System.Serializable]
    11.     public class MoveSettings
    12.     {
    13.  
    14.         public float rotateVel = 100;
    15.         public float forwardVel = 12;
    16.         public float jumpVel = 25;
    17.         public float distToGrounded = 0.1f;
    18.         public LayerMask ground;
    19.      
    20.  
    21.  
    22.  
    23.     }
    24.     [System.Serializable]
    25.     public class PhysSettings
    26.     {
    27.         public float downAccel = -0.75f;
    28.        
    29.  
    30.  
    31.     }
    32.     [System.Serializable]
    33.     public class InputSettings
    34.     {
    35.         public float inputDelay = 0.1f;
    36.         public int jumpDelay = 1000;
    37.         public float tapSpeed = 0.5f;
    38.         public float slopeAngle;
    39.        
    40.     }
    41.  
    42.     public MoveSettings moveSetting = new MoveSettings();
    43.     public PhysSettings physSetting = new PhysSettings();
    44.     public InputSettings inputSetting = new InputSettings();
    45.  
    46.  
    47.  
    48.     Vector3 velocity = Vector3.zero;
    49.     Quaternion targetRotation;
    50.     Rigidbody rBody;
    51.     Animator anim;
    52.  
    53.    
    54.     public float forwardInput, turnInput ,jumpInput, runInput;
    55.     public Quaternion TargetRotation
    56.     {
    57.         get { return targetRotation; }
    58.     }
    59.  
    60.    /*bool Grounded()
    61.     {
    62.         return Physics.Raycast(transform.position, Vector3.down, moveSetting.distToGrounded, moveSetting.ground);
    63.     }*/
    64.    
    65.     void Start()
    66.     {
    67.        
    68.         //moveSetting.DisstanceToTheGround = GetComponent<Collider>().bounds.extents.y;
    69.         forwardInput = 0;
    70.         turnInput = 0;
    71.         jumpInput = 0;
    72.         runInput = 0;
    73.        
    74.         targetRotation = transform.rotation;
    75.         if (GetComponent<Rigidbody>())
    76.             rBody = GetComponent<Rigidbody>();
    77.         else
    78.         { Debug.LogError("the character needs rigidbody"); }
    79.         if(GetComponent<Animator>())
    80.         {
    81.             anim = GetComponent<Animator>();
    82.         }
    83.         else
    84.         { Debug.LogError("the character needs animator"); }
    85.        
    86.     }
    87.  
    88.     void GetInput()
    89.     {
    90.         forwardInput = Input.GetAxis(/*inputSetting.FORWARD_AXIS*/"Vertical");
    91.         turnInput = Input.GetAxis(/*inputSetting.TURN_AXIS*/ "Horizontal");
    92.         jumpInput = Input.GetAxisRaw(/*inputSetting.JUMP_AXIS*/ "Jump");
    93.         runInput = Input.GetAxis("shiftrun");
    94.      
    95.     }
    96.  
    97.     void Update()
    98.     {
    99.         GetInput();
    100.         Turn();
    101.     }
    102.  
    103.     void FixedUpdate()
    104.     {
    105.         Run();
    106.         Jump();
    107.         rBody.velocity = transform.TransformDirection(velocity);
    108.         attack();
    109.         Grounded();
    110.     }
    111.        
    112.  
    113.    
    114.  
    115.     void Run()
    116.     {
    117.        
    118.  
    119.         if (Mathf.Abs(forwardInput) > inputSetting.inputDelay)
    120.         {
    121.             //move rBody.velocity = transform.forward * forwardInput * moveSetting.forwardVel;
    122.  
    123.        
    124.  
    125.             velocity.z = moveSetting.forwardVel * forwardInput;
    126.  
    127.             if (Mathf.Abs(runInput) > inputSetting.inputDelay)
    128.             {
    129.                 moveSetting.forwardVel = 30;
    130.                 anim.SetFloat("velocity", 2f);
    131.                
    132.                
    133.  
    134.             }
    135.             else
    136.             {
    137.                 moveSetting.forwardVel = 12;
    138.                 anim.SetFloat("velocity", 0f);
    139.                 anim.SetInteger("condition", 1);
    140.             }
    141.             anim.SetInteger("condition", 1);
    142.            
    143.            
    144.             //rBody.velocity = transform.forward * forwardInput * moveSetting.forwardVel;
    145.         }
    146.         else
    147.         {
    148.             //  rBody.velocity = Vector3.zero;
    149.             velocity.z = 0;
    150.             anim.SetInteger("condition", 0);
    151.             //rBody.velocity = Vector3.zero;
    152.         }
    153.        
    154.  
    155.     }
    156.  
    157.     void Turn()
    158.     {
    159.         if (Mathf.Abs(turnInput) > inputSetting.inputDelay)
    160.         {
    161.             targetRotation *= Quaternion.AngleAxis(moveSetting.rotateVel * turnInput * Time.deltaTime, Vector3.up);
    162.         }
    163.         transform.rotation = targetRotation;
    164.     }
    165.    
    166.     void Jump()
    167.     {
    168.  
    169.         if (jumpInput>0 && isGrounded)
    170.         {
    171.             anim.SetBool("jumpon", true);
    172.            
    173.             velocity.y = moveSetting.jumpVel;
    174.            
    175.  
    176.             //moveSetting.stopJump = true;
    177.         }
    178.         else if (jumpInput==0 && isGrounded)
    179.         {
    180.             velocity.y = 0;
    181.             anim.SetBool("jumpon", false);
    182.          
    183.         }
    184.         else
    185.         {
    186.          
    187.             velocity.y -= physSetting.downAccel;
    188.          
    189.         }
    190.  
    191.  
    192.  
    193.     }
    194.     void attack()
    195.     {
    196.         if (Input.GetKeyDown("f"))
    197.             anim.SetBool("attackon", true);
    198.         else
    199.             anim.SetBool("attackon", false);
    200.     }
    201.  
    202.     public bool isGrounded = false;
    203.  
    204.     public void Grounded()
    205.     {
    206.         RaycastHit hit;
    207.         if (Physics.Raycast(transform.position, Vector3.down, out hit, moveSetting.distToGrounded, moveSetting.ground))
    208.         {
    209.             inputSetting.slopeAngle = (Vector3.Angle(hit.normal, transform.forward) - 90);
    210.            isGrounded = true;
    211.         }
    212.  
    213.         else
    214.         {
    215.             isGrounded = false;
    216.         }
    217.     }
    218.  
    219.    
    220. }
    221.  
    222.  
    223.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Your thread title and class name are confusing. Unity's built in CharacterController class is spelled exactly the same, and what people think you are talking about until they spend time looking through your script. Probably not a good idea, though not your specific problem here.

    On going down hills you're not applying any downward force if isGrounded is true. So I'd expect moving forward would not move down the slope but instead directly forward, until isGrounded resolves to false and then the character falls. I'm assuming you have gravity disabled on this object. Is there a reason why you don't just enable gravity and let Unity handle the downward force automatically, and don't override the Y velocity other than when starting a jump? The way you're currently doing it will result in a staircase effect.

    As far as moving forward when going up the hill, you are just applying more Z velocity, so any movement up the hill is just it sliding/friction against the terrain collider instead of you applying some Y velocity upward (on top of that, you're actually forcing Y velocity to 0f on line 180, which will prevent the physics system from applying some Y velocity automatically, pushing it up the hill, as a result of collision with the sloped terrain collider). Depending on physics materials involved, at a certain angle it just won't move up the slope doing it like this I'm sure. You keep just applying velocity pressing against the collider and then you're popping through it, which is what you see when it falls. Maybe using one of the continuous detection modes on the rigidbody will help prevent that from occurring. You used to be able to effectively make your terrain collider "thicker" to help address falling through the terrain, but I believe that was removed in the 2018.3 terrain system redesign.
     
  3. Fuocartex

    Fuocartex

    Joined:
    Jun 2, 2020
    Posts:
    13
    ok thanks but what can I do to solve those problems?