How to Script for Magnitude (C#)

I’m sure this has been answered before, but I can’t quite find the answer I’m looking for.

I have two Capsules, both with their own names, tags, and scripts. They are both rigidbodies affected by gravity, and one currently has a lookat toward the other. I’d like to create a script that causes the one looking to move toward the other one, only if the magnitude is greater than or equal to 10. I’ve found plenty of things explaining what Magnitude is, but not much on its application. Could anyone give me a clear answer to this?

Again, to simplify, I’m just trying to create an if statement based on the magnitude between CapsuleOne and CapsuleTwo. I can’t quite figure out how to reference that. Thank you.

Magnitude of what?

Magnitude is just a fancy word that mathematicians use to measure the size of something. But without the thing you are measuring the size of, its meaningless.

Ah, sorry for being unspecific. Vector3 magnitude. Distance between two points on all three planes.

Which script do you have? Can you show us that code?

Where do you think it would go?

Well, to be honest, I’m not sure where it would go. I’m not sure if I need to declare Magnitude as a part of the scriptholder’s vector transform, or if I need to have it be calculated in some way, I’m just really not sure. The official Unity page and tutorial explaining Magnitude don’t have any good examples.

I suppose I should state more accurately I’m simply trying to get an if statement triggered off of the distance between two objects. If Magnitude is the wrong way of doing this, I’m open to better options. I could try to show the script I have thus far, but as I’m learning C# right now, I’m just trying to take a concept and apply it, meaning the script I’ve got currently is just guesswork.

Distance is nice and easy to calculate:

distance = Vector3.Distance(oneVector3, anotherVector3);

So, your if statement would look something like…

if (Vector3.Distance(startObject.transform.position, endObject.transform.position) > 10.0f)
{
   // do stuff
}
1 Like

You can also use sqrMagnitude for calculating distances… it’s a bit faster.

2 Likes

Good tip, thanks! So just for my own knowledge, if you were checking if two objects were 10 units apart, would you do it something like this then?

if ((secondObject.position - firstObject.position).sqrMagnitude > 100.0f) // 100.0f being 10 squared, of course..

Yup, that’s the idea. These days processors are fast enough that you don’t have to be super strict on avoiding square root operations. But its a useful trick to have up your sleeve if you do get into performance critical code.

If performance was super critical, you could inline the vector operations as well.

1 Like

Makes sense, thanks!

I remember now some time ago one of my instructors mentioning that division and square roots are slow operations. Had forgotten about that! You could probably waste a lot of time over optimizing but it’s good to have that knowledge of where you can shave some cycles off anyways.

I would also ask what you mean by “two capsules 10 units apart”. If you mean their game object transform positions, then the above suggestions solve that for you. If you mean the distance between the two closest points on their surface, then you’ll need to find the distance between two line segments and subtract the capsule radii. For that there’s unfortunately not anything built into Unity that you can use.

Thank you all so much for the helpful information! I have to say, Unity’s vector maths tutorial doesn’t do a very good job of demonstrating application as much as it explains what magnitude is. =/

Yes, just the centerpoints. For a first game, I want to dabble with purely AI driven combat, no player interference (on a fighting level anyway). I love watching different AI duke it out.