- Home /
Vector3.Distance is acting erratically
Here's the brief version: I have a distance variable in my update function that finds the distance between the transform.position and the transform.position of another object. The variable starts at the actual distance between the two objects, but then, for some indiscernible reason, immediately proceeds to increase at a rate proportional to the gametime passing.
The details: I have a target variable of type Transform and a distance variable of type float in a script attached to an object. The only time the distance variable is changed is in this code right here, from my Update function:
distanceToTarget = Vector3.Distance(Target.position,transform.position);
When it didn't work, I set up a Gizmo to show the distance variable onscreen. What I saw was the distance variable rapidly alternating between the current distance and a steadily increasing value that had nothing to do with the distance. I've also tried to find the distance in different ways.
distanceToTarget = (Target.position - transform.position).magnitude;
This code had the exact same effect as the Vector3.Distance function.
distanceToTarget = Mathf.abs(Target.position.x - transform.position.x);
This code, on the other hand, worked perfectly, so I know it's not the individual values within the objects' Transforms. I still want to get a Vector3 distance, though, but I don't know what's wrong. If anyone has an answer, please tell me.
Edit: After doing more tests, I have confirmed that Vector3.Distance is not the problem, but what I found is still confusing me. When I tried this code:
distanceToTarget = Mathf.abs(Target.position.y - transform.position.y);
I got the exact same problem, so I guess the problem is with my objects' y positions. However, I can't figure out why this happens. Neither object is actually moving along the y axis. The only thing that affects their y positions is gravity, but that shouldn't matter if their actual position isn't changing, right?
Would it be possible for you to upload a Unity project that shows the issue so I could look around a bit?
I'm sorry it took so long for me to respond to this, but I've been trying to cut the project file down small enough to attach to this comment, but I don't think I can. The size limit for files is 512$$anonymous$$B, but I can't get my project any smaller than 24$$anonymous$$B without breaking the scene. Are there any other ways to show you the project?
You should upload it to something like dropbox.com or mediafire.com since there's no way to get your project that small. (I'd recommend dropbox though, it's a really nice way to share files)
O$$anonymous$$. Here's the link. The project is a piece of a set of tutorials that go through creating a $$anonymous$$ario clone. I've narrowed the problem down to the position.y in the enemyControls.js which is acting strangely in the Update function, but seems to work perfectly in the OnDrawGizmo function. What's even stranger is that I have nearly the same code for the playerControls.js, but it's position.y seems to be working perfectly. Thanks for helping me.
Answer by Sequence · Nov 11, 2013 at 11:57 PM
I don't think I got the same behavior that you got, but I still believe I found the solution. Take a look at this piece of code here.
velocity.y -= gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
It adds velocity downward based on time, seems to be in order right? Not so fast! The velocity is being added and added constantly even when the goomba is sitting on the ground. This results in really high velocities downward even when the goomba is just sitting there. You need to add a check for whether or not the goomba is on the ground before adding velocity for gravity.
if(!controller.isGrounded)
velocity.y -= gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
Some other things: You have a rigid body attached to your goomba object along with a character controller.
Rigid bodies should be for things that fall and bounce around, like crates, debris, and cars.
Character controllers are for things like players where you need keyboard input or AI.
You shouldn't put both on the same object because one is for script control and the other is for natural-like physics behavior. (So having them on the same object would be kind of contradictory. Just so you know, you can still have a script controlled rigid body, but that's another story.)
funny story. that rigid body was the problem the whole time. as soon as i removed it, I the functions worked correctly. the tutorial I'm following had the prefabs set up like that when I downloaded the files, so it was most likely an error on their part. thanks for pointing it out, though.
the constantly adding gravity is ok the way I have it because whenever the object is grounded the velocity is reset to zero.
Ok, I'd still recommend only adding velocity downward if the object is grounded like I suggested above. Otherwise, who knows what kind of behavior might emerge later. One last thing, if my answer is good, you can click the checkmark next to the up and down thumbs to mark it as the answer to your question.
Thanks. I will definitely keep that downward velocity in $$anonymous$$d, especially when working on something more serious than a tutorial. Also thanks for letting me know about the checkmark. I thought the thumbs-up was all I needed to click.
Answer by aldonaletto · Nov 11, 2013 at 02:00 AM
That's not a problem with Vector3.Distance: one of the objects is moving in another axis, thus the 3D distance increases continuously. Are you using an orthographic camera? If so, an object moving in the camera's forward direction seems to be static.
Yes, I'm using an orthographic camera, but neither object can be moving in the camera's forward direction because they only have a limited space on that axis to move. It also doesn't explain why the variable switches rapidly between the correct distance and the continuously increasing value. Thanks, though.
Physics?
I guess one object is a Rigidbody with gravity enabled, so it simply falls down.
The switching can have two reasons:
some script changes the Target variable between two or more objects.
If you check the value with Debug.Log, you probably just have two objects with the script attached.
no physics. the gravity is purely though the code. also, the script is only attached to one object in the scene. I haven't used Debug.Log before. how exactly does it work? where and how do I call it and what information does it give me?