Готовый перевод C# Game Programming Cookbook for Unity 3D / Программирование игр на Unity 3D C#. Книга готовых решений.: 9.3.1 Разбор скрипта
using AIAttackStates;
public class BaseArmedEnemy : ExtendedCustomMonoBehavior
{
The Start() function calls Init():
public void Start ()
{ // now call our script-specific init function
Init();
}
public void Init()
{ // cache our transform
myTransform= transform;
// cache our gameObject
myGO= gameObject;
if(weaponControl==null)
{
// try to find weapon controller on this gameObject
weaponControl= myGO.GetComponent<Standard_SlotWeapon
Controller>();
}
if(rendererToTestAgainst==null)
{
// we need a renderer to find out whether or not we
// are on screen, so let’s try and find one in our
// children if we don’t already have one set in the
// editor
rendererToTestAgainst= myGO.GetComponentInChildren
<Renderer>();
}
// if a player manager is not set in the editor, let’s try
// to find one
if(myPlayerManager==null)
{
myPlayerManager= myGO.AddComponent
<BasePlayerManager>();
}
myDataManager= myPlayerManager.DataManager;
myDataManager.SetName("Enemy");
myDataManager.SetHealth(thisEnemyStrength);
canFire=true;
didInit=true;
}
public virtual void Update ()
{
// if we are not allowed to control the weapon, we drop out
// here
if(!canControl)
return;
if(thisGameObjectShouldFire)
{
// we use doFire to determine whether or not to fire
// right now
doFire=false;
if( canFire )
{
if( currentState==AIAttackState.random_fire )
{
// if the random number is over x, fire
if( Random.Range(0,100)>98 )
{
doFire=true;
}
} else
if( currentState==AIAttackState.look_and_destroy )
{
if(Physics.Raycast( myTransform.position,
myTransform.forward, out rayHit ))
{
// is it an opponent to be shot at?
if( rayHit.transform.CompareTag
( tagOfTargetsToShootAt ) )
{
doFire=true;
}
}
} else {
// if we’re not set to random fire or look and
// destroy, just fire whenever we can
doFire=true;
}
}
if( doFire )
{
if(onlyFireWhenOnscreen && !rendererToTestAgainst.
IsVisibleFrom( Camera.mainCamera ))
{
doFire=false;
return;
}
// tell weapon control to fire, if we have a weapon
// controller
if(weaponControl!=null)
{
// tell weapon to fire
weaponControl.Fire();
}
// set a flag to disable firing temporarily (providing a
// delay between firing)
canFire= false;
// invoke a function call in <fireDelayTime> to reset
// canFire back to true, allowing another firing session Invoke ( "ResetFire", fireDelayTime );
}
}
}
public void ResetFire ()
{
canFire=true;
}
}
Внимание! Этот перевод, возможно, ещё не готов.
Его статус: идёт перевод
Переведено на Нотабеноиде
http://notabenoid.org/book/55263/242531
Переводчики: ilia1395
Настройки
Готово:
