• 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
1
Question by Bruno-Fludzinski · Jun 20, 2012 at 01:11 AM · shootingshootturrettornado-twins

Turret shooting at me

Hi im using Tornado Twins' YouTube series on how to make a turret shoot at me using this script:

     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;
      }
 }

I put my missile prefab on it, which i made in the Unity FPS tutorial, explodes when it hits a wall and makes sound. When I attach the script to the turret it just shoots tons and tons of missiles, so fast that they hit each other and explode. Can someone help fix this problem please? 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

1 Reply

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

Answer by aldonaletto · Jun 20, 2012 at 01:30 AM

This code uses a weird method to space the shots: it only shoots when the time changes to a new odd second (1, 3, 5 etc.)
I would use a different approach: after each shot, set a dead time - this allows control of the shot interval (which's fixed in the Tornado Twins code).

var LookAtTarget : Transform; var damp : float = 6.0; var bullitPrefab : Transform; var shotTime: float = 0; var shotInterval: float = 2;

function Update () { if(LookAtTarget) { var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp); if (Time.time > shotTime){ Shoot(); shotTime = Time.time + shotInterval; } } }

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

Comment
Add comment · Show 5 · 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 Bruno-Fludzinski · Jun 20, 2012 at 01:40 AM 0
Share

no, none of them go they just all stay at the spawnPoint :(

avatar image Bruno-Fludzinski · Jun 20, 2012 at 01:48 AM 0
Share

actually I got it to work, they stay seperate, but they still stop

avatar image Bruno-Fludzinski · Jun 20, 2012 at 02:02 AM 0
Share

I fixed it!

avatar image ruifernandes01 · Jun 27, 2012 at 08:49 AM 0
Share

hey thanks a lot! i'm also using that code, but i have a question, i'm making a boat game so i need the boat to look at me, but with his lateral part, but i cant see a way to add this bit of script:

Rotate(90 * Vector3.up), this is all i need, but i'm not a guru of programing, can u help me with this?

avatar image Bruno-Fludzinski · Jun 27, 2012 at 04:57 PM 0
Share

you can add Transform.LookAt ( http://docs.unity3d.com/Documentation/ScriptReference/Transform.LookAt.html )

and a normall rotation script (unity comes with it its called rotate.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Turret Script Problem 2 Answers

Turret Range Script 2 Answers

Launch a projectile from one object to another 1 Answer

How can i set up a turret that turns and shoots where you touch onscreen?,how to shoot where you touch 2 Answers

How to make a tps gun [C#] 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges