Jump to content
  • Advertisement
Sign in to follow this  
tamarind1001

Unity Best approach for handling collision detection in 3D platformer

This topic is 1007 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Helpwacko.png

 

 

I am following a Unity 2D platforming tutorial (one of Sebastian Lagues) and trying to extend this to the 3D world for a 3d platformer.  I am avoiding using the physics engine in order to keep the physics options as crazy as possible (and to learn). There is a character with a boundingbox, and all movement is via transform.translate. Currently in 2D the loop goes:

 

1. Check for a nonzero value for X,Y and (Z) from the velocity vector.

2. Send out rays from the extents of the bounding box in that direction (Z example below)

3. If there is a collision reduce the velocity in that direction

4. Set a flag that you have collided on that axis

5. At the original user input section, check for flags and disallow velocity on that axis (seems to stop jitter)

 

Thats great for 2D except that now my character can rotate, the local direction vector is not the same as the AABB which is fixed along world axes. I can translate the direction vector from local to world so the rays are being cast from the AABB in the right direction again but then the flag doesnt make sense and the jitter starts again.

 

So my question is: is this just the wrong approach altogether for a 3D platformer or will this work? If so how do I translate the info coming from bounding box rays to a character that can be rotated in any direction? If not what is the better approach?

 

thanks

void ZCollisions(ref Vector3 velocity) {
		
		float directionZ = Mathf.Sign (velocity.z);
		float rayLength = Mathf.Abs (velocity.z) + skinwidth;
		
		
		for (int i = 0; i < verticalRayCount; i++) {
			
			Vector3 rayOrigin = (directionZ == -1)?raycastOrigins.topLeft : raycastOrigins.ztopLeft;
			
			rayOrigin += Vector3.right * (verticalRaySpacing * i  + velocity.x);
			RaycastHit hitObject;
			bool  hit = Physics.Raycast(rayOrigin, Vector3.forward * directionZ, out hitObject,rayLength, collisionMask);
			
			
			if(hit)
			{	
				velocity.z = (hitObject.distance - skinwidth) * directionZ;/
				rayLength = hitObject.distance;
				collisions.front = directionZ == 1;
				collisions.back = directionZ == -1;
			}
		}
	}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!