Calculate x units forward?

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;
    			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;

transform.forward is a unit vector meaning it is equal to (0,0,1) that is why you always get the same position, you would have to convert it to world position.

I would go for:

forwardpos = transform.position + transform.forward*10;

I cant be sure without seeing your object hierarchy but i presume that GunPos object is not a child of the mesh which is moving since you are calling GameObject.Find.

So are you sure GunPos object is also moving when player is moving ? Because u are using this objects position as start point for line renderer.

I would suggest adding GunPos object as child of player so it would always move with player.

thank you! this was simple once you thought about how to translate it! :slight_smile: @fafase