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 BobbleHead · Sep 01, 2012 at 09:58 AM · cameraenemystaticpublic

script is treating all enemies as one, instead of them as individuals.

I've got myself a script that is attached to the camera which simply detects when target is at screen edge and tells the enemy2script to turn it around. However, when more than on enemy of type is on screen; the enemies act as one, and turn together and not at the edge. This has confused me.

public var pos : Vector3 = camera.WorldToViewportPoint(but had no idea what went here);

This is the main script; in it's basic form; It's how it was before I wanted it to affect more than one enemy of the same type.

 var target: Transform;
 static var turnaround : boolean = false;
 static var turnaround2 : boolean = false;
 
 //public var pos : Vector3 =  camera.WorldToViewportPoint();??
 
 function Update () {    
 
      target = GameObject.FindWithTag("Enemy2").transform; 
 
     var pos : Vector3 =  camera.WorldToViewportPoint (target.position);
 
     if(pos.x < 0.0){
     turnaround = !turnaround;
     }
     if(1.0 < pos.x){
     turnaround = !turnaround;
     }
     if(pos.y < 0.0){
     turnaround2 = !turnaround2; 
     }
     if(1.0 < pos.y){
     turnaround2 = !turnaround2;
 
     }
 
 }
 



Then this is accessed in my enemy2Script:

 function Start(){
 
     var someScript : enemy2CameraScript;
     someScript = gameObject.GetComponent("enemy2CameraScript");
     
     var someScript2 : multiplierScript;
     someScript2 = gameObject.GetComponent("multiplierScript");
 
 }
 
 function Update () {
 
     if(enemy2CameraScript.turnaround==true){    
            transform.eulerAngles = Vector3(0,0,180);
         }
         
     if(enemy2CameraScript.turnaround==false){    
            transform.eulerAngles = Vector3(0,0,0);
         }
         
     if(enemy2CameraScript.turnaround2==true){    
            transform.eulerAngles = Vector3(0,0,180);
         }
     
     if(enemy2CameraScript.turnaround2==false){    
            transform.eulerAngles = Vector3(0,0,0);
         }
 }



Knowing my luck I've missed something simple, but I'm now just confused.

Thanks in advance.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by BobbleHead · Sep 01, 2012 at 03:09 PM

Here is my fix, only took a morning (fml)

   var viewPos = Camera.main.WorldToViewportPoint(transform.position);
   transform.position = Camera.main.ViewportToWorldPoint(viewPos);
 
         
    if(0.0> viewPos.x){
            transform.eulerAngles = Vector3(0,0,0);
     }
     
     if(1.0 < viewPos.x){
            transform.eulerAngles = Vector3(0,00,180);
     }
     
     if(viewPos.y < 0.0){
            transform.eulerAngles = Vector3(0,0,0);
     }
     
     if(viewPos.y > 1){
            transform.eulerAngles = Vector3(0,0,180);
            
        }
Comment
Add comment · Share
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
avatar image
0

Answer by Kryptos · Sep 01, 2012 at 10:42 AM

Two mistakes here.

The following code will always return the same object.

 target = GameObject.FindWithTag("Enemy2").transform;

Therefore only one enemy will be checked.


You're using static variables. Thus your enemy2Script are always reading the same value. Combined with your first mistake, all enemies will turn if the first one is at the edge.


What can you do? Don't put the code to test the position in the camera script but in the enemy script. This way all enemy will do this check separately. Don't use static variables.

Comment
Add comment · Show 1 · Share
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
avatar image BobbleHead · Sep 01, 2012 at 10:54 AM 0
Share

That makes a lot of sense; I went through many iterations trying to get things to connect. (damn!)

If I don't use Static variables, generally scripts won't reverence variables all that well (I was under that impression, anyway) I'm sure I've been doing it wrong now, hah.

Finally, we come to my main problem; how do I test whether player is in camera view etc, if thew script isn't attached to camera? I will look into this myself now; but that is where i struggled to begin with.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't assign camera by dragging it into GUI 2 Answers

How to make camera position relative to a specific target. 1 Answer

is there a diffrence btween public var and static var javascript 1 Answer

error CS1525: Unexpected symbol `public' 2 Answers

First Person Phone Camera Effect 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