Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by tabpol95 · Oct 12, 2014 at 02:37 PM · rotatearoundturrettracking

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
 

alt text


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.

alt text

sddasdasdsad2.png (11.8 kB)
sddasdasdsad.png (9.1 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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

2D Tower Defense: Turret facing enemy 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges