Hey,
first of all this is a 2D project and I am trying to set the points of an EdgeCollider2D via code, a component which is added to a Line gameobject, which mainly consists of a Line Renderer. Which I draw using my mouse from one object to another to connect those visually.
This happens at some places in my project, the very first one I am working on is this snippet here:
void Update () {
Vector3 mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
mousePos.z = 0;
if(circCol.bounds.Contains(mousePos)) {
if (Manager.MouseLineRenderer) {
Manager.MouseLineRenderer.SetPosition (1, this.transform.position);
// Also set end point of Edge Collider
Manager.MouseLineEdgeCollider.points[1] = new Vector2(this.transform.position.x, this.transform.position.y);
}
}
}
The line with Manager.MouseLineCollider… is the one I am working on right now and as it is right now, this ends up with this ending point (my Line only consists of 2 points and therefore also the EdgeCollider2D) appearing somewhere to the down right of where it should be. What’s more, this line does not seem to affect the EdgeCollider2D points at all since they always appear at the same position regardless how I draw the line.
Setting the Manager objects/references is done in another class, which is attached to the gameobjects, from which I start drawing the line:
void OnMouseDrag () {
line = newLineObj.GetComponent<Line>();
line.originObject = this.gameObject;
lineRenderer = newLineObj.gameObject.GetComponent<LineRenderer> ();
Vector2 screenPos = new Vector2();
Camera.main.ScreenToWorldPoint (screenPos);
lineRenderer.SetPosition (0,
new Vector3 (origin.position.x + (GetComponent<SpriteRenderer>().bounds.size.x)/2,
origin.position.y,
origin.position.z));
lineRenderer.SetPosition (1, Camera.main.ScreenToWorldPoint(Input.mousePosition)+Vector3.forward*10);
Manager.MouseLineScript = newLineScript; // Set reference to current drawn line
Manager.MouseLineRenderer = newLineRend;
Manager.MouseLineEdgeCollider = newLineScript.gameObject.GetComponent<EdgeCollider2D> ();
}
What I want is this: points[1] should be set to the center point of this object (hence transform.position).
I guess this has to do with Worlspace/Screenspace/Camera but I don’t really understand what exactly I need to consider when doing such things.