- Home /
Stretch a Quad from one object to another
I am wondering what a good efficient way to Instantiate a simple textured Quad between two objects. (Think of a lightning arc reaching from GO to enemy GO;
I have a basic setup like in: http://docs.unity3d.com/Manual/DirectionDistanceFromOneObjectToAnother.html
But am curious how I would get the prefab(quad) to stretch the distance of the magnitude, bonus if I can get the angle to be more precise, and flatten the rotation so the quad is flat almost parallel the ground.
void OnTriggerEnter(Collider hit)
{
if (hit.gameObject.tag == "enemy") {
Debug.Log("lightning orb hit enemy");
my_enemy_hit = hit.gameObject;
assign_target(my_enemy_hit);
// TURN TOWARDS
Vector3 targetDir = my_target_transform.position - transform.position;
float turn = 10 * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, turn, 0.0F);
//Debug.DrawRay(transform.position, newDir, Color.red);
transform.rotation = Quaternion.LookRotation(newDir);
// create fx
//Vector3 newdir = transform.position - my_target_transform.position;
//newdir.x = 0;
//var magni = newdir.magnitude;
Quaternion newquat = Quaternion.LookRotation(newDir);
//Vector3.MoveTowards(transform, my_target_transform, 1.0f);
Instantiate(lightning_fx01_src, transform.position, newquat);
hit_lightning_orb(my_enemy_hit, 1);
rigidbody.velocity = Vector3.zero;
}
}
Answer by Cherno · Jan 30, 2015 at 01:48 AM
This could be a starting point; no scaling, it directly moves the vertices.
//assign these variables yourself, depending on how you want your quad to stretch
Vector3 startPos1;
Vector3 startPos2;
Vector3 endPos1;
Vector3 endPos2;
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
//switch around where the start and end positions go, just try it out until all four vertices go to the right positions
vertices[0] = new Vector3(startPos1);
vertices[1] = new Vector3(startPos2);
vertices[2] = new Vector3(endPos1);
vertices[3] = new Vector3(endPos2);
mesh.vertices = vertices;
Answer by ShawnFeatherly · Jun 14, 2015 at 08:43 AM
This re-positions a quad using two Vertor3's. As long as the Vector3's have the same Y value, it results in a quad that's facing up and parallel to the ground.
private void stretchQuad(GameObject quad, Vector3 start, Vector3 end, float lineWidth = 0.3f)
{
quad.transform.position = Vector3.Lerp(start, end, 0.5f);
quad.transform.LookAt(end);
quad.transform.Rotate(90, 0, 0);
quad.transform.localScale = new Vector3(lineWidth, Vector3.Distance(start, end), 1);
}
Your answer
Follow this Question
Related Questions
Checking if object intersects? 1 Answer
Help, can't get Grid to instantiate in the center of the screen 0 Answers
Instantiating prefabs, when there is nothing 2 Answers
Material Switching Upon Instantiation. over my head 0 Answers
continuous shooting 1 Answer