0

I have two Gameobjects A and B and I want to detect the Trigger b/w them. Remember, neither I want to use Rigidbody with colliders nor Physics.Raycast due to performance reasons.

I don't want to use Raycast as it is Expensive and also Rigidbody cause it don't allow to differentiate that which Object trigger the Another (like did car hit the motorcycle or motorcylce hit the car).

So my simple question, is there any third, fourth, fifth or sixth or else way available to detect Trigger without using any Raycast/Rigidbody/Collider?

4
  • "depends". What kind of GOs are you talking about? 2D or 3D? What shape? Does size/etc change? Do they move? Only one of them? How many GOs you have (two only, "any amount"? Only any two can collide at any given time? So give much more details. Either there is a 'cheaper' solution or there is not, it really depends on what you have on plate
    – user2299169
    Nov 11, 2016 at 12:00
  • simply a car navigation system Nov 14, 2016 at 12:30
  • then simply I cannot answer without proper details on my questions. good luck.
    – user2299169
    Nov 14, 2016 at 15:23
  • @Mark why becoming angry this is the just a general case. real scenario i am trying to develop a navigation system of cars. Nov 15, 2016 at 5:06

2 Answers 2

4

Use the Plane struct http://docs.unity3d.com/ScriptReference/Plane.html

Why?

  1. Performance, faster than Raycasts

  2. Accuracy, plane intersections are more precise

What is a Plane?

A plane in Unity is defined by an origin (position vector), and a normal (direction vector perpendicular to the plane). For instance, a plane with origin Vector3.zero and normal Vector3.up

var plane = new Plane(Vector3.up, Vector3.zero); // new Plane(normal, origin)

creates a theoretical plane on the X/Z axes that is infinite in size and located at the origin. Make sure the normal vecor is normalized!

How to intersect a plane with a ray?

Using the Plane.Raycast(ray) method, you input the ray and receive the distance from the intersection point. You then input this distance into the Ray.GetPoint(distance) method inside your ray. Like this:

// var plane from before
var distance : float;

plane.Raycast (new Ray(Vector3(0, 10, 0), -Vector3.up), distance);
var point = Ray.GetPoint(distance);
Debug.Log(point.ToString());

The code should print "(0,10,0)", since the ray starts at 10 units along the Y axis above the origin and travels downward 10 units before it hits the plane.

Example?

Here is an example of using the Plane struct with the Ray struct to efficiently calculate an intersection point.

public Plane groundPlane;

void Update() {
    if (Input.GetMouseButtonDown(0))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        Vector3 point = GetIntersectionPoint(groundPlane, ray)
        Debug.Log(point.ToString());
    }
}

Vector3 GetIntersectionPoint(Plane plane, Ray ray) {
    float rayDistance;
    if (plane.Raycast(ray, out rayDistance))
    {
        return ray.GetPoint(rayDistance);
    }  
}

Note

Don't be deceived by the Ray struct being used. The plane.Raycast() uses the ray for vector line calculations with the plane and returns a distance on that line to the intersection point.

2
  • 2
    Can you please elaborate that how to use it for collision detecting between two objects?
    – Umair M
    Jun 13, 2016 at 11:39
  • @MyIsaak your solution doesn't look efficient! I am not adopting raycast due to performance reason and your solution using it Nov 11, 2016 at 11:45
-1

Maybe something like Physics.Overlap? https://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.