I am making an experimental game and wondering if someone can shed some light on a few problems. This is where I am so far…
Step 1: Generate tree objects from coordinates in a txt file. DONE ![]()
Step 2: The ability to select trees using RectContains. DONE ![]()
Step 3: If RectContains, change the contained trees size/chop them etc. DONE ![]()
Step 4: Create handles and connect them with lines for visual representation of a selection tool. DONE ![]()
Step 5: Close the loop of the selection tool… Like in Illustrator, so by clicking on the first handle, the area is enclosed. NO LUCK :? Need help. Please.
Step 6: See if the trees are contained within the bounds of the handles/lines, instead of a RectContains, using PolyContains. NO IDEA
Need advice and help, pretty please…
Code so far… This is the Handle Editor which I put on an empty GO.
using UnityEngine;
using System.Collections;
public class HandleEditor : MonoBehaviour {
private static int h_count = 0;
public GameObject handle;
void Update () {
if (Input.GetMouseButtonUp (0)) {
Ray worldRay = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast (worldRay, out hitInfo)) {
UnityEngine.Object[] handles = FindObjectsOfType(typeof(Handle));
h_count = handles.Length;
GameObject handleInstance = Instantiate(handle, hitInfo.point, Quaternion.identity) as GameObject;
handleInstance.name = "Handle" + h_count.ToString("0");
Handle handleScript = handleInstance.GetComponent("Handle") as Handle;
handleScript.id = h_count;
h_count++;
}
}
}
}
This is the Handle Counter which I put on the handle prefab (which is just a small cube).
using UnityEngine;
using System.Collections;
public class Handle : MonoBehaviour {
public static ArrayList handles = new ArrayList ();
public int id;
public static void reset () {
handles = new ArrayList ();
}
public static void InitAll () {
Handle handle;
handles.Sort ();
for (int i = 0; i < handles.Count; i++) {
handle = (Handle)handles[i];
}
}
public void Awake () {
handles.Add(this);
}
}
This is the line code which connects the handles together.
var receiver: Transform;
function Update () {
var id = GetComponent("Handle").id - 1;
receiver = GameObject.Find("Handle" + id).transform;
var line : LineRenderer = GetComponent(LineRenderer);
line.SetPosition(0, transform.position);
line.SetPosition(1, receiver.position);
}
All the code works, but I can’t figure out how to close the shape ie, how to connect the line to the first instance of the handle when I click on it. This should then stop instantiating any more handles.
