Question by
kadirozyy · Jun 25, 2020 at 04:20 PM ·
linerendererdragmouse-draglockon
Line Renderer Object to Object and Lock On Object
I want to make line renderer lock on object when I drag to near it. And I want to start just in an object. I mean line renderer will start in an object and end with another object. The code only draw lines.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragIndictatorScript : MonoBehaviour
{
Vector3 startPos;
Vector3 endPos;
public Material drag;
Camera ccamera;
LineRenderer lr;
Vector3 camOffset = new Vector3(0, 0, 10);
// Start is called before the first frame update
void Start()
{
ccamera = Camera.main;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (lr == null)
{
lr = gameObject.AddComponent<LineRenderer>();
}
lr.enabled = true;
lr.positionCount = 2;
startPos = ccamera.ScreenToWorldPoint(Input.mousePosition)+camOffset;
lr.SetPosition(0, startPos);
lr.useWorldSpace = true;
lr.startWidth = 0.1f;
lr.endWidth = 0.1f;
lr.material = drag;
}
if (Input.GetMouseButton(0))
{
endPos = ccamera.ScreenToWorldPoint(Input.mousePosition) + camOffset;
lr.SetPosition(1, endPos);
}
if (Input.GetMouseButtonUp(0))
{
lr.enabled = false;
}
}
}
Comment