Here’s a link to the game, so you can see what’s going on.
Here’s my object Heirarchy:
Simply put, what I’m trying to do is have a laser shoot 10 units away from the player Mesh. If the player stays at (0,0,0), no problems. For some reason, the moment I move, the second vertex in the lineRenderer seems to stay in the same spot. Any clues?
using UnityEngine;
using System.Collections;
public class Bangbang : MonoBehaviour {
RaycastHit hit;
Ray ray;
LineRenderer lr;
Transform GunPos;
Transform Player;
public Vector3 forwardpos;
void Start () {
lr = GetComponent<LineRenderer>();
GunPos = transform.Find("Gun");
Player = GameObject.Find ("PlayerMesh").transform;
forwardpos.z = transform.forward.z * 10;
forwardpos.x = transform.forward.x * 10;
}
void Update ()
{
forwardpos.z = transform.forward.z * 10;
forwardpos.x = transform.forward.x * 10;
if(Input.GetKey(KeyCode.Space))
{
lr.enabled = true;
lr.SetPosition(0, GunPos.position);
lr.SetPosition(1, forwardpos);
if(Physics.Raycast(GunPos.position, transform.forward, out hit, 10.0f))
{
lr.SetPosition(1, hit.point);
Debug.DrawLine(GunPos.position, hit.point);
}
Debug.DrawLine(GunPos.position, forwardpos);
}
else lr.enabled = false;
}
}