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
3
Question by karl_ · Oct 09, 2013 at 04:23 PM · vector3vector2conversion

Prevent implicit cast from Vector3-Vector2

Unity provides implicit casts between most of the Vector classes. This is nice in most situations, but is making debugging a nightmare for some of my code. I wrote conversion methods that accept an Axis parameter which allows the user to specify which values to use when converting between Vectors. Ex:

 Vector2 myVec2 = new Vector3(1, 2);
 Vector3 myVec3 = myVec2.ToVector3(Axis.Y, 0f);    // myVec3 == [1, 0, 2]
 Vector2 andBack = myVec3.ToVector2(Axis.Y);       // now is [1, 2] again
 Vector2 implCst = myVec3;                         // I'd like this to throw an exception


The trouble is that if I forget to cast, or incorrectly apply math between Vector3 and Vector2 (which is allowed because of implicit casting) variables I get incorrect values.

I'd like to disable implicit conversions for just the Vector classes. Is this possible?

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
1
Best Answer

Answer by Jamora · Oct 09, 2013 at 05:25 PM

I don't think it's possible with C#, with Boo and UnityScript you could possibly do some metaprogramming magic to make it work. I wouldn't know.

Instead of changing Unity's implementation, you should create your own implementation of Vector2 & 3. You could then create new MyVectors from Unity's Vectors, operate on those, then convert them back to Unity's Vectors.

 public struct MyVector2{
 
     float x,y;
 
     public MyVector2(Vector2 v2){
         x = v2.x;
         y = v2.y;
     }
     public static MyVector2 operator *(float d, MyVector2 a)
     {
         return new MyVector2(a.x * d, a.y * d);
     } 

     public static implicit operator MyVector2(Vector3 v)
     {
         return new MyVector2(v.x, v.z);
     }
 
     public static implicit operator MyVector2(Vector2 v)
     {
         return new MyVector2(v.x, v.y);
     }

     public static implicit operator Vector3(MyVector2 v)
     {
         return new Vector3(v.x, 0f, v.y);
     }

     //etc...
 
 }

Now you just need to start operating on your own Vector implementation:

 MyVector2 vec2 = transform.position;
 transform.position = vec2 * 5.5f;
Comment
Add comment · Show 2 · 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 karl_ · Oct 11, 2013 at 03:29 PM 0
Share

This was my initial thought, but I was hoping for an easy #define DISABLE_I$$anonymous$$PLICIT_CAST or something to that effect. I noticed an ENABLE_DUC$$anonymous$$_TYPING define in the Editor logs, but I'm guessing that only applies to UnityScript/Boo.

avatar image sethillgard · Aug 18, 2014 at 04:52 AM 0
Share

I wonder if this has performance implications for mobile games on low end devices. Anyone has any data on this?

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

17 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

Related Questions

Project a Vector3 onto a plane, orthographically 2 Answers

Rotating A Vector2 Input Into A Vector3 1 Answer

Vector1, Vector2, Vector3? 1 Answer

Convert Vector2 to Vector3 based on the ground's height? 1 Answer

Vector2 Normalize ? 3 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