Physics free collision handling

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.

#pragma strict

var speed:float = 2;
var colliderRadius:float = 1;
var colliderCenter:Vector3;
var colliderMask:LayerMask = -1;

private var vector:Vector3;
private var hit:RaycastHit;
 
function Update () {

	if ( Input.GetAxis ("Horizontal") < 0 ) {
		vector.x = ( -speed );
	} else if ( Input.GetAxis ("Horizontal") > 0 ) {
		vector.x = ( speed ); 
	} else {
		vector.x = 0;
	}
	
	if ( Input.GetAxis ("Vertical") > 0 ) {
		vector.y = ( speed );
	} else if ( Input.GetAxis ("Vertical") < 0  ) {
		vector.y = ( -speed );
	} else {
		vector.y = ( 0 );
	}
	
	if  ( Physics.SphereCast( transform.position + colliderCenter, colliderRadius, vector.normalized, hit, vector.magnitude * Time.deltaTime, colliderMask ) ) {
		//Collision would happen if I moved
	} else {
		//No Collision! Let's move.
		transform.Translate( vector * Time.deltaTime );
	}
 
 }

Any help is appreciated.Thanks!

http://www.myphysicslab.com/collision.html

That is a good resource for 2d collision response and may be helpful to you.

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.

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.