I have a grappling type mechanic where if I press a button I create a line. The line works fine on stationary targets but when if there was a moving target the line would hit the target but not move along with it. Is there any way to fix this?
The code:
public Rigidbody player;
public SteamVR_TrackedObject controller;
public LineRenderer line;
private Vector3 target;
public GameObject Object;
public float distance;
bool hitwall = false;
public float raycastDistance;
bool pressed = false;
bool pressed2 = false;
public Animator anim;
public GameObject smoke;
// Update is called once per frame
void Start()
{
smoke.SetActive(false);
}
void Update()
{
var device = SteamVR_Controller.Input((int)controller.index);
if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
{
RaycastHit hit;
if (Physics.Raycast(controller.transform.position, controller.transform.forward, out hit, raycastDistance))
{
line.enabled = true;
line.SetPosition(0, Object.transform.position);
target = hit.point;
line.SetPosition(1, target);
anim.SetBool("small", true);
hitwall = true;
line.material.mainTextureOffset = Vector2.zero;
}
}
else if (device.GetTouch(SteamVR_Controller.ButtonMask.Trigger) && line.enabled)
{
line.SetPosition(0, Object.transform.position);
line.material.mainTextureOffset = new Vector2(line.material.mainTextureOffset.x + Random.Range(-.01f, 0.05f), 0f);
pressed = true;
hitwall = true;
}
else
{
line.enabled = false;
hitwall = false;
anim.SetBool("small", false);
}
if (device.GetPress(SteamVR_Controller.ButtonMask.Grip) && hitwall == true)
{
Debug.Log("Pressed");
anim.SetBool("big", true);
player.AddForce((target - controller.transform.position).normalized * 100 * distance);
smoke.SetActive(true);
}
else
{
smoke.SetActive(false);
anim.SetBool("big", false);
}
}
}