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. Dismiss Notice

Physics free collision handling

Discussion in 'Scripting' started by Mr-LeoSantos, Jun 1, 2014.

  1. Mr-LeoSantos

    Mr-LeoSantos

    Joined:
    Mar 6, 2013
    Posts:
    18
    Hi,

    I'm trying to create my own simple character controller without using rigidbodies.

    While I can successfully anticipate a collision and prevent movement, I can't figure out a good way to "slide" off collisions - the character simply stops completely on any collision. I imagine I'd have to use the collision normal to update the post-collision position, but everything I tried failed.

    Here's a (very simple) sample code.I plan on adding ground detection, jumping, etc, only after getting the collisions right.

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var speed:float = 2;
    5. var colliderRadius:float = 1;
    6. var colliderCenter:Vector3;
    7. var colliderMask:LayerMask = -1;
    8.  
    9. private var vector:Vector3;
    10. private var hit:RaycastHit;
    11.  
    12. function Update () {
    13.  
    14.     if ( Input.GetAxis ("Horizontal") < 0 ) {
    15.         vector.x = ( -speed );
    16.     } else if ( Input.GetAxis ("Horizontal") > 0 ) {
    17.         vector.x = ( speed );
    18.     } else {
    19.         vector.x = 0;
    20.     }
    21.    
    22.     if ( Input.GetAxis ("Vertical") > 0 ) {
    23.         vector.y = ( speed );
    24.     } else if ( Input.GetAxis ("Vertical") < 0  ) {
    25.         vector.y = ( -speed );
    26.     } else {
    27.         vector.y = ( 0 );
    28.     }
    29.    
    30.     if  ( Physics.SphereCast( transform.position + colliderCenter, colliderRadius, vector.normalized, hit, vector.magnitude * Time.deltaTime, colliderMask ) ) {
    31.         //Collision would happen if I moved
    32.     } else {
    33.         //No Collision! Let's move.
    34.         transform.Translate( vector * Time.deltaTime );
    35.     }
    36.  
    37.  }
    38.  
    Any help is appreciated.Thanks!
     
  2. mu-kow

    mu-kow

    Joined:
    May 15, 2012
    Posts:
    106
  3. Mr-LeoSantos

    Mr-LeoSantos

    Joined:
    Mar 6, 2013
    Posts:
    18
    Thanks mu-kow!

    However, I'm looking for an "old school" approach, which means not using "real" physics. Something like Megaman or Super Mario NES movement, but in 3d. After, all, if I wanted true physics, I'd just use Unity's system. Sorry if that wasn't clear!

    The key thing for me is to provide movement per frame, not per fixed update, since the game is in Pixel Art and things need to move exactly 1 pixel per frame, 2 pixels per frame, etc. Also, combining Character Controllers and Colliders is a little disappointing right now, I wish the Character Controller simply used a regular collider and regular OnCollision functions...

    Any alternatives?

    Thanks!
    Leo.
     
  4. mu-kow

    mu-kow

    Joined:
    May 15, 2012
    Posts:
    106
    Well I still think the collision response info could be helpful. Converting any decimal to a whole number would help you keep pixel perfect.

    Buuuut, I know I've seen things about sonic the hedgehogs character controller.

    http://opensnc.sourceforge.net/home/index.php

    That might be helpful, or google around a bit more.