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

[HELP] Charackter Controller on Rails.

Discussion in 'Scripting' started by zagari, Mar 21, 2015.

  1. zagari

    zagari

    Joined:
    Sep 30, 2014
    Posts:
    5
    Hi,

    The Basic idea of this CharacketerController is that the Player can move a Character in two directions, Left and Right.
    Mostly like you would do it for a 2.5D Game. At specific points in the Levels the Character will be automatically Rotated so that the Character is moving in Depth as well.

    The Problem I am stuck with is that, on Collision with other Objects the Player can be pushed of his Path.
    To solve this Problem you would usually use 2d Physics or lock the z axis, but as mentioned before I can't do this, because
    I also want the character to be able to move on the z axis.

    Attempts Solving this Problem:
    1. Shooting out Raycasts in the moving direction and stop the CharackterController from moving in this direction if he hits a obstacle he can't move over.
    This helped when moving the Character but not for ground collisions.
    If a Player jumps on a ledge that is not a straight Box collider and is not a 100% aligned with the Character,
    its still possible for the character to be drift off.

    2. After moving the CharackterController compare the current position to the position I wanted the character to moveTo.
    If the difference is bigger then 1% set the Character to its previous position and stop movement in this direction.
    This also worked to some degree, but if the Character is falling on an obstacle that would push the character to an unwanted Position, the Character will be stuck.

    I hope i described the Problem understandable.

    I would really appreciate any help, even if the result is just something like "you are totally insane for trying to do something like this :D".

    Thanks in advance,
    Sebastian
     
  2. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    Hi, have you tried splitting the character into 2 tranforms ?

    First one would only have the character controler and no visual components, does the movement along the path so forward and up/down.

    And second one would have visual components and do physics collisions with other objects that move it on the sides. It can be a child of the first or not depending on how you want rotations to behave, and use a constant smoothDamp to the first transform (or let the player go back on track, whatever suits your project).

    I hope it helps, good luck with your project :)
     
    Last edited: Mar 21, 2015
  3. zagari

    zagari

    Joined:
    Sep 30, 2014
    Posts:
    5
    Thaks for your quick resposne met44,

    If i do understand you correctly, you would disable the collision detection on the CharacterController and have a child object with a Rigidbody component on it right ? I am sure this helps when interacting with Enemies and what not.

    But right now i am pretty much at the Beginning of the Project and my concerns are more focus on traversing the environment at this point. Its the interaction with the static geometry that keeps pushing my Character of his path.
    I know why it happens and that i am trying to work against the normal physics behavior.

    I would like to show a video of an old Game that does exactly what i am trying to do.
    A good timecode would be 3m36s.


    greetings,
    Sebastian
     
    Last edited: Mar 21, 2015
  4. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    697
    I just looked at the code from my 2.5D game because I remember my character would slide. Put this code in Update. Try something like this:
    Code (JavaScript):
    1. // Adjust character sliding problem
    2.  
    3. transform.position.z = -1;
    4. if (transform.position.z > -1)
    5. {
    6.     transform.position.z = -1;
    7. }
    8.  
    9. if (transform.position.z < -1)
    10. {
    11.     transform.position.z = -1;
    12. }
     
  5. zagari

    zagari

    Joined:
    Sep 30, 2014
    Posts:
    5
    Thank you rmele09,

    but this works only if i dont want to move in z axis as well.
    Basically it's a 2.5d Sidescroller, but from time to time i want the Character to switch the direction,
    from only moving on a fixed z axis to a diagonal axis.
    So the charackter ist still moving on his local transform.forward. But given a y Rotation of 45°. And Thats where i cant wrap my head around, because now i cant just freez a specific axis anymore to solve the problem.
     
  6. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    Hehe, I use to love that game.

    You should probably use a spline tool to define the path, so you have a clean visual way to define it and can easily code the follow path script, then have your controler only handle up/down and forward/backward direction.

    When you move forward you increase the alpha along the spline then calculate the position that is corresponds to on the spline (there will be helpers shipping with the spline tool) and change the height with the one from the character. This way you're always updating to a position along the spline and at any height.

    Side note, platforms aligned with a path don't need a spline since you can use the bottom one and if you want to do "walls" that the player has to climb up by jumping multiple platforms up to the next path, use the bottom spline until you hit the upper spline trigger, at which point you switch the spline you're using to calculate the position.
     
    zagari likes this.
  7. zagari

    zagari

    Joined:
    Sep 30, 2014
    Posts:
    5
    Yes it's a hidden Gem :D.

    Thank you met44, i really appreciate your Help.
    I am already experimenting with this ( iTween pointOnPath), to move the Controller down a Spiral path. It did come into my mind to put splines around the whole level. So the Character would adjust the offset by himself when sliding.

    I just hoped there would be a way to completely prevent the Character from sliding.
     
  8. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    You could do the math yourself with Vector3.Project, path direction would be the normal, vector is the offset with previous position and final position is the projection perhaps ?
     
    zagari likes this.
  9. zagari

    zagari

    Joined:
    Sep 30, 2014
    Posts:
    5
    Thanks for that hint, i think i will use this for all movement from left to right, and bezier curves for the transitions onto different z axis or curved path.