Готовый перевод C# Game Programming Cookbook for Unity 3D / Программирование игр на Unity 3D C#. Книга готовых решений.: 6.1.2.1 Разбор скрипта

Внимание! Этот перевод, возможно, ещё не готов, так как модераторы установили для него статус «идёт перевод»

The BaseWeaponScript class derives from MonoBehavior:

using UnityEngine;
using System.Collections;
public class BaseWeaponScript : MonoBehavior
{
public virtual void Start()
{
Init();
}

public virtual void Init()
{ // cache the transform
myTransform= transform;
// cache the layer (we’ll set all projectiles to avoid this
// layer in collisions so that things don’t shoot
// themselves!)
myLayer= gameObject.layer;
// load the weapon
Reloaded();
}

public virtual void Enable()
{
canFire=true;
}
public virtual void Disable()
{
canFire=false;
}

public virtual void Reloaded()
{
// the 'isLoaded' var tells us if this weapon is loaded and
// ready to fire
isLoaded= true;
}

public virtual void SetCollider( Collider aCollider )
{
parentCollider= aCollider;
}

public virtual void Fire( Vector3 aDirection, int ownerID )
{

// be sure to check canFire so that the weapon can be
// enabled or disabled as required! if( !canFire )
return;
// if the weapon is not loaded, drop out if( !isLoaded )
return;

// if we’re out of ammo and we do not have infinite ammo,
// drop out... if( ammo<=0 && !isInfiniteAmmo )
return;

// decrease ammo
ammo--;
// generate the actual projectile FireProjectile( aDirection, ownerID );

// we need to reload before we can fire again
isLoaded= false;

// schedule a completion of reloading in <reloadTime>
// seconds: CancelInvoke( "Reloaded" ); Invoke( "Reloaded", reloadTime );
}

public virtual void FireProjectile( Vector3 fireDirection, int
ownerID )
{

// make our first projectile theProjectile= MakeProjectile( ownerID );

// direct the projectile toward the direction of fire theProjectile.LookAt( theProjectile.position + fireDirection );

// add some force to move our projectile
theProjectile.rigidbody.velocity= fireDirection *
projectileSpeed;
}

Внимание! Этот перевод, возможно, ещё не готов.
Его статус: идёт перевод

Переведено на Нотабеноиде
http://notabenoid.org/book/55263/242508

Переводчики: ilia1395

Настройки

Готово:

31.37% КП = 1.0

Непереведённые фрагменты будут пропущены.