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;
}
}
Almost. This: transform.TransformDirection(transform.forward10); doesn't make much sense ;) transform.forward is already a direction in worldspace. You treat it like a local space direction and transform it again into worldspace which would be quite rubbish. transform.forward is the same as: transform.TransformDirection(Vector3.forward); Vector3.forward is a constant which equals 0,0,1. transform.forward is the objects forward vector in worldspace. So it should be simply: forwardpos = transform.position + transform.forward10;
– Bunny83Oh yeah, my bad...even the doc starts with "The blue axis of the transform in world space."...
– fafaselol you might want to get rid of the old one. But thanks, that worked perfectly!
– firestorm713q