Search Unity

Character Controller falls over when mid-air or when climbing steps.

Discussion in 'Physics' started by hkay_, Mar 27, 2015.

  1. hkay_

    hkay_

    Joined:
    Feb 15, 2015
    Posts:
    5
    Hello, I'm currently working on a Diablo-style RPG and I already ran into some problems. My player movement script works fine, as long as I'm not in mid-air or try to climb steps, then my character falls over and starts glitching.

    Here's a video:


    Here's my script:

    Code (JavaScript):
    1.  
    2. #pragma strict
    3.  
    4. @script RequireComponent(CharacterController)
    5.  
    6. var gravity : float;
    7. var maxSpeed : float;
    8. var speedAnim : float;
    9. var bulletSpeed : float;
    10. var fireDistance : float;
    11.  
    12. private var moveDirection : Vector3;
    13. private var moveGravity : Vector3;
    14. private var moveSpeed : float;
    15. private var playerPosition : Transform ;              
    16. private var destinationPosition : Vector3;
    17. private var destinationDistance : float;          
    18. private var animator : Animator;
    19.  
    20. function Start() {
    21.     animator = GetComponent("Animator");
    22.     playerPosition = transform;                          
    23.     destinationPosition = playerPosition.position;
    24. }
    25.  
    26. function Update() {
    27.     destinationDistance = Vector3.Distance(destinationPosition, playerPosition.position);
    28.     var controller : CharacterController = GetComponent.<CharacterController>();
    29.     animator.SetFloat("Speed", speedAnim);
    30.     moveDirection = transform.TransformDirection(Vector3.forward) * moveSpeed;
    31.     moveGravity = transform.TransformDirection(Vector3.up);
    32.     controller.SimpleMove(moveGravity * gravity);                    
    33.        
    34.     controller.Move(moveDirection * Time.deltaTime);
    35.     if (Input.GetMouseButtonDown(1)&& GUIUtility.hotControl ==0)
    36.     {
    37.          var playerPlane : Plane = new Plane(Vector3.up, playerPosition.position);
    38.         var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    39.         var hitdist : float = 0.0f;
    40.         if (playerPlane.Raycast(ray, hitdist))
    41.         {
    42.             var targetPoint : Vector3 = ray.GetPoint(hitdist);
    43.             destinationPosition = ray.GetPoint(hitdist);
    44.             var targetRotation : Quaternion = Quaternion.LookRotation(targetPoint - transform.position);
    45.             playerPosition.rotation = targetRotation;
    46.         }
    47.     }
    48.     moveDirection.y -= gravity * Time.deltaTime;
    49.     controller.Move(moveDirection * Time.deltaTime);
    50.  
    51.     if(destinationDistance <0.5f)
    52.     {
    53.         speedAnim = 0.5f;
    54.         moveSpeed = 0f;
    55.     }
    56.    
    57.     if(destinationDistance > 0.5f)
    58.     {
    59.         speedAnim = 1f;  
    60.         moveSpeed = maxSpeed;
    61.     }
    62.    
    63.     if(destinationDistance > .5f)
    64.     {
    65.         transform.LookAt(destinationPosition);
    66.     }
    67. }
    68.  
    Thanks in advance for any help!
     
  2. hkay_

    hkay_

    Joined:
    Feb 15, 2015
    Posts:
    5
    I think i know, what the problem is, but i still don't know, how to solve it. Apparently, the player only detects the mouseclick on the plane, because when i click directly on the cube the player tries to get to the point under the cube on the plane, which creates the glitch.
     
  3. hkay_

    hkay_

    Joined:
    Feb 15, 2015
    Posts:
    5
    Is there really nobody, who is able to help me? :(