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

Оглавление
← →
Английский оригинал Перевод на русский

Сожалеем, но текст оригинала доступен только зарегистрированным пользователям.

The BaseWeaponScript class derives from MonoBehavior:

ilia1395 25.10.14 в 22:10

0

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

ilia1395 25.10.14 в 22:10

0

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();
}

ilia1395 25.10.14 в 22:10

0

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

ilia1395 25.10.14 в 22:11

0

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

ilia1395 25.10.14 в 22:11

0

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

ilia1395 25.10.14 в 22:11

0

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

ilia1395 25.10.14 в 22:11

0

// 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;

ilia1395 25.10.14 в 22:11

0

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

ilia1395 25.10.14 в 22:11

0

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

ilia1395 25.10.14 в 22:11

0

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

ilia1395 25.10.14 в 22:12

0

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

ilia1395 25.10.14 в 22:12

0

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

ilia1395 25.10.14 в 22:12

0

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

ilia1395 25.10.14 в 22:12

0

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

ilia1395 25.10.14 в 22:12

0

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

ilia1395 25.10.14 в 22:12

0
Минутку...

Следующая страница →