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 kris · Aug 14, 2010 at 12:06 AM · turrettornado-twins

I need a turret script

ok im doing the worm game tutorial (tornado twins) and I cant get the turret script to work, ive got it to fire in intervals, but it just does a masive spay for one second and has a rest for the next. heres my code: (ps I would apreciate it if somone would post there turret code so that way i dont have to mess around, i think below is enough to say I have tried my hardest. oh lol, ive just realised it cuts it off, http://www.youtube.com/watch?v=raNfTotAGQw thats a screen video i did of my problem with the code in the botum bar, thanks

var LookAtTarget : Transform; var damp : float = 6.0; var bullitPrefab : Transform; var savedTime = 0;

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 % 2); if(oddeven) Shoot(seconds); //transform.LookAt(LookAtTarget); }

}

function Shoot(seconds) { if(seconds!=savedTime) { var bullit = Instantiate(bullitPrefab ,transform.Find("spawnPoint").transform.position , Quaternion.identity); bullit.rigidbody.Addforce(transform.forward * 1000); savedTime=seconds; } }

Comment
Add comment · Show 1
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 Julian-Glenn · Aug 14, 2010 at 12:21 AM 0
Share

I haven't done their toots, but I know that they produce good work. I'd say retrace all your steps. Or alternatly use the Unity FPS tutorialwhich has some cool turret and enemy AI to disect: http://unity3d.com/support/resources/tutorials/fpstutorial

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by _Petroz · Aug 14, 2010 at 07:14 AM

I think your code looks OK but I noticed there was an error in the log when you opened Unity so maybe there is a compile error with your code. If there is an error Unity will use the old version of the code, so make sure there are no errors in that Console. You can jump to the error by double-clicking it the console.

I have watched the this tutorial, but I didn't make the game. While I did learn quite a lot, I think their code is quite poorly written. Here is a clearer version of that code:

var LookAtTarget : Transform; var damp : float = 6.0; var bulletPrefab : Transform; var nextShotTime : float = 0.0; var timeBetweenShots : float = 2.0;

function Update() { if(LookAtTarget) { var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);

       if (nextShotTime <= Time.time)
       {
           Shoot();
           nextShotTime = Time.time + timeBetweenShots;
       }
  }

}

function Shoot() { var bullet = Instantiate(bulletPrefab, transform.Find("spawnPoint").transform.position, Quaternion.identity); bullet.rigidbody.Addforce(transform.forward * 1000); }

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 LANDO · May 18, 2011 at 07:10 AM 0
Share

This did the trick for me (I'm making a dodgeball game and only need a projectile every 1second or so..). Thank you!

avatar image
0

Answer by BLF-Games · Nov 27, 2010 at 02:54 PM

I think I found your problem... below the modified code (just one var missplaced :p)

var LookAtTarget : Transform; var damp : float = 6.0; var bullitPrefab : Transform; var savedTime = 0;

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 % 2); if(oddeven) Shoot(seconds); //transform.LookAt(LookAtTarget); }

}

function Shoot(seconds) { if(seconds!=savedTime) { var bullit = instantiate(bullitPrefab,transform.Find("spawnPoint").transform.position , Quaternion.identity); bullit.rigidbody.Addforce(transform.forward * 1000); // Put this -> savedTime=seconds; }

  //here
  savedTime=seconds;

}

Let me know if it worked ;)

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 pixelplacement · Dec 10, 2010 at 06:53 PM

There's a decent turret solution in one of my iTween examples: http://itween.pixelplacement.com/examples.php

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 tommbomm126 · Jul 14, 2011 at 11:59 PM

I've got a problem aswell i copied and pasted petroz's script but this was the error that it came up with 359 times

NullReferenceException
TurretControl.Shoot () (at Assets/TurretControl.js:25)
TurretControl.Update () (at Assets/TurretControl.js:18)

please help me

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 Chris D · Jul 15, 2011 at 12:05 AM 0
Share

Please don't post comments as Answers. This question is quite old; if you're having problems, open a new question and link back to this one for reference.

That being said, learning to interpret those kind of errors is a basic program$$anonymous$$g skill. Look at lines 18 and 25 in your code and you'll likely find the source of the problem.

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

enemy AI script 3 Answers

Turret Script Problem 2 Answers

i need a c# of the tornado twins move around script 0 Answers

Turret Range Script 2 Answers

Turret shooting at me 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