- Home /
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?
You will get better answers if you can tell us why you want to compare vectors. What game effect are you trying to achieve?
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.
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
The > operator for Vector3 is undefined. This code should cause an error. Check out the script reference for Vector3
It was meant as a meta-code from the OP. Did not have to downvote :|
Okay, I'll be nice. The Vector3.one is a good contribution.
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
}
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)
Thank for your reply. I don't use your suggestion, but it can be useful in future. Big thank for you :)
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
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
}
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.
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
}