Search Unity

Strange Character Controller Interaction?

Discussion in 'Scripting' started by Zhialus, Jan 11, 2019.

  1. Zhialus

    Zhialus

    Joined:
    Jan 3, 2019
    Posts:
    16
    Hey there guys, I was wondering if someone could explain to me why this has been happening. I've spent a long time searching forum posts and the documentation but haven't found any conclusive information.

    In my script I was coding a "blink" mechanic that would shoot a raycast and move the Player to the hit position via setting their "transform.position = hit.point" (I can include script if needed) except the player wasn't budging from wherever they currently were, I tested it by setting other object's transform.position and it moved all of them just fine.

    The eventual work around I found, that I would like help understanding, is I had to first disable my player's Character Controller for the transform.position to have any effect on the player, something like this...

    controller.enabled=false;
    transform.position=hit.point;
    controller.enabled= true;


    I haven't been able to find any information on why the Character Controller might block transform.position from working. Explanations greatly appreciated.
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,546
    It's because of how the character controller deals with positional changes, you have to use the controller.Move() method and feed it a delta to the position you want the character like so:

    Vector3 moveDelta = hit.point - transform.position;
    controller.Move(moveDelta);


    You would still need to disable the collider and enable it again though so it doesn't get blocked by walls in the way. So either way...
     
    Zhialus likes this.
  3. Zhialus

    Zhialus

    Joined:
    Jan 3, 2019
    Posts:
    16
    Interesting, I was able to get that to work without disabling the controller, of course at the moment I'm just testing on a blank ground plane with the Player's origin point at the bottom so colliding with objects isn't an issue at the moment.

    So I'm still a bit new and don't have a full grasp on everything yet, but is "moveDelta" a kind of special term, because from my knowledge it seems like a regular Vector3 variable that could be replaced with something like targetLocation.

    I had tried that method before using like you said the controller.Move(); command, except I didn't have the part where you subtract the transform.position from the hit.point so maybe that was my missing part that was causing weird interactions.

    I'm still a tad confused on why the current position has to be subtracted from the hit point in order for it to send me to the correct location, is there a way you can explain that simply? Also I'm not sure about the collision part of it from what I know the raycast would already stop at any walls so having walls shouldn't get in the way? I've seen talk on CapsuleCast on other forums that I might look into as far as checking if the Player can fit in the destination area.