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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

When using a character controller I often am leaving the ground when

Discussion in 'Scripting' started by anydayzz, Feb 29, 2016.

  1. anydayzz

    anydayzz

    Joined:
    Feb 13, 2016
    Posts:
    111
    running towards an enemy that's rotating. How come? It doesn't matter if I make the capsule collider 10 in height and if I use a box collider the player gets shot into the air instead due to a bug with the character controller. This can only occur if the enemy is rotating (which he does to look at the player).
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    I believe that you will have to supply the code you are using for movement of the controller. We would need to see the math you are using as well as which statement you are using to make it move... Move or SimpleMove.
     
    anydayzz likes this.
  3. anydayzz

    anydayzz

    Joined:
    Feb 13, 2016
    Posts:
    111
    cC.Move(movementDirection * Time.deltaTime * moveSpeed + pushToGround);

    pushToGround is a small vector to make it easier to run down hills. Thank you for helping me.
     
  4. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    pushToGround should also be multiplied by Time.deltaTime if it is a speed or velocity. full code would help understand what it is better.. but if its litterally something you enter into the inspector and not calculated then yea you need to multiply it by delta time.
     
    anydayzz likes this.
  5. anydayzz

    anydayzz

    Joined:
    Feb 13, 2016
    Posts:
    111
    Code (CSharp):
    1. void Start()
    2. {
    3.     myTransform = transform;
    4.     pushToGround = new Vector3(0, -cC.stepOffset * 10, 0);
    5. }
    6.  
    7.  
    8. void Update()
    9. {
    10.     movementDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized;
    11.     cC.Move(movementDirection * Time.deltaTime * moveSpeed + pushToGround * Time.deltaTime);
    12.     myTransform.rotation = Quaternion.LookRotation(movementDirection);
    13. }
    14.  
    15.  
    It didn't make any difference when I added Time.deltaTime to pushToGround.

    Edit: it also happens when the enemy is standing still but very rarely. When using a box collider on the enemy and he's standing still it never happens.
     
    Last edited: Mar 1, 2016
  6. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    not that I would presume to state how and why you are using an odd vector for gravity...

    Code (csharp):
    1.  
    2. cC.Move(movementDirection * moveSpeed * Time.deltaTime + Physics.Gravity * Time.deltaTime * Time.deltaTime * 2);
    3.  
     
  7. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    Is this the only script driving the character controller? you may want to check the amounts of force your enemy is dealing when it turns or what even to keep itself in place.
     
    anydayzz likes this.
  8. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Maybe 2 more things to check:

    1) Do you have another collider on your player? That might cause weird CC behaviour in some cases as the CC might interact with other colliders on the player.
    2) As soon as your player leaves the ground, check if the target position's y-component has changed which would affect the direction of movement.
     
    anydayzz likes this.
  9. anydayzz

    anydayzz

    Joined:
    Feb 13, 2016
    Posts:
    111
    I explained why I use such an odd vector which is to be able to run down slopes without losing isGrounded and the amount it's set to is the lowest required one I found from testing. I tested your code and it had no effect but I could not use that anyway since now I can't run down slopes without the stutter.

    Yep this is the only script driving the cC as it's still very basic controls. I don't understand what you mean by checking the amounts of force. Do you mean the enemy is turning too fast? If I turn off the movement script for the enemy he just stands put in place.

    Even if the enemy does not turn I still leave the ground when running towards him from some angles.

    1) Nope there is no collider on the player except for the character controller.

    2) Neither of the coordinates change.

    Thank you to everyone for trying to help. I'm gonna go ahead and create a new project and see if the problem persists and I'll report back in a bit.
     
  10. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    That sounded as if you had a capsule collider (which you tried to exchange with a box collider) AND a CC component.

    What i actually meant is really no collider at all, as the CC already represents one itself.

    If that's not the case, it would be helpful if you provided more information about how he leaves the ground. Is it only that the CC's 'isGrounded' property returns false or does he really take off like a plane?
     
    anydayzz likes this.
  11. anydayzz

    anydayzz

    Joined:
    Feb 13, 2016
    Posts:
    111
    I was referring to the capsule collider of the enemy. I found the problem and it was the rotation the enemy made when looking at the player. I had forgot to set the x and z rotation to 0 for one of the rotation functions. Thank you everyone for helping me out.
     
    Suddoha likes this.