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

Question best way to handle the "character controller" component overwriting positions.

Discussion in 'Scripting' started by anonyperv, Apr 3, 2023.

  1. anonyperv

    anonyperv

    Joined:
    Nov 11, 2021
    Posts:
    3
    Hello, as per the title, the character controller (CC) is kind of bothering me. It has been more than 6 months since I created the movement system but since then, whenever I had to interact with the character in any way, the CC kept interfering in one way or another and I had to waste time figuring out that the CC was at fault, then find a solution.

    for movements, wasted more than a day figuring out why it wasn't working as expected with unity's animator before realizing it was the CC and I had to use a dirty workaround, to change the position of the player (after changing scenes for examples) the CC kept overwriting their positions, and again I used a workaround.

    Looking to buy animancer which appears to solve many of my issues with the default animator but I am afraid it the CC will give me even more troubles. If not, given my experience with it so far I will probably face more issues down the line.

    So my question would be, how to "use" the CC in more complex situations to avoid dirty workarounds which are definitely time-consuming and counterintuitive?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,008
    All you need to do is this:
    Code (CSharp):
    1. characterController.enabled = false;
    2. characterController.transform.position = somePosition;
    3. characterController.enabled = true;
    Doesn't seem dirty to me. And if three lines bother you so much, you can make it into an extension method.
     
  3. anonyperv

    anonyperv

    Joined:
    Nov 11, 2021
    Posts:
    3
    in that particular case it is more counterintuitive than dirty.

    But in the first scenario with the animator.
    Code (CSharp):
    1.  
    2.  private void OnAnimatorMove()
    3.         {
    4.             Vector3 velocity = animator.deltaPosition;
    5.             //gravity logic
    6.             velocity.y -= gravityMagnitude * Time.deltaTime;
    7.             controller.Move(velocity);
    8.         }
    9.         private void Update()
    10.         {
    11.            
    12.             playerRoot.transform.position += this.gameObject.transform.localPosition;
    13.             this.gameObject.transform.localPosition = Vector3.zero;
    14.         }
    15.  
    I had to place the controller on the child mesh instead of the parent and do something similar in order to get the player to move properly because the CC kept interfering. And disabling the CC every frame seemed needlessly expensive to use every frame on each character.

    don't remember the exact scenario because it happened months ago but this is how the movement logic ended up looking like.