• 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 /
  • Help Room /
avatar image
0
Question by byalexeykh · Dec 15, 2019 at 04:54 PM · distancecalculatecalculations

Vector3.distance wrong distance calculating?

I have Player object and Enemy object. Enemy is always stalking Player and trying to kill him. In function that sends damage to Player i use Vector3.distance to calculate disance between two objects and here strange things begin to appear: If i move Enemy prefab from editor to scene manually everything works fine, distance calculates correctly and Enemy sends damage to Player as it comes close enough. But if I Instatiate the same Enemy prefab via script Vector3.Distance begins to calculate the distance from the Enemy to some point located at the other end of the map, where there is literally nothing. There are no objects with the same tag (Im referencing Player GameObject to Enemy using FindGameObjectWithTag). Because of that Enemy can't hit Player.

Interestingly, to calculate the movement of the enemy to the player, I use the function Vector3.MoveTowardswith the same referencing to Player GameObject and it works fine in both cases.

i.e. if Enemy was Instatiated via script, it can move to Player but cannot hit him because the distance is calculated incorrectly

I ask you not to say that the FindGameObjectWithTag operation is heavy and "costs" some computing power, I know it

Reference for Vector3.Distance():

 void Start()
 {
 Target = GameObject.FindGameObjectWithTag("Player");
 }
 Vector3.Distance(Target.transform.position, transform.position)

Reference for Vector3.MoveTowards():

 void Start()
 {
 Target = GameObject.FindGameObjectWithTag("Player");
 }
 Vector3.MoveTowards(transform.position, new Vector3(Target.transform.position.x, transform.position.y, transform.position.z), speed * Time.deltaTime);

Game is in 2D space

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by byalexeykh · Dec 16, 2019 at 01:10 PM

Bunny83 said

Vector*3.Distance calculates just the euclidean distance in 3d*.

Since my game is in 2D space, I immediately went to check the Z coordinate, and the problem was there. Spawner that spawns Enemy prefabs was at -60 Z coordinate, and passed its coordinates to the instatiated objects.

Comment
Add comment · 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
1

Answer by Bunny83 · Dec 15, 2019 at 08:46 PM

Well you most likely do not have your objects placed on the same z depth. In your moveTowards method you do not move towards the player / target but towards the player / target position projected onto the depth plane where this object is located.


Vector3.Distance calculates just the euclidean distance in 3d. There certainly is nothing wrong with it. If something doesn't turn out as you expected it's your fault. Probably because your input positions are not what you think they are. Have you actually started debugging your issue? Have you printed the two positions? What distance do you get? Have you printed it?

Comment
Add comment · Show 1 · 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 byalexeykh · Dec 16, 2019 at 12:58 PM 0
Share

alt text

Yes, I am debugging all if statements. As you can see, debugging displays absolutely strange distance values. But your comment gave me one thought...

sad.png (9.0 kB)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

190 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 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 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 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 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 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 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 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 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 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

Calculate the arc distance between 2 points on a sphere 3 Answers

Calculate distance between gameobject and polyline 0 Answers

Trajectory line throwing downward 0 Answers

How to optimize big numbers calculatation. Not exact transform position when I'm moving on a far distance from a zero point 1 Answer

Fade Material's Transparency Based on Distance 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges