- Home /
vectors and ifs
Hi,everyone,what im trying to do is to have an object move back if 2 objects get close to each other and i have to do that with vectors but im getting an error. That error says Operator <= cannot be used with a left hand side of type 'UnityEngine.Vector3' and a right hand side of type 'UnityEngine.Vector3'
I seriously got no idea what to use so any idea would be really appreciated Thank You Very Much
function Update()
{
Player1= GameObject.FindWithTag("Player1");
Player2= GameObject.FindWithTag("Player2");
AllPlayers= Player1.transform.position + Player2.transform.position;
transform.position = AllPlayers/2;
if(AllPlayers <= Vector3(100,0,0))
{
transform.position.x=AllPlayers.x/2;
transform.position.y= AllPlayers.y*2;
transform.position.z=AllPlayers.z/2;
}
}
In case i wasnt really specific with my problem tell me plz i will try to explain it more
tried to do it this way and the other with alot of other things but seems like Transform wont let me do that it says the best overload for the method Distance is not compatible with the argument list UnityEngine.Transform The Problem is still in the same spot and this is how it looks like
var point1 : Transform; var point2 : Transform; var distance : float = 10; function Update () { if(Vector3.Distance(point1,point2)<=distance){ DoSomthing(); } }
function DoSomthing () { /*transform.position.x=(point1.transform.position.x + point2.transform.position.x)/2; transform.position.y= (point1.transform.position.y + point2.transform.position.y)2; transform.position.z=(point1.transform.position.z + point2.transform.position.z)/2;/ transform.position =(point1.transform.position+ point2.transform.position)/2; }
Answer by 3dDude · Aug 10, 2010 at 11:07 PM
you have to use Vector3.Distance(); for stuff like that EG :
var point1 : Transform; var point2 : Transform; var distance : float = 10; function Update () { if(Vector3.Distance(point1.position,point2.position)<=distance){ DoSomthing(); } }
function DoSomthing () { //do stuff here }
hope this helps
tried to do it this way and the other with alot of other things but seems like Transform wont let me do that it says the best overload for the method Distance is not compatible with the argument list UnityEngine.Transform
ok thank you it works but is not doing what i want do u know a way for distance to add both of them? cuz its not doing it
never$$anonymous$$d srry i think it was my problem in the function thank you very much seriously!!
Answer by cncguy · Aug 10, 2010 at 11:07 PM
You can't compare Vector3's that way as what does it actually mean for one to be less than the other?
What you actually want to do is compare distances
eg. Vector3.Distance(obj1, obj2) <= someDistance
Your answer
![](https://koobas.hobune.stream/wayback/20220613070942im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why does if(levelToLoad ==null) not work? 2 Answers
If/else statements working one way, and not the other 0 Answers
Setting Scroll View Width GUILayout 1 Answer
Code not entering if statement. 1 Answer