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 WolfTitan · Jan 24, 2012 at 10:08 PM · enemyproximity

Expecting : found ; error

Im wanting to make a script where whenever you get within Range #1 of a turret, the turret will look at you then start moving towards you. Then, when you get within Range #2, it will start shooting at you. I keep getting the expecting : found ; error and when I do as it says, it loads 3 new errors. Could someone please fix the script to where my intentions may be achieved? Here is the script:

 var distanceTillShoot = 30;
 var distanceTillLook = 15; 
 var target : Transform;
 var damp = 6.0;
 var laserPrefab : Transform;
 var savedTime = 0;
 var laserSpeed = 10000;
 var targetTag : String;
 var lookSwitch : Boolean = false;
 var movementSpeed = 5;
 
 function Start ()
 {
     var tar = GameObject.FindWithTag(targetTag);
     if(tar)
     lookAtTarget = tar.transform;
 }
 
 function Update () 
 {
 /*    if(lookAtTarget)
     {
         var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
         transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
         var seconds : int =  Time.time;
         var oddeven = (seconds % 50);
 */
     if(lookAtTarget)
         {
             Look(seconds);
             if(lookSwitch == true)
             { Shoot(seconds);
             }
         }
 }
 
         
 function Look(seconds)
 {
  var distance = Vector3.Distance(LookAtTarget.transform.position, transform.position);
    if (distance <= distanceTillLook)
    {
      if(seconds!=savedTime)
      lookSwitch = true;
     }
     {
     transform.lookAt(target);
     transform.Translate(Vector3.forward*movementSpeed*Time.deltaTime);    
     
 }
 function Shoot(seconds)
 {
  var distance = Vector3.Distance(LookAtTarget.transform.position, transform.position);
    if (distance <= distanceTillShoot)
    {
      if(seconds!=savedTime)
         var laser = Instantiate(laserPrefab,transform.Find("spawnPointCanon").transform.position , transform.rotation);
       }
       {
         gameObject.tag : "enemyProjectile";
         laser.rigidbody.AddForce(transform.forward * laserSpeed);
       }
       {
         savedTime=seconds;
       }
     if (distance > distanceTillShoot)
          {
         lookSwitch = false;        
      if(seconds!=savedTime)
    }
 }
Comment
Add comment · Show 3
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 Lo0NuhtiK · Jan 24, 2012 at 10:15 PM 0
Share

Format your code first so we can read it without going cross-eyed.

avatar image Eric5h5 · Jan 24, 2012 at 10:33 PM 0
Share

Also point out exactly where the errors are.

avatar image WolfTitan · Jan 24, 2012 at 10:37 PM 0
Share

The error is located at the "transform.lookAt(target);" section below function Shoot

2 Replies

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

Answer by Tasarran · Jan 26, 2012 at 07:50 AM

This code is fixed, but it doesn't look complete; there are two variables that haven't been declared (LookAtTarget and seconds), there is also an unfinished if statement toward the end...

But the compile errors are fixed.

There's a couple of things I think will help you, if you remember to do them: 1) whenever you start a new function or if statement, go ahead and type the closing brace, and type inside of the braces. This way, you'll never forget to add it. 2) always line up the closing braces with the line that they are closing, and indent anything inside. In other words, make sure the contents of the if statement are indented, and the closing brace is lined up even with the if line.

 var distanceTillShoot = 30;
 var distanceTillLook = 15; 
 var target : Transform;
 var damp = 6.0;
 var laserPrefab : Transform;
 var savedTime = 0;
 var laserSpeed = 10000;
 var targetTag : String;
 var lookSwitch : boolean = false;
 var movementSpeed = 5;
 
 function Start () {
     var tar = GameObject.FindWithTag(targetTag);
     if(tar) {
         lookAtTarget = tar.transform;
     }
 }
 
 function Update () {
     if(LookAtTarget) {
         Look(seconds);
         if(lookSwitch == true) {
             Shoot(seconds);
         }
     }
 }
 
 
 function Look(seconds) {
     var distance = Vector3.Distance(LookAtTarget.transform.position, transform.position);
     if (distance <= distanceTillLook) {
         if(seconds!=savedTime)
         lookSwitch = true;
     }
     transform.lookAt(target);
     transform.Translate(Vector3.forward*movementSpeed*Time.deltaTime);  
 }
 
 function Shoot(seconds) {
     var distance = Vector3.Distance(LookAtTarget.transform.position, transform.position);
     if (distance <= distanceTillShoot) {
         if(seconds!=savedTime)
         var laser = Instantiate(laserPrefab,transform.Find("spawnPointCanon").transform.position , transform.rotation);
     }
     gameObject.tag = "enemyProjectile";
     laser.rigidbody.AddForce(transform.forward * laserSpeed);
     savedTime=seconds;
     if (distance > distanceTillShoot) {
         lookSwitch = false;
         if(seconds!=savedTime) {
         }
     }
 }
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 WolfTitan · Jan 26, 2012 at 01:41 PM 0
Share

Thanks for the help. It works for the range, but I'm still working on making it shoot when within the second range.

avatar image
0

Answer by Tasarran · Jan 25, 2012 at 09:35 PM

The reason fixing one error is finding more is because there are multiple errors. Welcome to software debugging!

Pretty sure what's crashing you is the extra { just before the line

 transform.lookAt(target);

in Look(seconds)

And the last part has messed up braces, too. You don't have an opening brace for the if statement

 if(seconds!=savedTime)

but you do have a closing one. Also, why are you bracketing stand alone statements? If you just put:

{
  statement;
  statement;
}

That doesn't do anything except make your code harder to read. You do this two or three times in that last function...

Also, I think this line:

gameObject.tag : "enemyProjectile";

should be

gameObject.tag = "enemyProjectile";
Comment
Add comment · Show 4 · 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 WolfTitan · Jan 26, 2012 at 04:15 AM 0
Share

Thanks for your input. Im still getting the same errors and since I'm new to scripting, I'm not knowing if I'm even placing the code in the right order or when to put brackets.

avatar image syclamoth · Jan 26, 2012 at 04:20 AM 0
Share

It helps if you do more than just copy-paste other people's code, though.

avatar image WolfTitan · Jan 26, 2012 at 04:25 AM 1
Share

I don't just do that. I try to put my own script together. Though they are basic. Im self $$anonymous$$ching myself this stuff by looking at other peoples scripts and learning the keywords in each code.

avatar image Tasarran · Jan 26, 2012 at 07:32 AM 0
Share

Well, putting in matched sets of braces won't HURT you, i's just confusing.

The reason you put code in brackets is to say that it is to be executed together.

if you just put an if statement on on line, it automatically knows that the next thing is what it needs to execute. By putting braces around multiple statements, you tell the if statement that it needs to do ALL of those things.

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

7 People are following this question.

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

Related Questions

How to activate / deactivate an enemy on player proximity 4 Answers

enemy waypoints and detection scipting 2 Answers

Enemy follows player then attacks! 2 Answers

How do I stop a function from executing? 1 Answer

Trap Door Question 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