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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by greg1992 · Mar 15, 2013 at 03:52 AM · turrettower

Turret system not working c#

I'm trying aid my designers in being able to build part of a tower defence game, i've found an apparent working code online however i can't seem to get anything to work.(nothing firing out of a turret prefab) Turret Code

      using System.Collections;
         
          
         
          
         
          
         
         public class TurretScript : MonoBehaviour {
         
          
         
             
         
          
         
             
         
          
         
             
         
          
         
             public float reloadTime = 1f;
         
          
         
             public float firePauseTime = .25f;
         
          
         
             
         
          
         
             
         
          
         
             private Transform myTarget = null;
         
          
         
             private float nextFireTime;
         
          
         
             private float nextMoveTime;
         
          
         
             private Quaternion desiredRotation;
         
          
         
             public GameObject Bullet;
         
          
         
             
         
          
         
          
         
          
         
             // Use this for initialization
         
          
         
             void Start () {
         
          
         
             
         
          
         
             }
         
          
         
             
         
          
         
             // Update is called once per frame
         
          
         
             void Update () {
         
          
         
                 
         
          
         
                 if (myTarget!=null)
         
          
         
                 {
         
          
         
                     if(Time.time >= nextMoveTime)
         
          
         
                     {
         
          
         
                         CalculateAimPosition(myTarget.position);    
         
          
         
                         
         
          
         
                     }
         
          
         
                 
         
          
         
                     if(Time.time >= nextFireTime)
         
          
         
                     {
         
          
         
                         FireProjectile ();  
         
          
         
                         
         
          
         
                     }
         
          
         
                 }
         
          
         
             
         
          
         
             }
         
          
         
             
         
          
         
             void OnTriggerEnter(Collider other){
         
          
         
                 
         
          
         
                 if (other.gameObject.tag == "Enemy")
         
          
         
                 {
         
          
         
                     nextFireTime = Time.deltaTime + (reloadTime*0.75f);
         
          
         
                     myTarget = other.gameObject.transform;
         
          
         
                     
         
          
         
                 }
         
          
         
                 
         
          
         
                 
         
          
         
             }
         
          
         
             
         
          
         
             void OnTriggerExit(Collider other){
         
          
         
                 
         
          
         
                 if (other.gameObject.transform == myTarget)
         
          
         
                 {
         
          
         
                     myTarget = null;    
         
          
         
                 }
         
          
         
                 
         
          
         
                 
         
          
         
             }
         
          
         
             
         
          
         
             void CalculateAimPosition(Vector3 targetPos){
         
          
         
                 
         
          
         
                 Vector3 aimPoint = new Vector3(targetPos.x, targetPos.y, targetPos.z);
         
          
         
                 desiredRotation = Quaternion.LookRotation (aimPoint);
         
          
         
             }
         
          
         
          
         
          
         
          
         
          
         
             void FireProjectile(){
         
          
         
             
         
          
         
                 audio.Play();
         
          
         
                 nextFireTime = Time.time+reloadTime;
         
          
         
                 nextMoveTime = Time.time+firePauseTime;
         
          
         
                 
         
          
         
                 Instantiate (Bullet, transform.position, desiredRotation);
         
          
         
             }
         }


Bullet Code

 using UnityEngine;
 
 using System.Collections;
 
  
 
 public class BulletScript : MonoBehaviour {
 
     
 
     public float mySpeed = 5.0f;
 
     
 
     public float myRange = 1.5f;
 
     
 
     private float myDist;
 
  
 
     // Use this for initialization
 
     void Start () {
 
     
 
     }
 
     
 
     // Update is called once per frame
 
     void Update () {
 
     
 
         transform.Translate (Vector3.forward * Time.deltaTime * mySpeed);
 
         myDist += Time.deltaTime * mySpeed;
 
         if(myDist >= myRange)
 
             
 
         {
 
             Destroy(gameObject);
 
         
 
         }
 
         
 
 }
 
 }
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
2

Answer by robertbu · Mar 15, 2013 at 03:58 AM

You really need to format your code.

Your code creates a bullet but never adds any force to make it move. Replace line 309 by these two lines:

 GameObject go =  Instantiate (Bullet, transform.position, desiredRotation);
 go.rigidbody.AddForce(transform.forward * 1500);

Note the Bullet must have a Rigidbody component and you must drag the bullet prefab (a true prefab not a scene object) onto the Bullet variable in the inspector.

Comment
Add comment · Show 8 · 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 greg1992 · Mar 15, 2013 at 04:30 AM 0
Share

Thankyou for the speedy answer, however it hasn't seem to have worked -coding noob so on my end i bet-. void FireProjectile(){ audio.Play(); nextFireTime = Time.time+reloadTime; next$$anonymous$$oveTime = Time.time+firePauseTime; GameObject go = Instantiate (Bullet, transform.position, desiredRotation); go.rigidbody.AddForce(transform.forward * 1500); } }

The above code is giving me a error CS0266 message, removing the code so it's Instantiate (Bullet, transform.position, desiredRotation); rigidbody.AddForce(transform.forward * 1500);

gets rid of the error, however i of course mauled the code (in my head adding force to the rigidbody should work.

The bullet prefab as a rigidbody on it aswell as a collider, the turret (which is just a cube) prefab as similarly a collider and rigidbody attached.

avatar image greg1992 · Mar 15, 2013 at 05:00 AM 0
Share

Getting closer i hope, searched online about that error message which led me to trying out

  Rigidbody InstantiatedBullet =  (Rigidbody)Instantiate(Bullet,transform.position,transform.rotation);
 rigidbody.AddForce(transform.forward *1500);
 Debug.Log ("Bullet Fire");


Still not working of course.

avatar image robertbu · Mar 15, 2013 at 05:10 AM 0
Share

Sorry about missing the cast. What is happening? Are you getting an error. Is the bullet appearing in the hierarchy, and if so how is it moving?

avatar image greg1992 · Mar 15, 2013 at 05:23 AM 0
Share

Was the updated bit of code correct?

the bullet is not appearing at all. I don't know if it's because the turret i'm using is a Cube, so i tried changing the turret to a empty GameObject, with a collider (radius set) and rigidbody, still nothing.

Interestingly enough, i tried adding the bullet pre fab into the game, the moment i hit play it dissapears from scene and from the hierachy. Does this mean the code is working? just without it targeting my enemies/damaging them, i can't see? (debug.log still doesn't show though)

avatar image robertbu · Mar 15, 2013 at 05:32 AM 0
Share

I may have lead you astray. I assumed you wanted to fire bullets using Unity's Physics engine. This is how almost everyone else does it. But as a read this bullet code, it translates the bullet (i.e. moves it by hand). As a start, just remove the script from the bullet and try. Then get back to me about whether you want to use the Physics engine to fire a bullet. This may not be the best turret system to use as a basis for your project.

Show more comments
avatar image
0

Answer by greg1992 · Mar 17, 2013 at 08:33 PM

Any one have an idea?

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 robertbu · Mar 17, 2013 at 10:13 PM 0
Share

I stopped responding your question for several reasons.

First, you appear to be script shopping. That is looking for some ready-made solution rather than learning the program$$anonymous$$g to write your own scripts.

Second, I did not get the impression you understood what you want and needed. For example the script above use translate to move the bullet. You are going to have a hard time detecting collisions with this code. Plus you have conveyed very little about the specifications for your turrets.

A Last, any script from this site is unlikely to meet the criteria of a real project. Any programmer who knows Unity could write you a simple turret script like the one above in about 15 $$anonymous$$utes or so. But there is all the stuff that will come up as you move along. How to limit the turrets rotation so that it does not shoot through walls. How to create an object pooling system for all the ammunition. How to write detection code so that the turrets don't see through walls. These are just examples. The problems you might face will likely be different, but you are not going to solve them by getting "ready-made" scripts.

Here is a link to two scripts. One for turret rotation, one for gun elevation. There is no shooting code here. Put a spawn point in front of the barrel, make it child of the barrel, then you can take a look at just about any third-person shooting script on this site (there will be 1000's) to get the logic to fire a projectile from the end of the gun.

http://answers.unity3d.com/questions/388185/make-the-turret-automatically-rotate-to-look-at-wh.html#answer-389079[link text][1]

[1]: http://answers.unity3d.com/questions/388185/make-the-turret-automatically-rotate-to-look-at-wh.html#answer-389079++

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

10 People are following this question.

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

Related Questions

Turret AI doesnt hit enemy 2 Answers

Placing objects in TD Game 1 Answer

Lock rotation of object 4 Answers

Turret bullet rotation problem 1 Answer

instantiate works on one but not other object 0 Answers


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