- Home /
Greater than or equal to Vector3
Okay so basically i want to check if one Vector3 is greater or equal to another vector3 Heres my script where I would like to check if transform.position is greater or equal to DesinationSpot. How would i go about doing this?? using UnityEngine; using System.Collections;
public class RigidbodyMovePlatform : MonoBehaviour {
public Vector3 DestinationSpot;
public Vector3 OriginSpot;
public Vector3 speed = new Vector3(3, 0, 0);
public Vector3 speedBack = new Vector3(-3, 0, 0);
public bool Switch = false;
void FixedUpdate () {
//Here I wanna check if transform.position is greater than or equal to DestinationSpot
if(transform.position == DestinationSpot){
Switch = true;
}
//Here I wanna check if transform.position is less than or equal to OriginSpot
if(transform.position == OriginSpot){
Switch = false;
}
if (Switch == true) {
rigidbody.MovePosition(rigidbody.position + speedBack * Time.deltaTime);
}
else{
rigidbody.MovePosition(rigidbody.position + speed * Time.deltaTime);
}
}
}
I know others have asked simular questions but i havent been able to get a good answer atleast not one in C#.
What does greater or equal mean in a 3D environment? For example, an object with a smaller 'z' value is closer to the camera and therefore on top. $$anonymous$$aybe you want:
bool GreaterEqual(Vector3 a, Vector3 b) {
return (a.x >= b.x) && (a.y >= b.y) && (a.z >= b.z);
}
Are you talking about the magnitude? (length of the vector)? If so then you can use
if (vec1.mangitude <= vec2.magnitude)
Answer by snarf · May 12, 2014 at 10:57 AM
Directly comparing two Vector3 is not the way to go.
instead you should compare the distance to some value - like this;
public class RigidbodyMovePlatform : MonoBehaviour {
public Vector3 DestinationSpot;
public Vector3 OriginSpot;
public Vector3 speed = new Vector3(3, 0, 0);
public Vector3 speedBack = new Vector3(-3, 0, 0);
public bool Switch = false;
void FixedUpdate () {
//Here I wanna check if transform.position is greater than or equal to DestinationSpot
if(Vector3.Distance(transform.position, DestinationSpot) < 1.0f /* within 1 meter radius */){
Switch = true;
}
//Here I wanna check if transform.position is less than or equal to OriginSpot
if(Vector3.Distance(transform.position, OriginSpot) < 1.0f /* within 1 meter radius */){
Switch = false;
}
if (Switch == true) {
rigidbody.MovePosition(rigidbody.position + speedBack * Time.deltaTime);
}
else{
rigidbody.MovePosition(rigidbody.position + speed * Time.deltaTime);
}
}
}