- Home /
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!
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); }
no, none of them go they just all stay at the spawnPoint :(
actually I got it to work, they stay seperate, but they still stop
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 progra$$anonymous$$g, can u help me with this?
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.