- Home /
Fast moving objects 2D game
Hey guys!
I find the official unity training https://www.youtube.com/watch?v=D5MqLcO6A8g 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!!
Answer by TheRobWatling · Apr 12, 2015 at 01:46 PM
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 set Collision Detection "Continuous". I try different $$anonymous$$in Penetration for penalty. It did not help.
Answer by Oriyus · May 16, 2015 at 04:40 PM
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 :D .
Your answer
Follow this Question
Related Questions
Colliders in a wall jut out 0 Answers
Collision with renderer.enabled? 0 Answers
Multiple Stacked Colliders only register for single object 0 Answers
Collider2D/RigidBody2D not working 1 Answer
2 isTrigger Colliders (2D) 1 Answer