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?
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)
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 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
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
}
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
}