- Home /
Approach to Sensing in a 4x4 Grid
Hello all, I have a game in which I need to determine which of 16 (4x4) irregular boxes a player is in. By irregular, I mean that they have different dimensions. i was wondering the approaches that I could take to sense which box the player was in. Should I use playerposition, oncollision, etc. Any ideas? Thanks for the help, and good day to you all.
--Edit--
so I have this code:
using UnityEngine; using System.Collections;
public class hammerhit : MonoBehaviour {
public Rigidbody mole;
public Vector3 molevector3;
public Collider b1;
public Collider b2;
public Collider b3;
public Collider b4;
public Collider b5;
public Collider b6;
public Collider b7;
public Collider b8;
public Collider b9;
// Update is called once per frame
void Update ()
{
molevector3 = mole.position; //sets molevector3 to mole position in realtime
//Debug.Log (molevector3); //returns molevector3, the mole position
//Debug.Log (molevector3.x); //returns mole x postition
}
void OnCollisionEnter (Collision col1)
{
if (col1.collider == b1 || b2 || b3 || b4 || b5 || b6 || b7 || b8 || b9)
{
// Destroy (col1.gameObject); }
}
}
and I attach it to the camera, which is my player. However, when the destroy (col1.gameobject) line is called, it destroys anything it touches, not just b1, b2, etc. Ideas?
Depends on what exactly you want. If a collider collides with another collider, it doesn't meant it is inside it. If the player must always be in just one of the squares, you should probably use player position and Rect.Contains() for example.
Answer by RobAnthem · Dec 09, 2016 at 06:19 AM
OnCollision is your best bet, you can make a player property called something like public int currentGrid; Then on collision just set the variable to whatever section you want. Trying to calculate the coordinates is only easy if your grid is consistent size.
so I have this code:
using UnityEngine;
using System.Collections;
public class hammerhit : $$anonymous$$onoBehaviour {
public Rigidbody mole;
public Vector3 molevector3;
public Collider b1;
public Collider b2;
public Collider b3;
public Collider b4;
public Collider b5;
public Collider b6;
public Collider b7;
public Collider b8;
public Collider b9;
// Update is called once per frame
void Update ()
{
molevector3 = mole.position; //sets molevector3 to mole position in realtime
//Debug.Log (molevector3); //returns molevector3, the mole position
//Debug.Log (molevector3.x); //returns mole x postition
}
void OnCollisionEnter (Collision col1)
{
if (col1.collider == b1 || b2 || b3 || b4 || b5 || b6 || b7 || b8 || b9)
{
// Destroy (col1.gameObject);
}
}
}
and I attatch it to the camera, which is my player. However, when the destroy (col1.gameobject) line is called, it destroys anything it touches, not just b1, b2, etc. Ideas?
@RobAnthem do you have any ideas based on my code that I sent?
I'd either add a script to each box, or simply
if(b1.bounds.Contains(mole.position))
{
molePosition = 1;
}
Your answer
Follow this Question
Related Questions
Create a grid on a cube? 0 Answers
Grid-based building system not working 1 Answer
grid-based block placement system with touch input 1 Answer
moving down blocks 0 Answers