- Home /
Drawing line from plane edge
I am trying to draw a line from one object to a target object. i have managed to do this however the lines are drawn from the centers
what i would like to do is draw the lines from the edge of a plane to the edge of a plane on the target
In this image the white lines are the currently drawn connection and the red lines are how i would like the lines to be drawn
https://www.youtube.com/watch?v=$$anonymous$$bW$$anonymous$$8bCAU2w
Check this tutorial out. This might be able to help you set up those rays based on the edges of the planes at least.. Not so sure about sending the rays to hit the other edges though.
You could also set up 4 empty objects on the edges at the centers and fire rays based on which object is closest? Don't quote me too much on these solutions though, just thinking of how I'd try to do it.
P.S. Sick lookin' game you're working on!
In general in the first place you should carefully learn about
Bounds
$$anonymous$$ake sure you learn properly, and be sure to understand the difference between renderer bounds, and the others.
And yeah your game looks O$$anonymous$$. Don't get cocky.
Answer by Masterio · Jan 20, 2016 at 08:30 PM
Maybe this helps:
http://docs.unity3d.com/ScriptReference/Mesh-bounds.html
Attach this sctript on the plane
using UnityEngine;
using System.Collections;
public class GetBounds : MonoBehaviour
{
Vector3 targetPoint = Vector3.zero;
public void OnDrawGizmos()
{
Mesh mesh = GetComponent<MeshFilter>().mesh;
Bounds bounds = mesh.bounds;
// not affected by scale or rotation
Debug.Log ("x: " + bounds.size.x +" y: "+ bounds.size.y + " z: " + bounds.size.z);
// next you can simply calculate the start point of the ray
Vector3 edgeA = transform.position + new Vector3(bounds.size.x * transform.localScale.x / 2, 0, 0);
Vector3 edgeB = transform.position - new Vector3(bounds.size.x * transform.localScale.x / 2, 0, 0);
Vector3 edgeC = transform.position + new Vector3(0, 0, bounds.size.z * transform.localScale.y / 2);
Vector3 edgeD = transform.position - new Vector3(0, 0, bounds.size.z * transform.localScale.y / 2);
// next if plane is rotated you must calculate position for this rotation...
edgeA = RotatePointAroundPivot(edgeA, transform.position, transform.localRotation.eulerAngles);
edgeB = RotatePointAroundPivot(edgeB, transform.position, transform.localRotation.eulerAngles);
edgeC = RotatePointAroundPivot(edgeC, transform.position, transform.localRotation.eulerAngles);
edgeD = RotatePointAroundPivot(edgeD, transform.position, transform.localRotation.eulerAngles);
Gizmos.color = Color.red;
Gizmos.DrawLine(edgeA, targetPoint);
Gizmos.DrawLine(edgeB, targetPoint);
Gizmos.DrawLine(edgeC, targetPoint);
Gizmos.DrawLine(edgeD, targetPoint);
}
private Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles)
{
Vector3 dir = point - pivot; // get point direction relative to pivot
dir = Quaternion.Euler(angles) * dir; // rotate it
point = dir + pivot; // calculate rotated point
return point; // return it
}
}
It looks like this:
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to check for a collisoin between objects 1 Answer
Creating randomly disappearing floor tiles 0 Answers
Draw shortest distance between 2 points on a sphere 1 Answer