Reduce length of a Vector3D

Hi

I want to make sure that there is no object betwen two objects a and b, I reach this with a Physics.Linecast().

if(!Physics.Linecast(transform.position, obj.transform.position - objPos)) {
      good.Add(obj);
}

The problem is that the “obj” has a box collider on it with the size of 1 at each side and therefore it is always something betwen the objects. So I decided to calculate a vector in the same direction (form a to b) but 1 unit less long. I’m not the genius in this sort of math so …

Vector3 objPos = new Vector3();
					
if(obj.transform.position.x > obj.transform.position.y  obj.transform.position.x > obj.transform.position.z)
objPos = new Vector3(1, obj.transform.position.y / obj.transform.position.x, obj.transform.position.z / obj.transform.position.x);

else if(obj.transform.position.y > obj.transform.position.x  obj.transform.position.y > obj.transform.position.z)
objPos = new Vector3(obj.transform.position.x / obj.transform.position.y, 1 , obj.transform.position.z / obj.transform.position.y);

else if(obj.transform.position.z > obj.transform.position.x  obj.transform.position.z > obj.transform.position.y)
objPos = new Vector3(obj.transform.position.x / obj.transform.position.z, obj.transform.position.y / obj.transform.position.z, 1);

I know that this is defenitly not the corrects way to calculate it. So how can I make the thing work?
With my code the Objects gets the most time detected when they are diagonally, so a diagonally to b. Which shouldn’t be.

Thanks.

realm_1

  1. there is no such thing as a Vector3D. It’s Vector3.

  2. Unity - Scripting API: Vector3.Normalize then scale it back up by how long you want it to be.

Sry when I made the title I thought at Unity3D and wrote by mistake Vector3D.
Nice function, never understood what it really does but now I do and the thing work.

Thanks.

realm_1

You could use the layerMask parameter from Linecast function to exclude the colliders that you don’t want to be detected.

To subtract exactly “one unit” from vector:

  objPos  -= objPos.normalized;

A normalized vector is a vector with same direction but length 1

I’m working already with LayerMask but at the OverlapSphere function, so the both would collide. Anyway I solved the problem with the normalized vector.

Thanks.

realm_1