Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ochreous · May 26, 2015 at 09:30 PM · c#gameobjectvector3distanceboundary

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");
     }
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ochreous · May 27, 2015 at 02:41 AM 0
Share

Is there any way to adjust bounds.contains parameters? When I reach the ends of the boundary I can no longer move my gameobject.

avatar image gabrielcac · May 27, 2015 at 02:51 AM 0
Share

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.

avatar image gabrielcac · May 27, 2015 at 11:10 AM 0
Share

Add your code here, or open a new question, I'll be glad to help.

avatar image Ochreous · May 27, 2015 at 06:20 PM 0
Share

$$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;
     }
 }



Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotating a Vector3 in Instantiation 1 Answer

Destroying Game Object On MouseButtonDown + Distance Collision 0 Answers

How to get a "Traveled" vector point along a path with some width? 1 Answer

Questions creating a gameObject boundary 1 Answer

How do I find the farthest verts of a mesh relative to its object position and store them in an array 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges