- Home /
Question by
alphagas · Sep 28, 2011 at 07:59 AM ·
linerendererlaserbeam
Laser using line renderer
I'm trying to make my character shoot a laser beam or ray similar to this video: http://www.youtube.com/watch?v=VD1wsPAXS2o I can't even get the laser to show up. Here's what I have. I don't care about what it looks like or colliding with anything I just want the laser to originate at my character and continue to the point where my mouse is clicking.
using UnityEngine;
using System.Collections;
public class PlayerShoot : MonoBehaviour {
public GameObject laser;
// Update is called once per frame
void Update () {
if(Input.GetMouseButton(0)){
Plane zeroPlane = new Plane(Vector3.up, Vector3.zero);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float distance;
if(zeroPlane.Raycast(ray, out distance)){
Vector3 outputPosition = ray.origin + ray.direction * distance;
(laser.GetComponent(LineRenderer)).SetPosition(0, this.transform.position);
(laser.GetComponent(LineRenderer)).SetPosition(1, outputPosition);
Debug.Log("Position: " + outputPosition);
Debug.DrawLine(transform.position, outputPosition, Color.white);
}
}
}
}
Comment
Best Answer
Answer by Julien-Lynge · Sep 29, 2011 at 12:30 AM
I assume that the code you posted works and you're correctly getting the mouse position on the collider, and all you need is code to create the actual beam. Have you tried looking at the sample project that comes with Unity (angry bots)? That demo has a gun laser sight that would probably get you 90% of the way there.
Good luck.