Problem with the Intersect method
Hello Unity community,
I have a problem with the Intersect method because it says me some object intersect when they don't. I have a spider which spins threads. At each end of the thread there is a point which scales the thread accordingly (Rescale Script) I want to get all points which are on a thread. (=> They need to intersect)
In this example:
In the following I have listed want points I wanted to get when I use the Intersect method:
Thread 1: Point 1, 2, 4
Thread 2: Point 1, 3, 5
Thread 3: Point 4, 5
However, my code returns me:
Thread 1: Point 1, 2, 4
Thread 2: Point 1, 3, 5
Thread 3: Point 1, 4, 5
I don't get why Point 1 intersects thread 3. I hope you guys can help me.
For simplification I have simplified the code for only this example.
Rescale Script
Scales the thread accordingly to its start and end point.
using UnityEngine;
using System.Collections;
public class Rescale : MonoBehaviour {
public Transform start;
public Transform end;
public bool completed = false;
public float factor = 0.5f;
void Start() {
SetPos(start.position, end.position);
}
void Update () {
if (!completed) {
SetPos (start.position, end.position);
}
}
void SetPos(Vector3 start, Vector3 end) {
var dir = end - start;
var mid = (dir) / 2.0f + start;
transform.position = mid;
transform.rotation = Quaternion.FromToRotation(Vector3.up, dir);
Vector3 scale = transform.localScale;
scale.y = dir.magnitude * factor;
transform.localScale = scale;
}
}
Overlap Script:
Returns all points which intersect with the thread. (Script is attached to each thread)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CheckOverlap : MonoBehaviour {
public List<GameObject> reachablepoints;
// Use this for initialization
void Start () {
reachablepoints = new List<GameObject> ();
GameObject[] points = GameObject.FindGameObjectsWithTag ("Point");
for (int i = 0; i < points.Length; i++) {
if (Intersects (points [i].GetComponent<Collider> ().bounds)) {
reachablepoints.Add (points [i]);
}
}
}
bool Intersects(Bounds bounds){
bool overlapping = GetComponent<Collider>().bounds.Intersects(bounds);
return overlapping;
}
}
Your answer
Follow this Question
Related Questions
Check if object position not taken. 0 Answers
Instantiated Objects Inside of Walls 1 Answer
Finding name of gameobject that entered a trigger 0 Answers
How to draw a collider between two points 0 Answers
Character Controller and Rigidbody 1 Answer