- Home /
Distance sensor
I have made a collision sphere parented to my enemy so that if my players trigger enters the collision sphere then it moves and attacks. just something to make it more realistic so i have two scrips one for the enemy and one for the sphere, the one forthe sphere has a static variable set to false and when my players trigger enters it, it turns to true and another function the turns it back to false when the trigger exits. the thing im having trouble with is on the other script. i have it so the rest of the AI charecteristics work but i tried to make it
function Update () {
if(ISDETECTED) {
var rotate = Quaternion.LookRotation(LookAt.position-transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate,
Time.deltaTime * damp);
transform.position += transform.forward * Mspeed * Time.deltaTime;
var seconds : int = Time.time;
var oddeven = (seconds % 2);
if(oddeven) Shoot(seconds);
}
}
i know everything but the if statement is correct how do i fix it
Answer by skovacs1 · Nov 08, 2010 at 11:47 PM
Not everything but the if statement is correct.
Every frame, if ISDETECTED (which is a terrible variable name), you create a look rotation to look along LookAt(which is an even worse variable name since it is also the name of a function)'s position-the current poisition with up along the world up vector and then slerp your rotation by Time.deltaTime * damp and move along forward.
Slerping from transform.rotation when you are changing transform.rotation by some non-incrementing value will create a smoothing at the end of the slerp.
Slerping by Time.deltaTime makes your slerp framerate dependent. If your frame rate gets really slow, your slerp will jump to look at the target and if it gets really fast, your slerp may barely move. If you are Slerping by a non-incrementing value, you should probably use a constant in stead so that your behaviour is at least consistent, although by slerping with a non-incrementing value, your rotation will never exactly reach the target rotation.
You then get the number of seconds that have passed cast to an int (which may round) and get whether the returned value is odd. If the amount of time is odd (by coercing an integer into a boolean), you call Shoot. For an entire second, every frame, that happens that second, you call Shoot. Is this what you want?
To fix it the calling of Shoot every frame for a whole second, you could include a boolean or time to track when last you shot and whether you should shoot again or add a yield statement to Shoot and then you could just drop the second calculation entirely.
There are a lot of ways to fix the things wrong with your script, but since you really don't describe which broken part of your script is causing you problems and in what way, it's really quite hard to give a specific answer.