Search Unity

Character Controller - Character starts spinning

Discussion in 'Scripting' started by henmachuca, Feb 5, 2018.

  1. henmachuca

    henmachuca

    Joined:
    Oct 14, 2016
    Posts:
    105
    Hello, I am coding a player movement script using a Character Controller without rigidbody in my player.

    The thing is that whenever the player gets to the target position, sometimes it starts spinning around and wont stop. I can't figure out what's happening. Any thoughts?

    Code (CSharp):
    1. private Animator anim;
    2.     private CharacterController charController;
    3.    
    4.     private float moveSpeed = 5f;
    5.     private bool canMove;
    6.     private bool finishedMovement = true;
    7.  
    8.     private Vector3 targetPos = Vector3.zero;
    9.     private Vector3 playerMove = Vector3.zero;
    10.     private float playerToPointDistance;
    11.  
    12.     void Start ()
    13.     {
    14.         anim = GetComponent<Animator>();
    15.         charController = GetComponent<CharacterController>();
    16.     }
    17.    
    18.     void Update ()
    19.     {
    20.         MovePlayer();
    21.         charController.Move(playerMove);
    22.     }
    23.  
    24.     void MovePlayer()
    25.     {
    26.         if(Input.GetMouseButtonDown(0))
    27.         {
    28.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    29.             RaycastHit hit;
    30.  
    31.             if(Physics.Raycast(ray, out hit))
    32.             {
    33.                 if(hit.collider.tag == "Ground")
    34.                 {
    35.                     playerToPointDistance = Vector3.Distance(transform.position, hit.point);
    36.                     if(playerToPointDistance >= 1.0f)
    37.                     {
    38.                         canMove = true;
    39.                         targetPos = hit.point;
    40.                     }
    41.                 }
    42.             }          
    43.         } //if input mouse button down 0
    44.  
    45.         if (canMove == true)
    46.         {
    47.             anim.SetFloat("Walk", 1.0f);
    48.             Vector3 targetTemp = new Vector3(targetPos.x, transform.position.y, targetPos.z);
    49.             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetTemp - transform.position), 15.0f * Time.deltaTime);
    50.             playerMove = transform.forward * moveSpeed * Time.deltaTime;
    51.  
    52.             if (Vector3.Distance(transform.position, targetPos) <= 1f)
    53.             {
    54.                 canMove = false;
    55.             }
    56.         }
    57.         else
    58.         {
    59.             playerMove.Set(0f, 0f, 0f);
    60.             anim.SetFloat("Walk", 0f);
    61.         }
    62.     }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I tried your code. My scene was setup like this:
    1 quad for the ground
    1 capsule with character controller + your script.

    I noticed that my capsule had to be ~1m higher than the ground.

    So, when you get your target position, I added the hit.normal to it
    I also changed the distance check to a sqr magnitude, but that's up to you :)

    Code (csharp):
    1.  
    2. targetPos = hit.point;
    3. targetPos += hit.normal;
    4.  
    and
    Code (csharp):
    1.  
    2.  if ((targetPos - transform.position).sqrMagnitude <= .1f)
    3. {
    4.    canMove = false;
    5.    playerMove.Set(0f, 0f, 0f);
    6. }
    Hope that helps. :)
     
  3. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    Try replacing lines 36-40 with this:

    Code (CSharp):
    1. if(Vector3.Distance(transform.position, hit.point) >= 1.0f)
    2. {
    3.     canMove = true;
    4.     targetPos = hit.point;
    5. }
    6. else
    7. {
    8.     canMove = false;
    9. }
    And move it outside Input.GetMouseButtonDown(0) condition so it will check check distance every frame.

    Also get rid of lines 52-55 since they won't be needed anymore.
     
    Last edited: Feb 5, 2018
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I don't think that's correct, because those lines are only executed when the mouse is pressed.
     
  5. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    Good point. Updated the post.