- Home /
Beam Weapon
Sorry if this has already been asked before a million times, but i don't quite understand how i would combine these 2 scripts, i need my ship to fire a directed energy beam at the target reticle...
Here is the beam script
@script RequireComponent (LineRenderer)
var mouse : Vector2;
var hit : RaycastHit;
var range : float = 100.0;
var line : LineRenderer;
var lineMaterial : Material;
var ray : Ray;
function Start()
{
line = GetComponent(LineRenderer);
line.SetVertexCount(2);
line.renderer.material = lineMaterial;
line.SetWidth(0.1f, 0.25f);
}
function Update() {
if (ship) {
//var myShipClass = GetComponent("shipClass");
target = ship.GetComponent(shipClass).Get_Target();
}
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit, range))
{
if(Input.GetMouseButton(0))
{
line.enabled = true;
line.SetPosition(0, transform.position);
line.SetPosition(1, hit.point + hit.normal);
}
else
line.enabled = false;
}
}
And the Reticle Script:
private var target:GameObject = null;
var reticle:Texture2D;
var ship:GameObject;
private var guiCrossPos:Vector3 = Vector3(0,0,0);
private var guiCrossSize:Vector2;
function Start () {
}
function Update () {
if (ship) {
//var myShipClass = GetComponent("shipClass");
target = ship.GetComponent(shipClass).Get_Target();
}
if (target && reticle) {
guiCrossSize = Vector2(135,100);
guiCrossPos = camera.WorldToScreenPoint(target.transform.position);
guiCrossPos.x -= guiCrossSize.x / 2;
guiCrossPos.y += guiCrossSize.y / 2;
guiCrossPos.y = Screen.height - guiCrossPos.y;
print(guiCrossSize);
}
}
function OnGUI () {
if (target && reticle) {
GUI.DrawTexture(Rect(guiCrossPos.x,guiCrossPos.y,guiCrossSize.x,guiCrossSize.y),reticle);
}
}
Format the question please, it hurts my eye trying to read the 2nd script. By the way, [code]...[/code] will not work here.
Thanks, basically what i'm trying to do is this:
Can anyone please assist with this?
So i ask someone to help me with this and i get a down vote? that's not very nice.
I think you got downvoted because it isn't clear what you have tried, what problems you are encountering, or what question you are asking. Try to boil it down a little further.
Well, my beam goes flying around to objects other than what the Targeting Reticle has selected.
Answer by robertbu · Mar 07, 2013 at 12:21 AM
You likely got a down vote because you posted a question with very little explanation about what code was doing now or how you wanted it to behave. Nor did you show any understanding of the code in your comments, which pushed the question towards "do it for me" rather than "help me."
The Reticule code simply displays the retcule at target position projected onto the screen. The first script finds targets based on projecting a ray from the mouse position into the scene and looking at what it hit. You don't need to actually combine the two scripts (though you can), but you need to change the first script so that it targets in the same way. In looking at the script, here are some completely untested changes that will point you in the right direction:
function Update() {
if (ship) {
//var myShipClass = GetComponent("shipClass");
var target = ship.GetComponent(shipClass).Get_Target();
}
//ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//if(Physics.Raycast(ray, hit, range))
//{
if(Input.GetMouseButton(0))
{
line.enabled = true;
line.SetPosition(0, transform.position);
//line.SetPosition(1, hit.point + hit.normal);
line.SetPosition(1, target.transform.position);
}
else
line.enabled = false;
//}
}
Note given the parameters above, the LineRenderer component must have the the "Use World Space" turned on.
Thank you good sir, that got me a good bit on my way to figuring this beast out :)
Your answer
Follow this Question
Related Questions
Character lasergun? 1 Answer
Creating laser gun effect. 1 Answer
How to draw beams? 2 Answers
Look at target and flip 1 Answer
Ray Gun / Laser Beam 3 Answers