- Home /
EMERGENCY!!! PLEASE HELP!!!!!
Ok. Sorry, but I am REALLY desperate... my script is half working. What it is supposed to be doing is turning a turret to look at my character, and firing one fireball per second or something, and it is looking, but it wont fire! yes I checked every possible error. For this I might need a really good coders help. I also dont mind if you request that I make a new code for this. Here is the code:
var LookAtTarget : Transform;
var damp = 1.0;
var BulletPrefab : Transform;
var savedTime;
function Update()
{
if(LookAtTarget)
{
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
var secs;
secs+= Time.deltaTime;
if(secs < 1)
{
Shoot();
secs=0;
}
}
}
function Shoot()
{
var bullet = Instantiate(BulletPrefab, gameObject.Find("spawnPoint1").transform.position, Quaternion.identity);
bullet.Rigidbody.AddForce(transform.forward * 1000);
}
THANKS SO MUCH FOR HELPING!!!!!
Dude, clean up your code blocks, I'm gonna have to copy paste into my own editor to even read it. Also, please don't use a title like that. We are all here for help, but with a title like that, people dont have any idea what you're asking about until they click. Some people will just ignore your post if you do that.
What is the variable "LookAtTarget" supposed to represent? is it a state that the turret is supposed to be in? (One state being looking, the opposite being not looking)
Answer by hoy_smallfry · Feb 13, 2013 at 01:50 AM
Ah, I just figured it out. See, every Update call, you are creating a variable called "secs". The state of that variable will cease to exist at the end of a it's scope. Usually, you can tell what a variable scope is by looking at the first set of brackets that surround it:
{
var x = 10;
{
var y;
// x also exists in scopes under the one it was created in
x = 34;
// y exists within these brackets, so i can do this:
y = 5;
}
// y no longer exists here - this will cause an error:
y = 5;
// x is still alive, though.
x = 0;
}
This is something that most imperative programming languages like Javascript and C# do. What it means to you is that at the end of the if statement, "secs" no longer exists. Every time the Update() starts over, you have an entirely new "secs" variable that starts at 0.
So, long story short, your "secs" variable will never get to 1 because the old values of Time.deltaTime never add up.
However, if you define secs outside the Update() function, it will stay alive throughout the entirety of the script's lifespan.
NOTE: In Unityscript, the scope of a variable declared inside a function is the entire function. It's different from C#, where a local variable only exists inside the brackets where it's defined (I made this same mistake too)
Yeah, they call that "hoisting".. yet another reason to avoid Unityscript.
Answer by Proportion1 · Feb 13, 2013 at 02:19 AM
your condition that gets the thing to shoot is wrong. it should be
if(secs > 1){
Shoot();
secs = 0;
}
cos like u want it to shoot after 1 sec yea?
Answer by aldonaletto · Feb 13, 2013 at 01:58 AM
Have you set the field LookAtTarget? You must drag the target object to it in the Inspector, otherwise nothing will happen. You should also set the field BulletPrefab, or tons of error messages will pop up in the console view. Finally, the secs variable is incredibly useless: it's created every Update, thus every time it starts with value 0, and pass the if - you should have projectiles being created like bubbles in front of your weapon.
You could change the code to something like this:
...
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
// replace that secs part with this:
if (Time.time > savedTime){ // if it's time to shoot...
Shoot(); // shoot...
savedTime = Time.time + 1; // and define the next time to shoot
}
}
}
function Shoot(){
...
Also define the savedTime type as float in its declaration:
var savedTime: float;
Anyway, pay attention to the error messages in the console view when running: many errors only appear at runtime. When a runtime error occurs, the function is aborted and all instructions after the error aren't executed.
Yours worked. However, I want it to shoot ONCE every second, not a lot forever.... Is there a way to do that?