How to impale and stack targets correctly according to the collider and its coordinate?

Posted by David Dimalanta on Game Development See other posts from Game Development or by David Dimalanta
Published on 2014-03-13T03:09:40Z Indexed on 2014/06/09 3:44 UTC
Read the original article Hit count: 420

I'm making another simple game, a catch game, where a spawning target game object must be captured using a skewer to impale it. Here how:

  1. At the start, the falling object (in red) will fall in a vertical direction (in blue) enter image description here

  2. When aimed properly, the target will fall down along the line of the skewer. (in blue) Then, another target is spawned and will fall vertically. (in red) enter image description here

  3. When aimed successfully again in a streak, the second target will fall along the skewer and stacked. Same process over and over when another target is spawned. enter image description here

However, when I test run it on the scene tab in Unity, when impaled several targets, instead of a smooth flow and stacking it ended up overlaying it instead of stacking it up like a pancake. Here's what it look like: enter image description here

As I noticed when reaching the half-way of my progress, I tried to figure out how to deal with collider bodies without sticking each other so that it will actually stack like in the example of the image at no. 3. Here's the script code I added in the target game object:

using UnityEngine;
using System.Collections;

public class ImpaleStateTest : MonoBehaviour {

    public GameObject target;
    public GameObject skewer;

    public bool drag = false;

    private float stack;
    private float impaleCount;

    void Start () {
        stack = 0;
        impaleCount = 0;
    }

    void Update () {
        if(drag) {
            target.transform.position = new Vector3 (DragTest.dir.transform.position.x, DragTest.dir.transform.position.y - 0.35f, 0);
            target.transform.rotation = DragTest.degrees;
            target.rigidbody2D.fixedAngle = true;
            target.rigidbody2D.isKinematic = true;
            target.rigidbody2D.gravityScale = 0;

            if(Input.GetMouseButton(0)) {
                Debug.Log ("Skewer: " + DragTest.dir.transform.position.x);
                Debug.Log ("Target: " + target.transform.position.x);
            }
        }
    }

    void OnTriggerEnter2D(Collider2D collider) {
        impaleCount++;
        Debug.Log ("Impaled " + impaleCount + " time(s)!");
        drag = true;
        audio.Play ();
    }

}

Aside from that, I'm not sure if it's right but, the only way to stick the impaled targets while dragging the skewer left or right is to get the X coordinates from the skewer only. Is there something else to recommend it in order to improve this behavior as realistic as possible? Please help.

© Game Development or respective owner

Related posts about c#

Related posts about collision-detection