Fast moving objects 2D game

Hey guys!

I find the official unity training - YouTube
and find bug.(look at the score)

I spent about 2 days to fix it and failed.
I find the script http://wiki.unity3d.com/index.php?title=DontGoThroughThings and try to rewrite to use in 2D. Failed again)

Please help me!

This is rewrite script:

using UnityEngine;
using System.Collections;

public class DontGoThroughThings2D : MonoBehaviour {

	public LayerMask layerMask; //make sure we aren't in this layer 
	public float skinWidth; //probably doesn't need to be changed 
	
	private float minimumExtent; 
	private float partialExtent; 
	private float sqrMinimumExtent; 
	private Vector2 previousPosition; 
	private Rigidbody2D myRigidbody; 
	
	
	//initialize values 
	void Awake() 
	{ 
		myRigidbody = GetComponent<Rigidbody2D>(); 
		previousPosition = myRigidbody.position;
		minimumExtent = Mathf.Min(Mathf.Min(GetComponent<Collider2D>().bounds.extents.x, GetComponent<Collider2D>().bounds.extents.y)); 
		partialExtent = minimumExtent * (1.0f - skinWidth); 
		sqrMinimumExtent = minimumExtent * minimumExtent; 
	} 
	
	void FixedUpdate() 
	{ 
		//have we moved more than our minimum extent? 
		Vector2 movementThisStep = myRigidbody.position - previousPosition; 
		float movementSqrMagnitude = movementThisStep.sqrMagnitude;
		
		if (movementSqrMagnitude > sqrMinimumExtent) 
		{ 
			float movementMagnitude = Mathf.Sqrt(movementSqrMagnitude);
			//RaycastHit2D hitInfo; 
			
			//check for obstructions we might have missed 
			if (Physics2D.Raycast(previousPosition, movementThisStep, movementMagnitude, 0, layerMask.value)) 
				myRigidbody.position = (movementThisStep/movementMagnitude)*partialExtent;
			Debug.DrawLine(myRigidbody.position, myRigidbody.position - previousPosition, Color.green);
		} 
		
		previousPosition = myRigidbody.position; 
	}
}

This is unitypackage https://www.dropbox.com/s/a3n1dalbc1k0k42/Hat%20Trick.unitypackage?dl=0

P.S. Sorry for my english and thank you for help!!

First thing to check is your rigidbody component in the scene. Check to see if it has its collision set to Continuious Dynamic rather than Dynamic or the other option. Unity suggests this for fast moving objects.

I just spent some time trying to solve this. For me what did the job is Edit/Project Settings/Physics or Physics2d and making Position Iterations 6 from 3(default).

I’m not sure how expensive this is but at least its working good :smiley: .