Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by JSBaker · Nov 11, 2013 at 01:50 AM · vector3transform.positionvector3.distance

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?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sequence · Nov 11, 2013 at 01:52 AM 0
Share

Would it be possible for you to upload a Unity project that shows the issue so I could look around a bit?

avatar image JSBaker · Nov 11, 2013 at 04:19 AM 0
Share

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?

avatar image Sequence · Nov 11, 2013 at 07:46 PM 0
Share

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)

avatar image JSBaker · Nov 11, 2013 at 10:16 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer Wiki

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.)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JSBaker · Nov 12, 2013 at 12:33 AM 0
Share

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.

avatar image Sequence · Nov 12, 2013 at 02:01 AM 0
Share

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.

avatar image JSBaker · Nov 12, 2013 at 02:22 AM 0
Share

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.

avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JSBaker · Nov 11, 2013 at 02:11 AM 0
Share

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.

avatar image Bunny83 · Nov 11, 2013 at 10:24 PM 1
Share

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.

avatar image JSBaker · Nov 11, 2013 at 10:40 PM 0
Share

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?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Getting Vector3 in between two given Vector3 after given Distance. 2 Answers

Why is my y-axis bugging? 1 Answer

Having problem with Vector3.distance 1 Answer

Trouble converting transform.position to C# 1 Answer

Teleport using Random.insideUnitCircle relative to a GameObject's position 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges