- Home /
C# Creating a 2d boundary
I would like to create 2d boundary for a script. I had this idea of comparing the distance between the gameobject's transform.position to the vector2 boundary. I also compared it to the negative of the boundary to create a box like boundary that will prevent the gameobject from moving outside of it. But for some reason the if statement isn't working. Am I going about this correctly?
using UnityEngine;
using System.Collections;
public class BoundaryScript : MonoBehaviour {
public Vector2 boundary = new Vector2(2,2);
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Vector3.Distance(transform.position, boundary) < 1.0f && Vector3.Distance(transform.position, -boundary) < 1.0f){
Debug.Log(gameObject.name + "Is within the Boundary");
}
}
}
Answer by gabrielcac · May 27, 2015 at 12:45 AM
Your logic is a little flawed. Let me explain:
--|--.--|--
Supose this drawing above is your scene. The dot is your object and the "|" are the boundaries. The "-" are only there so make it easy for us to see the distance between the objects. The drawing only has one dimension, but its easier to understand whats going on this way.
With the boundary placed on the position 2, the outcome of your function would be that the object is outside the boundary because its distance from both the upper and lower border is greater than 1.
To fix this what you need to do is test if the distance between the object and both borders is equal or lesser than 4. If the borders were positioned in the position 3, then you should test if the distance between them is equal or lesser than 6, and if it was in the position 4, then you should test if the distances are equal or lesser than 8, and so on...
The distance you should test should aways be twice the value of the border position in relation to the origin (assuming your object starts its movement at the origin).
--|---.---|-- Distance from both borders is less then 3, so its inside
--|------.|-- Distance from left border is 6 and from right border is 0 3, so its inside
--|------|-.- Distance from left border is 7 and from right border is 1 3, so its outside
Now that I explained what is going on, let me tell you about the Boundary.Contains(Vector3 point) function :p
Just change your update function to
void Update () {
if(boundary.Contains(transform.position)){
Debug.Log(gameObject.name + "Is within the Boundary");
}
and everything should be ok.
Is there any way to adjust bounds.contains parameters? When I reach the ends of the boundary I can no longer move my gameobject.
The Contains()
function only tells you if the given point is inside the boundaries. I believe its your script that controls the gameObject that is stopping all movement once the gameObject is outside the boundaries. I would need to look at it to be sure.
Add your code here, or open a new question, I'll be glad to help.
$$anonymous$$ here's the rest of the code.
using System.Collections;
using UnityEngine;
class Grid$$anonymous$$ove : $$anonymous$$onoBehaviour {
public Bounds boundary = new Bounds(Vector3.zero,new Vector3(5,5,0));
public string inputString;
public float moveSpeed = 3f;
public float gridSize = 1f;
public Vector2 input;
public bool is$$anonymous$$oving = false;
public Vector3 startPosition;
public Vector3 endPosition;
public float t;
public float factor;
public void Update() {
if (!is$$anonymous$$oving) {
input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
if(boundary.Contains(transform.position)){
if (input != Vector2.zero) {
StartCoroutine(move(transform));
}
}
}
}
public IEnumerator move(Transform transform) {
is$$anonymous$$oving = true;
startPosition = transform.position;
t = 0;
endPosition = new Vector3(startPosition.x + System.$$anonymous$$ath.Sign(input.x) * gridSize,
startPosition.y + System.$$anonymous$$ath.Sign(input.y) * gridSize, startPosition.z);
if(input.x != 0 && input.y != 0) {
factor = 0.7071f;
} else {
factor = 1f;
}
while (t < 1f) {
t += Time.deltaTime * (moveSpeed/gridSize) * factor;
transform.position = Vector3.Lerp(startPosition, endPosition, t);
yield return null;
}
is$$anonymous$$oving = false;
yield return 0;
}
}