Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Help] Using Tenths Instead of Integers in Bounding Box Collision System

Discussion in '2D' started by MossCairn, Oct 20, 2019.

  1. MossCairn

    MossCairn

    Joined:
    Mar 14, 2019
    Posts:
    10
    Hello! I'm making progress on my collision system, and now the player can move and collide. However, I'm having some problems with the movement, which I am assuming are due to the way I'm handling the numbers. I've messed around with it for a bit and I'm not entirely sure what I'm doing wrong, but I figure I'm just missing something obvious.

    Here's the base collision detection script, this one seems to do it's job without any problems.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public abstract class Collide : MonoBehaviour
    6. {
    7.     //eventually going to replace this with a collection of all colliders in the scene or something for performance
    8.     //public GameObject self;
    9.     //Collider2D selfCollider; deprecated due to new overlapBox method
    10.  
    11.     //vector2 offset and position for use in MoveX/Y
    12.     Vector2 offset;
    13.     //Vector2 position;
    14.  
    15.  
    16.     //Establish hitbox sizes, this should be removed and passed in through the player class once that's set up
    17.     //private Vector2 idleHitbox = new Vector2(0.6f, 1.3f);
    18.  
    19.  
    20.     // Start is called before the first frame update
    21.     void Start()
    22.     {
    23.         //Vector3 position3d = self.transform.position;
    24.         //Vector2 position = position3d;
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         //in the final draft this should not be called through the collide, but through the classes inheriting from it
    31.  
    32.     }
    33.  
    34.     //this needs to be tweaked so that an offset can be passed in. This way, the Actor class can pass in the Sign of 'move', letting it check ahead of itself by a certain offset for collisions.
    35.     public bool CollideAt(Vector2 collider, Vector2 position, Vector2 offset)
    36.     {
    37.         //you'll want to edit this at some point so that the hitboxes can be passed in through the player class, because right now, this is just bad.
    38.         bool collisionTest = Physics2D.OverlapBox(position + offset, collider, 0);
    39.         //bool collisionTest = Physics2D.OverlapBox(self.transform.position/*+ offset*/, new Vector2(0.6f, 1.3f), 0); //original standalone collision detection code
    40.         Debug.Log(/*self.transform.position + " — " + */collisionTest);
    41.         return collisionTest;
    42.     }
    43. }
    And here's the Actor class script I'm testing, which inherits from Collide.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6. public class Actor : Collide
    7. {
    8.     private float xRemainder;
    9.     private float yRemainder;
    10.  
    11.     public delegate void Action(string logText);
    12.     public Action onCollide;
    13.  
    14.  
    15.  
    16.     Vector2 actorCollider = new Vector2(1, 1);
    17.     Vector2 actorPosition;
    18.  
    19.  
    20.     // Start is called before the first frame update
    21.     void Start()
    22.     {
    23.         onCollide = OnCollide;
    24.         onCollide("PHWEEE");
    25.  
    26.         ////This is the groundwork for the movement system. We take the 3d pos, pass it to a varaible which modifies it, then return the modified version to move.
    27.         //this.transform.position = new Vector3(0, 0, 0);
    28.         actorPosition = this.transform.position;
    29.         //actorPosition.x += 0.8f;
    30.         //this.transform.position = actorPosition;
    31.  
    32.  
    33.         //MoveX(0.1f, OnCollide);
    34.  
    35.  
    36.     }
    37.  
    38.     // Update is called once per frame
    39.     void Update()
    40.     {
    41.         //Vector3 position3d = this.transform.position;
    42.         //actorPosition = position3d;
    43.  
    44.         if (Input.GetKey(KeyCode.RightArrow))
    45.         {
    46.             //    Debug.Log("right");
    47.             //    actorPosition = this.transform.position;
    48.             //    actorPosition.x += 0.1f;
    49.             //    this.transform.position = actorPosition;
    50.             MoveX(1, OnCollide);
    51.         }
    52.  
    53.         if (Input.GetKey(KeyCode.LeftArrow))
    54.         {
    55.             //    Debug.Log("left");
    56.             //    actorPosition = this.transform.position;
    57.             //    actorPosition.x += -0.1f;
    58.             //    this.transform.position = actorPosition;
    59.             MoveX(-1, OnCollide);
    60.         }
    61.  
    62.         if (Input.GetKey(KeyCode.UpArrow))
    63.         {
    64.             //    Debug.Log("right");
    65.             //    actorPosition = this.transform.position;
    66.             //    actorPosition.x += 0.1f;
    67.             //    this.transform.position = actorPosition;
    68.             MoveY(1, OnCollide);
    69.         }
    70.  
    71.         if (Input.GetKey(KeyCode.DownArrow))
    72.         {
    73.             //    Debug.Log("left");
    74.             //    actorPosition = this.transform.position;
    75.             //    actorPosition.x += -0.1f;
    76.             //    this.transform.position = actorPosition;
    77.             MoveY(-1, OnCollide);
    78.         }
    79.     }
    80.  
    81.     public void MoveX(float amount, Action onCollide)
    82.     {
    83.         int intAmount = (int)amount * 10;
    84.  
    85.         xRemainder += amount;
    86.         int move = Math.Sign(xRemainder);
    87.         Debug.Log(move);
    88.  
    89.         if (move != 0)
    90.         {
    91.             xRemainder -= move;
    92.             int sign = Math.Sign(move);
    93.             Debug.Log("Sign: " + sign);
    94.             //float signInUnits = sign / 10;
    95.  
    96.             while (move != 0)
    97.             {
    98.                 //if (!collideAt(solids, Position + new Vector2(sign, 0))
    99.                 //{
    100.                 //    //No solid immediately beside us
    101.                 //    Position.X += sign;
    102.                 //    move -= sign;
    103.                 //}
    104.                 //else
    105.                 //{
    106.                 //    //Hit a solid!
    107.                 //    if (onCollide != null)
    108.                 //        onCollide();
    109.                 //    break;
    110.                 //}
    111.  
    112.                 if (!CollideAt(actorCollider, actorPosition, new Vector2(sign, 0)/*offset*/))
    113.                 {
    114.                     //No solid immediately beside us
    115.                     actorPosition.x += sign;
    116.                     move -= sign;
    117.                     this.transform.position = actorPosition;
    118.                     Debug.Log("Actor Position: " + actorPosition);
    119.                 }
    120.                 else
    121.                 {
    122.                     Debug.Log("Collision Detected");
    123.                     this.transform.position = actorPosition;
    124.                     break;
    125.                 }
    126.             }
    127.         }
    128.     }
    129.  
    130.     public void MoveY(float amount, Action onCollide)
    131.     {
    132.         int intAmount = (int)amount * 10;
    133.  
    134.         yRemainder += amount;
    135.         int move = Math.Sign(yRemainder);
    136.         Debug.Log(move);
    137.  
    138.         if (move != 0)
    139.         {
    140.             yRemainder -= move;
    141.             int sign = Math.Sign(move);
    142.             Debug.Log("Sign: " + sign);
    143.             //float signInUnits = sign / 10;
    144.  
    145.             while (move != 0)
    146.             {
    147.                 //if (!collideAt(solids, Position + new Vector2(sign, 0))
    148.                 //{
    149.                 //    //No solid immediately beside us
    150.                 //    Position.Y += sign;
    151.                 //    move -= sign;
    152.                 //}
    153.                 //else
    154.                 //{
    155.                 //    //Hit a solid!
    156.                 //    if (onCollide != null)
    157.                 //        onCollide();
    158.                 //    break;
    159.                 //}
    160.  
    161.                 if (!CollideAt(actorCollider, actorPosition, new Vector2(0, sign)/*offset*/))
    162.                 {
    163.                     //No solid immediately beside us
    164.                     actorPosition.y += sign;
    165.                     move -= sign;
    166.                     this.transform.position = actorPosition;
    167.                     Debug.Log("Actor Position: " + actorPosition);
    168.                 }
    169.                 else
    170.                 {
    171.                     Debug.Log("Collision Detected");
    172.                     this.transform.position = actorPosition;
    173.                     break;
    174.                 }
    175.             }
    176.         }
    177.     }
    178.  
    179.  
    180.     public void OnCollide(string logText)
    181.     {
    182.         //Debug.Log(logText);
    183.     }
    184.  
    185. }
    186.  
    The way it's currently set up, it works fine if you move in integers, but anything less than that and it starts acting up. I need to change it so that everything moves in tenths, but I'm not entirely sure how to go about doing this.

    EDIT: If it helps, I'm going off of this resource (https://mattmakesgames.tumblr.com/post/127890619821/towerfall-physics), and creating the missing pieces based on the descriptions of what they need to do.
     
    Last edited: Oct 20, 2019
  2. MossCairn

    MossCairn

    Joined:
    Mar 14, 2019
    Posts:
    10
    (Bump)
    I'm not sure how to approach this, if anyone has any advice it would be a huge help. Thank you!
     
  3. MossCairn

    MossCairn

    Joined:
    Mar 14, 2019
    Posts:
    10
    I found a workaround which eliminates the need to do any of this, now I can do nice and logical integer collisions.