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 Team_26 · May 13, 2014 at 08:04 PM · transformpositionvector3if

Vector3 add or subtract

Ok. So, I have a problem with a != operator. It's because I want to make a script that's checking if operation:

if(Vector3 != transform.position) //variable instead Vector3

It's simple and there's no compilation fails. But now I want make something like this:

if(Vector3 > (transform.position + 1) || Vector3 < (transform.position - 1))

But I know that I can't add or subtract from transform.position. My question is: Can I subrtact (or add) 1 from all values of trasform.position (or other Vector3) by one command?

Comment
Add comment · Show 2
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 Kiwasi · May 13, 2014 at 09:25 PM 0
Share

You will get better answers if you can tell us why you want to compare vectors. What game effect are you trying to achieve?

avatar image Team_26 · May 14, 2014 at 02:26 PM 0
Share

I'm going to switch off rigidbody.is$$anonymous$$inematic of block when the position of other block isn't equal StartPos (start position). But I know that the position of this other block varies between 0.5, because he's using gravity and it's is on the other block. Sorry for my bad English, I hope that you know what I mean.

5 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by CodeElemental · May 13, 2014 at 08:09 PM

The closest solution to what you're trying to acheive is

 if(Vector3 > (transform.position + Vector.one) || Vector3 < (transform.position - Vector.one))


See more here

Comment
Add comment · Show 8 · 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 AngryCSharpNerd · May 13, 2014 at 08:44 PM 0
Share

This guy's got it. I even learned something new.

avatar image Kiwasi · May 13, 2014 at 09:24 PM 0
Share

The > operator for Vector3 is undefined. This code should cause an error. Check out the script reference for Vector3

avatar image CodeElemental · May 13, 2014 at 09:49 PM 0
Share

It was meant as a meta-code from the OP. Did not have to downvote :|

avatar image Kiwasi · May 13, 2014 at 10:10 PM 0
Share

Okay, I'll be nice. The Vector3.one is a good contribution.

avatar image CodeElemental · May 14, 2014 at 02:23 PM 1
Share

for that you would need to compare them directly I'm afraid. So what you suggested is the actual solution

 if (StartPos.x > (transform.position.x + 1))
 {
  // startpos.x is greater than transform.position.x + 1
 }
Show more comments
avatar image
3
Wiki

Answer by Kiwasi · May 13, 2014 at 09:22 PM

If you are trying to find out if a given vector is within 1 of transform.position then use this code

 if((myVector3-transform.position).sqrMagnitude > 1){

If you are trying to compare vector if vector lengths are within 1 use this

 if(myVector3.sqrMagnitude > transform.position.sqrMagnitude + 1 || myVector3.sqrMagnitude < transform.position.sqrMagnitude - 1){

Couple of points to note

Vectors contain information about distance and direction

Vector3.magnitude gives a float that represents the length of the vector.

Vector3.sqrMagnitude fives a float that represents the length of the vector squared. (Faster and can often be used instead of magnitude).

Vector3 + 1 has no meaning (1, 2, 4) + 1 = ???

Vector3 > Vector3 has no meaning (1, 0, 4) > (2, 2, 1) = ???

Vector3 + Vector3 is defined (1,2,3) + (1,2,3) = (2,4,6)

Vector3 - Vector3 is defined (1,2,3)-(1,0,2) = (0,2,1)

Comment
Add comment · Show 1 · 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 Team_26 · May 14, 2014 at 02:51 PM 0
Share

Thank for your reply. I don't use your suggestion, but it can be useful in future. Big thank for you :)

avatar image
1

Answer by Bunny83 · Jul 08, 2018 at 09:42 AM

The easiest way to do an axis aligned bounding box test is to use Unity's Bounds struct.

 if(new Bounds(transform.position, Vector3.one*2).Contains(yourTestVector))

This will return true if "yourTestVector" is within a bounding box with size 2 around "transform.position". So a box that goes from x-1,y-1,z-1 up to x+1,y+1,z+1

Comment
Add comment · 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
0

Answer by Tsilliev · Jul 08, 2018 at 06:22 AM

 if(transform.position.x == obj.transform.position.x + 1 && transform.position.y == obj.transform.position.y && transform.position.z == obj.transform.position.z)
 {
 // obj is ahead of this transform by +1 on the X axis
 
 }
Comment
Add comment · Show 1 · 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 Bunny83 · Jul 08, 2018 at 09:38 AM 0
Share

Directly comparing floating point values for equality is never a good idea due to possible rounding errors. Besides that the OP wanted to check a range and not just a single position.

avatar image
0

Answer by Legendmir · Oct 23, 2019 at 06:51 PM

if your checking if somthing within a certain distance would Vector3.Distance not be better?

Honest question here so please tell me if there is a better way.

         float distance = Vector3.Distance(playerPosition, Destination);
 
         if( distance <= 1)
         {
                Do somthing
         }
Comment
Add comment · 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

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

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

Related Questions

Setting target position in Vector3.MoveTowards 2 Answers

Player not facing the mouse correctly 1 Answer

Creating a bouncing game without using physics - Vector3 math problem, 2 Answers

Trouble converting transform.position to C# 1 Answer

Trouble setting up a vector 3. 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