- Home /
don't include vertical distance?
I have this code
while(Vector3.Distance(player.postion + player.forward*3 , transform.position) > .5){
transform.LookAt(player.postion + player.forward*3);
//code to move forward
yield;
}
How do I properly tell it to ignore the players vertical distance? Thanks
Edit: a picture ref: I hope this makes things clearer, if not tell me. I will try again
You've asked this question at least 3 different ways in the last three days. It's clear you have no idea what you are working with.
Please try some tutorials and get a feel for vector mathematics and 3d spaces. This sort of exhaustive guided debugging is NOT what Unity Answers is for.
It's not that I have no idea what I'm working with, it's that I'm confused. I know of the manuals and general tutorials. If I could figure it out on my own, I wouldn't be putting up with the trouble so many times. I need an ANSWER. Hence, I come to Unity...Answers. It's not like I'm asking for free codes. If this site is not for guidance and 'debugging', then what it is for?
We're all confused too. Apparently you're not conveying your problem in a clear manner. I'll tell you what. You draw a picture of your general problem. Forget dogs and people. Just break it down to the fundamental issue. Drop any "solutions" you're trying to make work. Just give us what you have to work with and what you want to get out of it. I want to devote as much time to this as I possibly can to get it answered and off of the site **forevar!!!!!*
Ok, lots of people here are giving you stellar answers from what I can tell. It seems the problem is that you are plugging this into a larger script and you're not seeing what you expect, because some other part is broken.
Reduce it to the simplest case so you know what we are telling you works. This is a trivial problem. Constraining a distance measurement to the X-Z plane.
Something else must be wrong.
Answer by testure · Jun 24, 2011 at 04:33 AM
just create a new vector that uses the object's y position rather than the player's:
Vector3 temp = new Vector3(player.position.x, transform.position.y, player.position.z)
Transform.LookAt is still ai$$anonymous$$g for the y vector though, on uneven surfaces, even if it's in the correct xz postion to the player, it still aims for the y of the player. What now?
your code is telling it to look at the player's position and adding a local forward * 3 to it. both player.position and player.forward contain this "Y" value that you want to ignore, you're going to have to zero them both out, otherwise you're just adding it back in.
Okay, the closest I got to that was this, but it still doesn't work. What did I do wrong?
var point2 : Vector3 = leader.forward;
point2.y = transform.forward.y;
var Vector3 = new Vector3(leader.position.x, transform.position.y, leader.position.z);
while (Vector3.Distance(leader.position + point2*3 , transform.position ) > .5){
transform.LookAt(leader.position + point2*3);
What is var Vector3 defining? I don't use JavaScript, but don't you have to supply a variable name? It looks like you're using a type as a variable name. Additionally, leader.position still has the y you're tring to get rid of.. It gets added back in every time.
okay, how about this one (still malfunctioning)
var point1 = leader.forward;
point1.y = transform.forward.y;
var point2 : Vector3 = leader.position;
point2.y = transform.position.y;
while (Vector3.Distance(point2 + point1*3 , transform.position) > .5){
transform.LookAt(point2 + point1 *3);
comeHere = true;
yield;
}
transform.LookAt(leader.position + leader.right * 5);
animation.CrossFade("idle");
comeHere = false;
}
Answer by Joshua · Jun 25, 2011 at 12:32 AM
The easiest way to get the distance between two vectors if you want to ignore height (or an other dimension) is
var a : Vector3;
var b : Vector3;
function Start()
{
print( "ignoring height, the distance between a and b is "
Vector3( a.x-b.x, 0, a.z-b.z ).magnitude );
}
You've forgot something:
Vector3(a.x-b.x, 0, a.z-b.z).magnitude
does this calculate x and z distance the same as my last attempt? It acts the same way as before. You're right with your example, but I think the distance part isn't the issue though, I think it's how I have LookAt set up. The object will go the right position but once there, will rapidly move around at in that spot. I think I somehow am STILL adding height to that somewhere?
Your answer
Follow this Question
Related Questions
Distance won't work 1 Answer
Set position to transform.forward+distance 0 Answers
distance to variable object 1 Answer
Moving child and parent 1 Answer