Search Unity

Brick Shooter (tutorial) question

Discussion in 'Community Learning & Teaching' started by a1604, May 16, 2013.

  1. a1604

    a1604

    Joined:
    Apr 5, 2013
    Posts:
    5
  2. MoiFou

    MoiFou

    Joined:
    Aug 31, 2013
    Posts:
    3
    I tried to come up with a solution for this scenario, and I ended up with a new question. :(

    I tried the following code in a script that I attached to the physics_powerCube prefab.
    I thought this code would detect when individual physics_powerCube objects had been moved, either by being hit with the physics_samoflange, or by being pulled down by other cubes:


    bool isTumbled = false;
    public Vector3 startPos;
    public Vector3 currentPos;

    // Use this for initialization
    void Start () {
    startPos = transform.position;
    }

    // Update is called once per frame
    void Update () {

    if (!isTumbled)
    {

    currentPos = transform.position;

    //Debug.Log(GetInstanceID() + "startPos = " + startPos.ToString());
    //Debug.Log(GetInstanceID() + "currentPos = " + currentPos.ToString());

    if (!(currentPos.Equals(startPos)))
    {
    isTumbled = true;
    //Debug.Log(GetInstanceID() + ": isTumbled is true");
    //ToDo: Increase score.
    }
    }

    }



    But, guess what? The cubes settle after the Start() event has already been called. So currentPos.Equals(startPos) is never true in the Update() function. So, I tried something like this:


    bool isTumbled = false;
    public Vector3 startPos;
    public Vector3 currentPos;

    // Use this for initialization
    void Start () {
    startPos = transform.position;
    }

    // Update is called once per frame
    void Update () {

    if (!isTumbled)
    {

    currentPos = transform.position;

    //Debug.Log(GetInstanceID() + "startPos = " + startPos.ToString());
    //Debug.Log(GetInstanceID() + "currentPos = " + currentPos.ToString());

    if ((Mathf.Abs(currentPos.x - startPos.x) > 0.3f)
    (Mathf.Abs(currentPos.y - startPos.y) > 0.3f)
    (Mathf.Abs(currentPos.z - startPos.z) > 0.3f))
    {
    isTumbled = true;
    //Debug.Log(GetInstanceID() + ": isTumbled is true");
    //ToDo: Increase score.
    }
    }

    }



    I tried different values than 0.3f, but the results were not reliable. So I tried this:


    bool isTumbled = false;

    void OnCollisionEnter(Collision col)
    {
    if (!isTumbled)
    {
    if (!(col.gameObject.name.Contains("powerCube")))
    {
    Debug.Log(GetInstanceID() + ": has collided with " + col.gameObject.name.ToString());
    isTumbled = true;
    //ToDo: Increase score.
    }
    }
    }



    I used the if (!(col.gameObject.name.Contains("powerCube"))) line because the settling issue was triggering the OnCollisionEnter() event, because the powerCubes were colliding with each other as they settled. Annoying!! I thought the if line would filter out collisions with other powerCubes, leaving only collisions with the samoflange and the floor_* objects. This produced a bit more reliable results.

    But, wtf is with the settling? How do you prevent the settling? (That's the new question I ended up with.) The meshes appear to be butted up right against each other. So I thought, ok, maybe the size of the box collider needs to be tweaked, but whether larger, smaller, or the same as the mesh, there was still settling. What am I missing? I think if the settling was eliminated, any of these chunks of code would be more reliable.

    Here's a video of the settling: [video=youtube_share;CRKgdCNJPXU]http://youtu.be/CRKgdCNJPXU
     
    Last edited: Sep 9, 2013
  3. raiden

    raiden

    Joined:
    Feb 8, 2009
    Posts:
    333
    Untested, but what about invisible trigger colliders with each block, detecting each block in OnTriggerStay(), then when a block leaves the trigger, OnTriggerExit(), it registers a score? That would eliminate all the position checking.

    -Raiden
     
  4. MoiFou

    MoiFou

    Joined:
    Aug 31, 2013
    Posts:
    3
    Bah! Perfect!
     
  5. layinka

    layinka

    Joined:
    Jun 26, 2013
    Posts:
    6
    @raiden ,Please can you give a sample of what you mean?
     
  6. raiden

    raiden

    Joined:
    Feb 8, 2009
    Posts:
    333
    @layinka

    Sorry for getting back so late, but I've created a small project demonstrating how to do this.

    https://drive.google.com/open?id=0B8CuPAuHoXtUeGxJRVRJZXB6OVE

    I actually use OnCollisionEnter, and it works well. I added simple crosshairs to help visually know where your shooting the ball.

    Let me know if you have any questions.

    Enjoy :)

    -Raiden