- Home /
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);
}
}
}
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.
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.
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.
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?
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)
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.
Answer by greg1992 · Mar 17, 2013 at 08:33 PM
Any one have an idea?
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.
[1]: http://answers.unity3d.com/questions/388185/make-the-turret-automatically-rotate-to-look-at-wh.html#answer-389079++
Your answer
Follow this Question
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