- Home /
tornado twins worm game tutorial turret not firing
im trying to follow the youtube tutorial, but i cannot make the turret to fire, the script is exactly the same, and i do have a spawn point, script is:
var LookAtTarget:Transform;
var damp = 6.0;
var bullitPrefab:Transform;
function Update ()
{
if(LookAtTarget)
{
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
Shoot();
}
}
function Shoot()
{
var bullit = Instantiate(bullitPrefab, transform.find("SpawnPoint").transform.position ,
Quaternion.identity);
bullit.rigidbody.AddForce(transform.forward * 1000);
}
please hit edit -> select all code -> hit the 1010101 button to format your code.
I was about to blame 'bullit' for the difficulties (since it's supposed to be 'bullet'... at least in the english language I learned at school) but it seems to be consistently (ab)used, so it's probably not the problem... o.O
can you start the game? any error-messages in the console? or is it just not doing what it should? describe a little more what's going on.
Ok, try this. add inside the if(LookAtTarget)-statement:
print("I have a Target");
and add inside the Shoot-function at the beginning:
print("I'm trying to shoot");
and add inside the Shoot-function at the end:
print(bullit);
this way you can tell where the problem is happening.
There is any error message?
Do you have a "SpawnPoint" object in the scene?
A hint: try to Understand the tutorial, don't just make exactly as said. Try to understand each peace of code.
@Borgo hrm... his formatting and answer-ins$$anonymous$$d-of-comment makes it less accessible, but he already answered both questions. He gets no error-messages and has a spawnpoint. What makes you doubt he's trying to understand the tutorial?
@Sister$$anonymous$$y: Sorry, I did not see
And my comment about "understand" is because he is writing exactly the video as required, including spelling errors.
Answer by kgoswami · Sep 08, 2011 at 07:01 AM
Change "find" to "Find" with a capital "F" here is the line of code. Also make sure your spelling of the "spawnPoint" is correct. Check the turret for spawnPoint object name.
var bullit= Instantiate(bullitPrefab,transform.Find("spawnPoint").transform.position,Quaternion.identity); //create bullet
Also after doing this code and adding it to your turret, click the turret object in your heirarchy and drag your worm object into the LookAtTarget Prefab box on the inspector window and the fireball prefab into the bullitprefab box of your inspector window. Also one more thing is that do not add your worm prefab as your lookattarget, add the worm object from your scene heirarchy. But I can see that u are not having problems with the looking part. also you can make thw turret actually move closer to your character uptil a certain distance.
Here is the code.
var ahead=transform.TransformDirection(Vector3(0,0,1)); //Go ahead direction
var distance=Vector3.Distance(LookAtTarget.position,transform.position); //calculate distance between turret and player
if(distance>enemyStopDist) //keep distance from player
{
turretController.SimpleMove(ahead*turretMovSpeed);
}
//Here, enemyStopDist is a var that is the distance at which the turret will stop moving closer to your worm
Hope this helps.
Answer by kgoswami · Sep 08, 2011 at 07:19 AM
A better way is to not use Find. Just as you have var bullitPrefab:Transform and dragged your fireball into it you can use var spawnPointPrefab:Transform. After doing this follow these steps. Make your spawnpoint object into a transform by dragging it from the Heirarchy to project window. Drag your spawnPoint object(not prefab, but the one in your heirarchy window) from the turret object (not prefab) into the turret>inspector>turretControl>spawnPointPrefab box.
This way the unity run time environment does not have to search for every object to find your spawnPoint.
Your answer
![](https://koobas.hobune.stream/wayback/20220612195011im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I need a turret script 4 Answers
Please help with Turret rate of fire 1 Answer
TornadoTwins Tutorial Problem 4 Answers
Scripting error comes up when I press play 1 Answer
Turret Script Problem 2 Answers