- Home /
Sidescroller turret Script
Hi i'm currently trying to make a sidescrolling platformer but i'm having some problems with a turret script, i know what i want it to do and so on but i cant seem to get it into code.
I want a very simple script, i just want it to look at the player when it's within a certain range and fire a bullet every -- seconds.
Some help to were i can find some help or some suggestion how to do it here would make me very happy.
Answer by duck · Oct 12, 2010 at 01:15 PM
Something like this should do the job. Create this Javascript script, and place it on your turret object. This should actually work fine for both 2D and 3D gameplay. Make sure your turret object is modeled so that its barrel is pointing along its forward axis.
The turret object will need a trigger collider (probably best to have a sphere collider), which acts as the range within which the turret will fire.
The player needs to be tagged "Player"
You also need to create and assign a bullet prefab, which must have a rigidbody component. This is so the turret can instantiate bullet objects as required.
The script works by starting a Coroutine if the player enters the trigger area. The Coroutine loops, keeping the turret pointed at the player, and fires a bullet every second. The loop ends if the player exits the zone (because target is set to 'null' if that happens).
Enjoy!
var bulletPrefab : Rigidbody; var bulletSpeed : float = 10; private var target : Transform;
function OnTriggerEnter(otherCollider : Collider) { if (otherCollider.CompareTag("Player")) { target = otherCollider.transform; Fire(); } }
function OnTriggerExit(otherCollider : Collider) { if (otherCollider.CompareTag("Player")) { target = null; StopCoroutine(); // aborts the currently running Fire() coroutine } }
function Fire() { while (target != null) { var nextFire = Time.time + 1; while (Time.time < nextFire) { transform.LookAt(target); yield WaitForEndOfFrame(); }
// fire!
var bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
bullet.velocity = transform.forward * bulletSpeed;
}
}
Thanks alot it really helped.
But could you explain the "target = collider.transform;" part, not really understanding it.
"target" is a variable which can contain a transform. A transform is a reference to an object's position,rotation & scale. So, when the player enters the trigger area, the variable "target" gets assigned a reference to the player's transform. (this is accessible from the collider passed into the function - although I have just realised that I probably should call that variable "otherCollider" to distinguish it from the trigger zone's own collider! - will edit now!)
Hi again, i just saw an error with your script, which i have tried to solve myself but with little success. The problem is that when i enter the collider more than once the turret will start shooting more than one bullet at the time.
As i said i tried to fix it with changing variables and stuff like that but i just cant get it to work properly.
Just wanted to add that what i mean is that if you enter, exit and then enter the range of the turret fast, it will start shooting one more bullet each time you enter the trigger.
I think this is because you end up with multiple coroutines running at the same time - they only stop if the target == null just after firing so if you exit and enter while the turret is waiting, a new coroutine is spawned, but the old one remains running because it didn't do the null check at the right moment. To ensure that the currently running coroutine stops, we could add additional checks to see whether "target==null" during the coroutine, and if so abort.
Answer by Rhys_Pieces · Mar 29, 2012 at 04:09 PM
I tried this script on my turret but i am getting an error
Assets/Turret Script.js(20,22): BCE0017: The best overload for the method 'UnityEngine.MonoBehaviour.StopCoroutine(String)' is not compatible with the argument list '()
how do i fix it?
Your answer
![](https://koobas.hobune.stream/wayback/20220613075420im_/https://answers.unity.com/themes/thub/images/avi.jpg)