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 esitoinatteso · Feb 11, 2015 at 06:47 PM · recty-axisz-axisswapalternative

Rect with Z instead of Y or similar alternatives

Hi there! I'm editing this question...

I came to understand that the Rect class has a very valuable Function called Contains, which I'd like to use.

My problem is that the matrix I use to build those Rects has X,Z coordinates, and when I pass them to a Rect my Zs become Ys.

I just need a Class that holds 4 Vector3s as vertices, without meshes or other stuff. I could build up my own one but that Cointains function would require me some extra work ( since my skills are halfway to acceptability).

Is there something else I can use to achieve this goal or maybe a way to build Rects with coordinates I want? Maybe inheriting from Rect/similar and then...? Thanks for the patience!

EDIT + added comments

This is what I've got so far, if you're interested in commenting ( I'd like some suggestions about the code because I'm a self taught "programmer" and I don't know if my way is canon or utterly twisted):

 using UnityEngine;
 using System.Collections;
 [System.Serializable]
 
 public class RectZ{
 
     //Do you like Rects? Would you like them to be X,Z and Y,Z too? Then you'll love this!
 
     public float wMIn;                        //minimum width... sort of base
     public float wMAx;                        //maximum width
     public float hMIn;                        //minimum height
     public float hMAx;                        //maximum height
     public Vector3 center;                    //the center of this RectZ
     public Vector3 size;                    //the size
     public Vector3 extents;                    //..........
     public Vector3 worldCoordinates;        //.... pretty straightforward ain't it?
 
 
     //SO...
     //To build a new RectZ from another script, you pass these parameters inside its constructor...
     public RectZ (float _UpMinVertex, float _UpMaxVertex, float _DownMinVertex, float _DownMaxVertex, int _1xy_2xz_3yz){
 
         wMIn = _UpMinVertex;
         wMAx = _UpMaxVertex;
         hMIn = _DownMinVertex;
         hMAx = _DownMaxVertex;
 
         //I use an INT as a BOOL to understand which axes you want... this makes the call easier
         if(_1xy_2xz_3yz == 1){
             size = new Vector3(wMAx-wMIn,hMAx-hMIn,0);
             extents = new Vector3(size.x/2,size.y/2,0);
             center = new Vector3(size.x-extents.x,size.y-extents.y,0);
             worldCoordinates = new Vector3(wMIn+center.x,hMIn+center.y,0);
         }
         if(_1xy_2xz_3yz == 2){
             size = new Vector3(wMAx-wMIn,0,hMAx-hMIn);
             extents = new Vector3(size.x/2,0,size.z/2);
             center = new Vector3(size.x-extents.x,0,size.z-extents.z);
             worldCoordinates = new Vector3(wMIn+center.x,0,hMIn+center.z);
         }
         if(_1xy_2xz_3yz == 3){
             size = new Vector3(0,wMAx-wMIn,hMAx-hMIn);
             extents = new Vector3(0,size.y/2,size.z/2);
             center = new Vector3(0,size.y-extents.y,size.z-extents.z);
             worldCoordinates = new Vector3(0,wMIn+center.y,hMIn+center.z);
         }
     }
 }
 
 

To call it I just type things like this:

 RectZ cell = new RectZ(vertex.x,vertex.x+tileSize,vertex.z,vertex.z+tileSizeZ,2);

I've skipped other functions of RectZ because I haven't tested them yet, but I'll update them as I progress. This thing, called using my matrix X,Z, works!

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 giulio-pierucci · Feb 11, 2015 at 08:07 PM

There are a Bounds object in Unity that you may use if fit your needs:

http://docs.unity3d.com/ScriptReference/Bounds.html

Otherwise, you can write a custom class like this example:

public class foo { public vector3[] vertices;

  public bool Contains(vector3 v)
  {
       ..... implement contains logic here
  }

}

Comment
Add comment · Show 3 · 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 esitoinatteso · Feb 11, 2015 at 09:04 PM 0
Share

Hi giulio! I've been coding this class so far.

Since I lack basics I don't know how to use it from other scripts... ideally, I wanted something like all usual calls ( Vector3.x for example, or Physics.Raycast() ).

I'm getting an error about things in this class not being static. To solve the issue I watched this tutorial but I can't grasp the logic.

These are chunks of the custom class I've built:

 using UnityEngine;
 using System.Collections;
 
 public class RectZ:$$anonymous$$onoBehaviour{
 
     public float w$$anonymous$$In;
     public float w$$anonymous$$Ax;
     public float h$$anonymous$$In;
     public float h$$anonymous$$Ax;
     public Vector2 center;
     public Vector2 size;
     public Vector2 extents;
 
     
     public RectZ NewRect(float _Up$$anonymous$$inVertex, float _Up$$anonymous$$axVertex, float _Down$$anonymous$$inVertex, float _Down$$anonymous$$axVertex){
         w$$anonymous$$In = _Up$$anonymous$$inVertex;
         w$$anonymous$$Ax = _Up$$anonymous$$axVertex;
         h$$anonymous$$In = _Down$$anonymous$$inVertex;
         h$$anonymous$$Ax = _Down$$anonymous$$axVertex;
         size = new Vector2(w$$anonymous$$Ax-w$$anonymous$$In,h$$anonymous$$Ax-h$$anonymous$$In);
         extents = new Vector2(size.x/2,size.y/2);
         center = new Vector2(size.x-extents.x,size.y-extents.y);
 
     }
 
     public bool Is2dPointBetween (float pointCoord1, float pointCoord2, RectZ rect){
 
         
     }
 }
 

I've skipped the boring stuff. The problem is I can't call NewRect from another script to make a new RectZ, which should be a copycat of Rect struct, except it can work with Z.

avatar image esitoinatteso · Feb 11, 2015 at 09:06 PM 0
Share

by the way, Rect is sealed... that's sad! I wanted to inherit from it!

avatar image esitoinatteso · Feb 11, 2015 at 09:35 PM 0
Share

Hi again! I've fixed the problem stated above!

Thanks for pointing Bounds out though, if you don't $$anonymous$$d, I'd like you to elaborate the differences between using Bounds tweaked to suit my needs and the Class I've built. I'll edit my question to include the new code.

That'd really help me improve my bizarre and unreliable program$$anonymous$$g skills!

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

20 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 avatar image

Related Questions

Rotate z and y together? 1 Answer

I have a serious problem! 1 Answer

Enemy Follow Disable Y Axis 1 Answer

How do I make an angle with the X axis, Y axis, and Z axis and not rotate AROUND the axis? 0 Answers

How to stop rotation in x and y axis??? 2 Answers


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