- Home /
I need a turret script
ok im doing the worm game tutorial (tornado twins) and I cant get the turret script to work, ive got it to fire in intervals, but it just does a masive spay for one second and has a rest for the next. heres my code: (ps I would apreciate it if somone would post there turret code so that way i dont have to mess around, i think below is enough to say I have tried my hardest. oh lol, ive just realised it cuts it off, http://www.youtube.com/watch?v=raNfTotAGQw thats a screen video i did of my problem with the code in the botum bar, thanks
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 haven't done their toots, but I know that they produce good work. I'd say retrace all your steps. Or alternatly use the Unity FPS tutorialwhich has some cool turret and enemy AI to disect: http://unity3d.com/support/resources/tutorials/fpstutorial
Answer by _Petroz · Aug 14, 2010 at 07:14 AM
I think your code looks OK but I noticed there was an error in the log when you opened Unity so maybe there is a compile error with your code. If there is an error Unity will use the old version of the code, so make sure there are no errors in that Console. You can jump to the error by double-clicking it the console.
I have watched the this tutorial, but I didn't make the game. While I did learn quite a lot, I think their code is quite poorly written. Here is a clearer version of that code:
var LookAtTarget : Transform; var damp : float = 6.0; var bulletPrefab : Transform; var nextShotTime : float = 0.0; var timeBetweenShots : float = 2.0;
function Update() { if(LookAtTarget) { var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
if (nextShotTime <= Time.time)
{
Shoot();
nextShotTime = Time.time + timeBetweenShots;
}
}
}
function Shoot() { var bullet = Instantiate(bulletPrefab, transform.Find("spawnPoint").transform.position, Quaternion.identity); bullet.rigidbody.Addforce(transform.forward * 1000); }
This did the trick for me (I'm making a dodgeball game and only need a projectile every 1second or so..). Thank you!
Answer by BLF-Games · Nov 27, 2010 at 02:54 PM
I think I found your problem... below the modified code (just one var missplaced :p)
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); // Put this -> savedTime=seconds; }
//here
savedTime=seconds;
}
Let me know if it worked ;)
Answer by pixelplacement · Dec 10, 2010 at 06:53 PM
There's a decent turret solution in one of my iTween examples: http://itween.pixelplacement.com/examples.php
Answer by tommbomm126 · Jul 14, 2011 at 11:59 PM
I've got a problem aswell i copied and pasted petroz's script but this was the error that it came up with 359 times
NullReferenceException
TurretControl.Shoot () (at Assets/TurretControl.js:25)
TurretControl.Update () (at Assets/TurretControl.js:18)
please help me
Please don't post comments as Answers. This question is quite old; if you're having problems, open a new question and link back to this one for reference.
That being said, learning to interpret those kind of errors is a basic program$$anonymous$$g skill. Look at lines 18 and 25 in your code and you'll likely find the source of the problem.
Your answer
Follow this Question
Related Questions
enemy AI script 3 Answers
Turret Script Problem 2 Answers
i need a c# of the tornado twins move around script 0 Answers
Turret Range Script 2 Answers
Turret shooting at me 1 Answer