- Home /
The question is answered, right answer was accepted
Turret Script Error
Hi all,
The following code is intended to print 'a' in the console and rotate a turret towards the player (or any game character, just not munitions or structures). Currently it does not. Can anyone point out what I have done wrong?
Thanks.
#pragma strict
// turret to turn
var turret : Transform;
// target
var target : Transform;
// angle to return to
var stationryAngle : float = 0.0;
function OnTriggerEnter (other : Collider) {
if (other.isTrigger || other.gameObject.tag != 'munition' || other.gameObject.tag != 'structure')
{
return;
}
else
{
target = other.gameObject.transform;
}
}
function OnTriggerExit (other : Collider) {
if (other.isTrigger || other.gameObject.tag != 'munition' || other.gameObject.tag != 'structure')
{
return;
}
else
{
target = null;
}
}
function Start ()
{
turret.transform.Rotate ( 0, stationryAngle, 0);
}
function Update ()
{
if (target != null)
{
RotateTowardsPosition (target.position, 1.0);
print('a');
}
else
{
print('b');
}
}
function RotateTowardsPosition (targetPos : Vector3, rotateSpeed : float) : float
{
// Y
// Compute relative point and get the angle towards it
var relative = transform.InverseTransformPoint(targetPos);
var angle = Mathf.Atan2 (relative.x, relative.z) * Mathf.Rad2Deg;
// add random inaccuracy
if ( angle < 0.5 && angle > -0.5)
{
angle = angle + ( Random.Range(-3, 3) );
SendMessage("Fire");
}
// Clamp it with the max rotation speed
var maxRotation = rotateSpeed * Time.deltaTime;
var clampedAngle = Mathf.Clamp(angle, -maxRotation, maxRotation);
// Rotate
transform.Rotate( 0, clampedAngle, 0);
// Return the current angle
return angle;
}
im having a similiar issue, i want to use this method but am havin trouble understandin how it works.
{@myth ps can i use some of the code u postd?}
Do you have code? If you do, could you post it and I'll try and create a solution.
Answer by aldonaletto · Apr 06, 2012 at 11:00 PM
There's a logic error in the OnTrigger events: if the target tag is "munition", it's not "structure", and vice-versa - thus the target will never be acquired. You could use a different logic:
function OnTriggerEnter (other : Collider) { if (!other.isTrigger && (other.tag == 'munition' || other.tag == 'structure')) { target = other.transform; } }
function OnTriggerExit (other : Collider) { if (!other.isTrigger && (other.tag == 'munition' || other.tag == 'structure')) { target = null; } }
This will only shoot non-trigger 'munition' or 'structure' objects. If you want to shoot anything but these two, the expression should be:
if (!other.isTrigger && other.tag != 'munition' && other.tag != 'structure')
Answer by Myth · Apr 07, 2012 at 12:00 AM
Here is the code, with that bug (and another couple) fixed
#pragma strict
// turret to turn
var turret : Transform;
// target
var target : Transform;
// angle to return to
var stationryAngle : float = 0.0;
function OnTriggerEnter (other : Collider) {
if (!other.isTrigger && other.tag != 'munition' && other.tag != 'Structure')
{
print ('v');
target = other.gameObject.transform;
}
}
function OnTriggerExit (other : Collider) {
if (!other.isTrigger && other.tag != 'munition' && other.tag != 'Structure')
{
return;
}
else
{
target = null;
}
}
function Start ()
{
turret.transform.Rotate ( 0, stationryAngle, 0);
}
function Update ()
{
if (target != null)
{
RotateTowardsPosition (turret, target.position, 20.0);
}
else
{
// print('b');
}
}
function RotateTowardsPosition (divice : Transform, targetPos : Vector3, rotateSpeed : float) : float
{
// Y
// Compute relative point and get the angle towards it
var relative = divice.transform.InverseTransformPoint(targetPos);
var angle = Mathf.Atan2 (relative.x, relative.z) * Mathf.Rad2Deg;
// add random inaccuracy
if ( angle < 0.5 && angle > -0.5)
{
angle = angle + ( Random.Range(-3, 3) );
SendMessage("Fire");
}
// Clamp it with the max rotation speed
var maxRotation = rotateSpeed * Time.deltaTime;
var clampedAngle = Mathf.Clamp(angle, -maxRotation, maxRotation);
// Rotate
divice.transform.Rotate( 0, clampedAngle, 0);
// Return the current angle
return angle;
}