- Home /
Unity 2d Collision Problem
My player glitches on other objects. example video;http://www.youtube.com/watch?v=tCmcIdgk8x4
Update İ use Triggers on diamonds all same colors are same rectangle Script pragma strict
var speed = 3.0; //This Data type is float.
var jumpSpeed = 50.0;
var grounded = true;
var customSkin : GUISkin;
var textureToDisplay : Texture2D;
private var score = 0;
function Start () {
}
function Update ()
{
var z : Vector3 = transform.right * Time.deltaTime * speed;
transform.Translate( z);
if (Input.GetButtonDown("Jump"))
{
Jump();
}
}
function Jump()
{
if (grounded == true)
{
rigidbody2D.AddForce(Vector3.up * jumpSpeed);
grounded = false;
}
}
function OnCollisionEnter2D(collision : Collision2D)
{
grounded = true;
}
function OnTriggerEnter2D(coin : Collider2D)
{
// Increment the global score variable declared in the by 1
// ++ is short hand for currentScore = currentScore + 1;
score++;
Destroy(coin.gameObject);
}
function OnGUI ()
{
GUI.skin = customSkin;
GUI.Label (Rect (-15, -12, 454 , 129),textureToDisplay);
GUI.Label (Rect (50, 20, 500, 200), score.ToString());
}
You don't give us much information to go on. Seeing your script would be helpful. Are you using triggers or colliders for the diamonds? Do you use the same for the rectangle?
Assu$$anonymous$$g this same script is on the rectangle, then it looks like you forgot to turn on the 'isTrigger' property on the collider.
i want to solid stop on collision with rectangle, not to take like coin/diamond, is it possible ?
Answer by robertbu · Feb 17, 2014 at 06:57 AM
The problem is because you are directly manipulating the transform for forward movement. When you do manipulate the transform, you make small 'teleports' forward. Therefore you teleport into the large rectangle and physics pushes you back out.
A fix (not without its own set of issues) is to move the object using the Rigidbody2D instead of manipulating the transform. For example, you can do this instead of the 'transform.Translate()'.
function FixedUpdate() {
rigidbody2D.velocity = Vector2(speed, 0);
}
thanks fixed the problem for now i should do more search on rigibody and fix the new issues.