Search Unity

Problem with Click to Move Script and Character Controller.

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

  1. hkay_

    hkay_

    Joined:
    Feb 15, 2015
    Posts:
    5
    Hello, I've got a problem with my player movement script. The player doesn't move left, right or back, only forward. I'm stuck at this for hours now and would be really happy, if someone could help me.
    Here is my code:

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

    Brotherhood

    Joined:
    Dec 8, 2012
    Posts:
    73
    You need to update your moveDirection to be dependent on the destination. You want it to be updated when you click the mouse button.

    On that note,your elseif(controller.isGrounded) section has no purpose because you would be moving even if you weren't grounded (since all your movement code is outside of the condition). I would probably fix that too.
     
    hkay_ likes this.
  3. hkay_

    hkay_

    Joined:
    Feb 15, 2015
    Posts:
    5
    Thanks! It works! I'm using
    Code (JavaScript):
    1.  
    2. moveDirection = transform.TransformDirection(Vector3.forward) * moveSpeed;