- Home /
How to send a transform from a C# script to a Js script
Im trying to get a turret to locate a pacific target by tag and then to rotate its base to face the target and to raise the barrel to point at the target.
The scripts are in 3 parts, -The "TeamIder.cs" that finds the closest enemy based on its own tag and the enemys tag -The "TurrentBase.Js" that rotates the turrets base to point at the target -The "TurrentGun.Js" that raises the barrel to point at the target
The "TurrentBase.Js" and "TurrentGun.Js" are supposed to get there target from the "TeamIder.cs" script. (Note: the target variable in this case is a transform). But the problem is that i cant find any documentation on such a thing, and im out of ideas.
---TeamIder.cs---
using UnityEngine;
using System.Collections;
public class TeamIder : MonoBehaviour {
public float SearchRate = 1f;
private float SearchTime = 0f;
private Transform Target;
private float distance;
private string mytag;
private string Enemytag;
private Transform ReceiveTarget;
public GameObject barrelobj;
// Use this for initialization
void Start () {
WhosMyEnemy();
FindClosest();
}
//Finds the enemy of the object that this script is attached to (by tag)
void WhosMyEnemy(){
mytag = gameObject.tag;
if (mytag == "TeamHumans"){
if(GameObject.FindGameObjectWithTag("TeamProphets")){
Enemytag = "TeamProphets";
}
if(GameObject.FindGameObjectWithTag("TeamRots")){
Enemytag = "TeamRots";
}
if(GameObject.FindGameObjectWithTag("TeamAncients")){
Enemytag = "TeamAncients";
}
}
if (mytag == "TeamProphets"){
if(GameObject.FindGameObjectWithTag("TeamRots")){
Enemytag = "TeamRots";
}
if(GameObject.FindGameObjectWithTag("TeamAncients")){
Enemytag = "TeamAncients";
}
if(GameObject.FindGameObjectWithTag("TeamHumans")){
Enemytag = "TeamHumans";
}
}
if (mytag == "TeamRots"){
if(GameObject.FindGameObjectWithTag("TeamProphets")){
Enemytag = "TeamProphets";
}
if(GameObject.FindGameObjectWithTag("TeamAncients")){
Enemytag = "TeamAncients";
}
if(GameObject.FindGameObjectWithTag("TeamHumans")){
Enemytag = "TeamHumans";
}
}
if (mytag == "TeamAncients"){
if(GameObject.FindGameObjectWithTag("TeamProphets")){
Enemytag = "TeamProphets";
}
if(GameObject.FindGameObjectWithTag("TeamRots")){
Enemytag = "TeamRots";
}
if(GameObject.FindGameObjectWithTag("TeamHumans")){
Enemytag = "TeamHumans";
}
}
if (mytag == "TeamCivilian"){
Enemytag = "TeamRot";
}
//if (mytag != "TeamHumans" && mytag != "TeamProphets" && mytag != "TeamRots" && mytag != "TeamAncients"){
// Debug.LogWarning("UnknownTag");
//}
}
//Finds the closest object with the Enemytag
void FindClosest() {
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag(Enemytag);
GameObject closest;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject go in gos) {
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance){
closest = go;
distance = curDistance;
Target = closest.transform;
}
}
}
//
void sendtarget () {
ReceiveTarget = Target;
//barrelobj.GetComponent(TurrentGun).GetTarget(ReceiveTarget);
//GameObject.GetComponentInChildren(barrelobj).GetTarget(ReceiveTarget);
}
// Update is called once per frame
void Update () {
SearchTime += Time.deltaTime;
if (SearchTime >= SearchRate){
SearchTime = 0;
FindClosest();
}
}
}
---TurrentBase.Js---
var goTarget : GameObject;
var maxDegreesPerSecond : float = 30.0;
private var qTo : Quaternion;
private var target;
function Start () {
target = GameObject.FindWithTag("Player"); //To be removed
qTo = target.transform.localRotation; //To be removed
}
//Gets the target from "TeamIder.cs"....well its suposed to
function GetTarget(ReceiveTarget){
target = ReceiveTarget;
}
//Rotates the base to point at the target
function RotateAt(){
qTo = target.transform.localRotation;
var v3T = target.transform.position - transform.position;
v3T.y = transform.position.y;
qTo = Quaternion.LookRotation(v3T, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, maxDegreesPerSecond * Time.deltaTime);
}
//calls the RotateAt function...tho you already knew that
function Update () {
RotateAt();
}
---TurrentGun.Js---
var goTarget : GameObject;
var maxDegreesPerSecond : float = 30.0;
private var qTo : Quaternion;
private var target;
function Start () {
target = GameObject.FindWithTag("Player"); //To be removed
qTo = target.transform.localRotation; //To be removed
}
//Gets the target from "TeamIder.cs"....well its suposed to
function GetTarget(ReceiveTarget){
target = ReceiveTarget;
//barrelobj.GetComponent(TurrentGun).GetTarget(ReceiveTarget);
}
//Raises the barrel to point at the target
function PointAt(){
var v3T = goTarget.transform.position - transform.position;
var v3Aim : Vector3;
v3Aim.x = 0.0;
v3Aim.y = v3T.y;
v3T.y = 0.0;
v3Aim.z = v3T.magnitude;
qTo = Quaternion.LookRotation(v3Aim, Vector3.up);
transform.localRotation = Quaternion.RotateTowards(transform.localRotation, qTo, maxDegreesPerSecond * Time.deltaTime);
}
//calls the PointAt function...tho you already knew that
function Update () {
PointAt();
}
Here is a screen shot of the turret. The Black cube has no scripts and is just to hold the turret in place. The "TeamIder.cs" and "TurrentBase.Js" scripts are attached to an empty game object. The yellow box is the base of the turret that rotates to face the target. The green cube holds the "TurrentGun.Js" script. The white ball has a tag of "TeamHumans" so as to act as a test for the tracking.
Turret hierarchy:
-TempBlock =Black cube
-Rotatepoint =empty game object
-Base =Yellow cube
-TurretBlock =Green cube
-BarrelA =Red cube
-BarrelB =Red cube
extra things to fix:
At the moment the turret is rotating back about 10 degrees on the X rotation and rotates up to 45 degrees down when the target is within a distance of 10(meters?), The turret needs to be locked to the local X rotation.
also the "BarrelA" and "BarrelB" objects do not rotate WITH the TurretBlock, they seem to rotate around it.
Your answer
Follow this Question
Related Questions
target tracking with lead time 1 Answer
How to make a turret facing to player both parented to sphere ? 0 Answers
Turret script bug 2 Answers
multipe targets 1 Answer