So im checking distance like that. But i want to visually show the chase range to set it accuratly.
(transform.position - _player.transform.position).sqrMagnitude < ChaseRange;
How can i show it similar to this, is there a way?
private void OnDrawGizmos()
{
if (PatrolPlaceCenter != null)
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(PatrolPlaceCenter.position, PatrolPlaceRadius);
}
}
It would help if you described how you wanted to show the range. A line, a sphere etc.
It sounds like you want to draw a sphere centered on the player of radius “ChaseRange”?
private void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(_player.transform.position, ChaseRange);
}
Thank you for your answer sir.
Sphere would be best but ohers are also okey. The problem with this
Gizmos.DrawWireSphere(_player.transform.position, ChaseRange);
It doesnt show the actual range. It shows some other range not related to chase range.
For example when i set chase range to 40 it creates sphere like this.
But the actual detect range is way more less then this.
Your image doesn’t really show anything clearly and there’s multiple things being drawn too.
That draws a sphere of the radius you specify. The only way it won’t be is if you change the gizmo matrix: Unity - Scripting API: Gizmos.matrix
You can set this to Identity too.
Just draw a sphere of 1 radius and verify.
No i didnt change the gizmo matrix.
I think I didn’t manage to describe the problem fully.
So i created a empty scene to show
As you see from the photo when i set testrange to 5 it must be in range.
But as you see from code even sphere shows that its in range its actually not
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class testscript : MonoBehaviour
{
public GameObject test1, test2;
public float testRange;
private void Update()
{
if ((test1.transform.position - test2.transform.position).sqrMagnitude < testRange) Debug.Log("IN RANGE");
else Debug.Log("NOT IN RANGE");
}
private void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(test1.transform.position, testRange);
}
}
If i set testrange to 13 it says its in range but its way more then sphere shows.
You’re comparing square magnitude to something that isn’t squared?
You should be using Unity - Scripting API: Vector3.magnitude
yes because there was a comment saying that it is more performant than vector3.distance and when I tested it I really saw the performance difference.
that comment
15:55 Optimization tip for the distance checking part: - Vector3.Distance involves a square root in the math, which is super slow to compute. Since you’re only checking which object is the closest, you can use squared distances to remove the square root and make the check faster. - There’s no Vector3.SquaredDistance static method, however you can use (pos1 - pos2).sqrMagnitude. This returns the squared distance between pos1 and pos2.
Yes but only at scale and you need to use square of both sides if you’re comparing them.
You’re saying “if ( (5 * 5) < 5 )”
I wouldn’t take performance comments to mean for things that are not at scale. All it does is avoid a square root. You won’t see that unless you’re doing lots and lots of them in a tight loop.
2 Likes
For sqrMagnitude you need
(test1.transform.position - test2.transform.position).sqrMagnitude < testRange * testRange
or use magnitude instead.
1 Like
Ahh now i understand, thank you very much for clarifying.
1 Like