[UPDATE]I AM GOING TO CLEAN THIS UP
VIDEO AT BOTTOM OF PAGE
**I want to thank you all for you’re time and help, and especially incorrect for the solution! **
So, I have turrets, that each have the same script. They all have a “fire” variable which is call atk.
I have found a solution, but It needs some work:
I don’t want to go into how this all worked out, but basicly I needed to have a script control another script, and here it is:
The title is a bit too simple, so sorry.
My problem is this:
I have an object called Turret. Turret has a child named spawnPoint. Turret has a script called spawn and that script has a timer to only spawn a laser prefab every 1/2 second or so, and also looks at target: transform.LookAt(atar);
The laser prefab has no gravity and has rigidbody
Some of the Code to instantiate:
(I didn’t post the timer by the way)
So this works, and it always follows the target, and fires lasers directly at it, and the lasers are always facing the correct direction: The short side is pointed at it :
[Target]
^<-- Direction the laser is going
|
|<- laser
But here is my problem:
In another script, I tell Turret to transform.LookAt(atar.trnasform), an then use the sane instantiate code to spawn a laser:
The only difference is that it finds the Turrets child object:
And what ends up happening is that the lasers face the wrong direction, and transform.forward brings them the worng way! I have tried Vector3.left and -transform.right, but that just makes them only get projected forward, not and angle like the other script does.
It also rotates the wrong way. I have tried to fix this and ont know whats makeing it do this. Please help!
[Target]
Direction its going
٧
__ >
^Laser
Alright, here is the Script that had the static variables: (Its a bit messy)
(bfg.js)It works perfectly by it self (not including the problem with the static variables)HERE IS
bfg.js
#pragma strict
var curtar = "";
var fore = 1000;
var damping = 6.0;
var smooth = true;
var player : GameObject;
var laserb:Transform;
var st;
var dbp = true;
var atk = false; //THIS USED TO BE STATIC
static var atar : GameObject;
function Update(){
//atar = GameObject.FindWithTag("fd");
if (atk == true) {
atar = GameObject.FindWithTag("fd");
transform.LookAt(atar.transform);
var seconds = Time.time;
//var next = (seconds % 2);
dbp = true;
fire(seconds);
}
}
var df = transform.rotation;
var lastFired = 0 ;
var target : Transform;
function fire(seconds) {
if (Time.time > lastFired + 1.0){
atar = GameObject.FindWithTag("fd");
//var plo = atar.transform;
var laser = Instantiate(laserb, transform.Find("spawnPoint").transform.position,
Quaternion.identity);
//laser.transform.LookAt(atar.transform);
laser.rigidbody.AddForce(transform.forward * fore);
laser.transform.LookAt(atar.transform);
//laser.rigidbody.AddForce(Vector3.Dot * 5 * 5);
var laser1 = Instantiate(laserb, transform.Find("spawnPoint1").transform.position,
Quaternion.identity);
laser1.rigidbody.AddForce(transform.forward * fore);
laser1.transform.LookAt(atar.transform);
lastFired = Time.time;
yield WaitForSeconds(0.25);
laser = Instantiate(laserb, transform.Find("spawnPoint").transform.position,
Quaternion.identity);
laser.rigidbody.AddForce(transform.forward * fore);
laser.transform.LookAt(atar.transform);
laser1 = Instantiate(laserb, transform.Find("spawnPoint1").transform.position,
Quaternion.identity);
laser1.rigidbody.AddForce(transform.forward * fore);
laser1.transform.LookAt(atar.transform);
yield WaitForSeconds(0.25);
laser = Instantiate(laserb, transform.Find("spawnPoint").transform.position,
Quaternion.identity);
laser.rigidbody.AddForce(transform.forward * fore);
laser.transform.LookAt(atar.transform);
laser1 = Instantiate(laserb, transform.Find("spawnPoint1").transform.position,
Quaternion.identity);
laser1.rigidbody.AddForce(transform.forward * fore);
laser1.transform.LookAt(atar.transform);
yield WaitForSeconds(0.25);
laser = Instantiate(laserb, transform.Find("spawnPoint").transform.position,
Quaternion.identity);
laser.rigidbody.AddForce(transform.forward * fore);
laser.transform.LookAt(atar.transform);
atar = GameObject.FindWithTag("fd");
laser1 = Instantiate(laserb, transform.Find("spawnPoint1").transform.position,
Quaternion.identity);
laser1.rigidbody.AddForce(transform.forward * fore);
laser1.transform.LookAt(atar.transform);
st = seconds;
dbp = false;
}else {
}
}
PROBLEM–
OKAY HERE IS THE FILE THAT HAS THE PROBLEM:
So turrets is an array of turret objects, and then d is a turret.
This file is ctrl.js
#pragma strict
var targ = "";
static var attr = "";
var sel = false;
var attack = false;
var turrets : GameObject[];
var targeter = false;
var target : GameObject;
var d : GameObject;
var laserb : GameObject;
var fore = 1000;
function Start () {
}
var atar : GameObject;
function Update(){
if (sel == true) {
if (targeter == true){
//turrets*.atar =*
-
//turrets[1].GetComponent(bfg).atk = true;*
-
//var def : bfg;*
-
//def = turrets[1].gameObject.GetComponent(bfg);*
-
//bfg.atk =*
-
fire();*
-
}*
}
}
var lastFired = 0;
function fire() {
if (Time.time > lastFired + 1.0){
atar = GameObject.FindWithTag(“fd”);
-
d.transform.LookAt(atar.transform);*
-
var fg = turrets[0];*
-
var laser1 = Instantiate(laserb, d.transform.Find(“spawnPoint”).transform.position,*
Quaternion.identity);
laser1.rigidbody.AddForce(transform.forward * fore);
laser1.transform.LookAt(atar.transform); -
lastFired = Time.time;*
-
yield WaitForSeconds(0.25);*
}
}
DOES NOT WORK AND USES CTRL.JS:
[40600-ddd.png|40600]*
WORKS AND USES BFG.js:
[40601-ddr.png|40601]*
[HERE IS A VIDEO][3]
[3]: - YouTube