- Home /
Align game object base on 2 point
Maybe this is a dump question, but I just start in Unity, sorry.
I have 2 point on the world, both of them have position ( a vector3). Between them is a LineRenderer. LineRenderer only need 2 point to be draw on world.
lr.SetPosition(0, vector3_0);
lr.SetPosition(1, vector3_1);
I want to change the LineRender with a cube (or any game object). My try so far:
//Test SampleCub
sampleCube.transform.position = lr.transform.position;
sampleCube.transform.rotation = Quaternion.LookRotation(vector3_0-vector3_1);
Not working. Please help. Thanks
Answer by PictonicStudio · Oct 17, 2015 at 07:41 PM
using UnityEngine;
using System.Collections;
public class LinePoints : MonoBehaviour
{
//set the number of points you your line to have in order
public GameObject[] Points;
private LineRenderer line;
private void Start()
{
//Make sure you have a LineRenderer component attached to an empty gameobject
line = GetComponent<LineRenderer>();
}
private void Update()
{
for (int i = 0; i < Points.Length; i++)
{
//Sets Points
line.SetPosition(i, Points[i].transform.position);
}
}
}
Here is a script I wrote that should work for you.
LineRenderers work by setting their positions in code by accessing their component
Sorry I really don't understand what your code do. Is it just draw LineRenderer on the world?
Thanks. I found the solution here: http://answers.unity3d.com/questions/406248/need-help-to-get-3d-rotation-from-2-points-in-spac.html
Answer by Azumiar · Oct 18, 2015 at 09:11 AM
You want to aling the position of a point with a position of an object so when you move an object the point will move aswell? If it is that then this code might help: In this case we will change second point
public GameObject object;
Vector3 point1;
Vector3 point2;
Vector3 objectPos;
Transform tr = object.GetComponent<Transform>();
void update() {
point2 = tr.position;
lr.SetPosition(0,point1);
lr.SetPosition(1, point2);
}