ilia1395: переводы
C# Game Programming Cookbook for Unity 3D / Программирование игр на Unity 3D C#. Книга готовых решений.
→Below is the Camera_Third_Person.cs script:
0 Ниже скрипт Camera_Third_Person:
→public class Camera_Third_Person : BaseCameraController
{
public Transform myTransform;
public Transform target;
public float distance = 20.0f;
public float height = 5.0f;
public float heightDamping = 2.0f;
public float lookAtHeight = 0.0f;
public float rotationSnapTime = 0.3F;
public float distanceSnapTime;
public Vector3 lookAtAdjustVector;
private float usedDistance;
float wantedRotationAngle;
float wantedHeight;
float currentRotationAngle;
float currentHeight;
Quaternion currentRotation;
Vector3 wantedPosition;
private float yVelocity = 0.0F;
private float zVelocity = 0.0F;
public override void SetTarget( Transform aTarget )
{
target= aTarget;
}
void LateUpdate ()
{
if ( target == null )
return;
if ( myTransform==null )
{
myTransform=Transform;
}
wantedHeight = target.position.y + height; currentHeight = myTransform.position.y;
wantedRotationAngle = target.eulerAngles.y; currentRotationAngle = myTransform.eulerAngles.y;
currentRotationAngle = Mathf.SmoothDampAngle(currentRotation
Angle, wantedRotationAngle, ref yVelocity, rotationSnapTime);
currentHeight = Mathf.Lerp(currentHeight, wantedHeight,
heightDamping * Time.deltaTime);
wantedPosition = target.position; wantedPosition.y = currentHeight;
usedDistance = Mathf.SmoothDampAngle(usedDistance, distance,
ref zVelocity, distanceSnapTime);
wantedPosition += Quaternion.Euler(0, currentRotationAngle,
0) * new Vector3(0, 0, -usedDistance);
myTransform.position = wantedPosition;
myTransform.LookAt( target.position + lookAtAdjustVector );
}
}
{
public Transform myTransform;
public Transform target;
public float distance = 20.0f;
public float height = 5.0f;
public float heightDamping = 2.0f;
public float lookAtHeight = 0.0f;
public float rotationSnapTime = 0.3F;
public float distanceSnapTime;
public Vector3 lookAtAdjustVector;
private float usedDistance;
float wantedRotationAngle;
float wantedHeight;
float currentRotationAngle;
float currentHeight;
Quaternion currentRotation;
Vector3 wantedPosition;
private float yVelocity = 0.0F;
private float zVelocity = 0.0F;
public override void SetTarget( Transform aTarget )
{
target= aTarget;
}
void LateUpdate ()
{
if ( target == null )
return;
if ( myTransform==null )
{
myTransform=Transform;
}
wantedHeight = target.position.y + height; currentHeight = myTransform.position.y;
wantedRotationAngle = target.eulerAngles.y; currentRotationAngle = myTransform.eulerAngles.y;
currentRotationAngle = Mathf.SmoothDampAngle(currentRotation
Angle, wantedRotationAngle, ref yVelocity, rotationSnapTime);
currentHeight = Mathf.Lerp(currentHeight, wantedHeight,
heightDamping * Time.deltaTime);
wantedPosition = target.position; wantedPosition.y = currentHeight;
usedDistance = Mathf.SmoothDampAngle(usedDistance, distance,
ref zVelocity, distanceSnapTime);
wantedPosition += Quaternion.Euler(0, currentRotationAngle,
0) * new Vector3(0, 0, -usedDistance);
myTransform.position = wantedPosition;
myTransform.LookAt( target.position + lookAtAdjustVector );
}
}
0 public class Camera_Third_Person : BaseCameraController
{
public Transform myTransform;
public Transform target;
public float distance = 20.0f;
public float height = 5.0f;
public float heightDamping = 2.0f;
public float lookAtHeight = 0.0f;
public float rotationSnapTime = 0.3F;
public float distanceSnapTime;
public Vector3 lookAtAdjustVector;
private float usedDistance;
float wantedRotationAngle;
float wantedHeight;
float currentRotationAngle;
float currentHeight;
Quaternion currentRotation;
Vector3 wantedPosition;
private float yVelocity = 0.0F;
private float zVelocity = 0.0F;
public override void SetTarget( Transform aTarget )
{
target= aTarget;
}
void LateUpdate ()
{
if ( target == null )
return;
if ( myTransform==null )
{
myTransform=Transform;
}
wantedHeight = target.position.y + height; currentHeight = myTransform.position.y;
wantedRotationAngle = target.eulerAngles.y; currentRotationAngle = myTransform.eulerAngles.y;
currentRotationAngle = Mathf.SmoothDampAngle(currentRotation
Angle, wantedRotationAngle, ref yVelocity, rotationSnapTime);
currentHeight = Mathf.Lerp(currentHeight, wantedHeight,
heightDamping * Time.deltaTime);
wantedPosition = target.position; wantedPosition.y = currentHeight;
usedDistance = Mathf.SmoothDampAngle(usedDistance, distance,
ref zVelocity, distanceSnapTime);
wantedPosition += Quaternion.Euler(0, currentRotationAngle,
0) * new Vector3(0, 0, -usedDistance);
myTransform.position = wantedPosition;
myTransform.LookAt( target.position + lookAtAdjustVector );
}
}
{
public Transform myTransform;
public Transform target;
public float distance = 20.0f;
public float height = 5.0f;
public float heightDamping = 2.0f;
public float lookAtHeight = 0.0f;
public float rotationSnapTime = 0.3F;
public float distanceSnapTime;
public Vector3 lookAtAdjustVector;
private float usedDistance;
float wantedRotationAngle;
float wantedHeight;
float currentRotationAngle;
float currentHeight;
Quaternion currentRotation;
Vector3 wantedPosition;
private float yVelocity = 0.0F;
private float zVelocity = 0.0F;
public override void SetTarget( Transform aTarget )
{
target= aTarget;
}
void LateUpdate ()
{
if ( target == null )
return;
if ( myTransform==null )
{
myTransform=Transform;
}
wantedHeight = target.position.y + height; currentHeight = myTransform.position.y;
wantedRotationAngle = target.eulerAngles.y; currentRotationAngle = myTransform.eulerAngles.y;
currentRotationAngle = Mathf.SmoothDampAngle(currentRotation
Angle, wantedRotationAngle, ref yVelocity, rotationSnapTime);
currentHeight = Mathf.Lerp(currentHeight, wantedHeight,
heightDamping * Time.deltaTime);
wantedPosition = target.position; wantedPosition.y = currentHeight;
usedDistance = Mathf.SmoothDampAngle(usedDistance, distance,
ref zVelocity, distanceSnapTime);
wantedPosition += Quaternion.Euler(0, currentRotationAngle,
0) * new Vector3(0, 0, -usedDistance);
myTransform.position = wantedPosition;
myTransform.LookAt( target.position + lookAtAdjustVector );
}
}
→Below is the script in full:
0 Ниже полный скрипт:
→using UnityEngine;
using System.Collections;
public class AutoSpinObject : MonoBehavior
{
public Vector3 spinVector = new Vector3(1,0,0);
private Transform myTransform;
void Start ()
{
myTransform=transform;
}
void Update () {
myTransform.Rotate (spinVector*Time.deltaTime);
}
}
using System.Collections;
public class AutoSpinObject : MonoBehavior
{
public Vector3 spinVector = new Vector3(1,0,0);
private Transform myTransform;
void Start ()
{
myTransform=transform;
}
void Update () {
myTransform.Rotate (spinVector*Time.deltaTime);
}
}
0 using UnityEngine;
using System.Collections;
public class AutoSpinObject : MonoBehavior
{
public Vector3 spinVector = new Vector3(1,0,0);
private Transform myTransform;
void Start ()
{
myTransform=transform;
}
void Update () {
myTransform.Rotate (spinVector*Time.deltaTime);
}
}
using System.Collections;
public class AutoSpinObject : MonoBehavior
{
public Vector3 spinVector = new Vector3(1,0,0);
private Transform myTransform;
void Start ()
{
myTransform=transform;
}
void Update () {
myTransform.Rotate (spinVector*Time.deltaTime);
}
}
→public override void CheckInput ()
{
// get input data from vertical and horizontal axis and
// store them internally in vert and horz so we don't
// have to access them every time we need to relay input
// data out
// calculate a percentage amount to use per pixel float scalerX = 100f / Screen.width; float scalerY = 100f / Screen.height;
{
// get input data from vertical and horizontal axis and
// store them internally in vert and horz so we don't
// have to access them every time we need to relay input
// data out
// calculate a percentage amount to use per pixel float scalerX = 100f / Screen.width; float scalerY = 100f / Screen.height;
0 public override void CheckInput ()
{
// get input data from vertical and horizontal axis and
// store them internally in vert and horz so we don't
// have to access them every time we need to relay input
// data out
// calculate a percentage amount to use per pixel float scalerX = 100f / Screen.width; float scalerY = 100f / Screen.height;
{
// get input data from vertical and horizontal axis and
// store them internally in vert and horz so we don't
// have to access them every time we need to relay input
// data out
// calculate a percentage amount to use per pixel float scalerX = 100f / Screen.width; float scalerY = 100f / Screen.height;
0 public void Start ()
{
prevMousePos= Input.mousePosition;
}
{
prevMousePos= Input.mousePosition;
}
→Below is the script:
0 Ниже скрипт:
→public class Mouse_Input : BaseInputController
{
private Vector2 prevMousePos;
private Vector2 mouseDelta;
private float speedX = 0.05f;
private float speedY = 0.1f;
public void Start ()
{
prevMousePos= Input.mousePosition;
}
public override void CheckInput ()
{
// get input data from vertical and horizontal axis and
// store them internally in vert and horz so we don't
// have to access them every time we need to relay input
// data out
// calculate a percentage amount to use per pixel float scalerX = 100f / Screen.width; float scalerY = 100f / Screen.height;
// calculate and use deltas float mouseDeltaY = Input.mousePosition.y - prevMousePos.y; float mouseDeltaX = Input.mousePosition.x - prevMousePos.x;
// scale based on screen size vert += ( mouseDeltaY * speedY ) * scalerY; horz += ( mouseDeltaX * speedX ) * scalerX;
// store this mouse position for the next time we’re here
prevMousePos= Input.mousePosition;
// set up some Boolean values for up, down, left and right Up = ( vert>0 ); Down = ( vert<0 ); Left = ( horz<0 ); Right = ( horz>0 );
// get fire / action buttons Fire1= Input.GetButton( "Fire1" );
}
public void LateUpdate()
{ // check inputs each LateUpdate() ready for the next tick
CheckInput();
}
}
{
private Vector2 prevMousePos;
private Vector2 mouseDelta;
private float speedX = 0.05f;
private float speedY = 0.1f;
public void Start ()
{
prevMousePos= Input.mousePosition;
}
public override void CheckInput ()
{
// get input data from vertical and horizontal axis and
// store them internally in vert and horz so we don't
// have to access them every time we need to relay input
// data out
// calculate a percentage amount to use per pixel float scalerX = 100f / Screen.width; float scalerY = 100f / Screen.height;
// calculate and use deltas float mouseDeltaY = Input.mousePosition.y - prevMousePos.y; float mouseDeltaX = Input.mousePosition.x - prevMousePos.x;
// scale based on screen size vert += ( mouseDeltaY * speedY ) * scalerY; horz += ( mouseDeltaX * speedX ) * scalerX;
// store this mouse position for the next time we’re here
prevMousePos= Input.mousePosition;
// set up some Boolean values for up, down, left and right Up = ( vert>0 ); Down = ( vert<0 ); Left = ( horz<0 ); Right = ( horz>0 );
// get fire / action buttons Fire1= Input.GetButton( "Fire1" );
}
public void LateUpdate()
{ // check inputs each LateUpdate() ready for the next tick
CheckInput();
}
}
0 public class Mouse_Input : BaseInputController
{
private Vector2 prevMousePos;
private Vector2 mouseDelta;
private float speedX = 0.05f;
private float speedY = 0.1f;
public void Start ()
{
prevMousePos= Input.mousePosition;
}
public override void CheckInput ()
{
// get input data from vertical and horizontal axis and
// store them internally in vert and horz so we don't
// have to access them every time we need to relay input
// data out
// calculate a percentage amount to use per pixel float scalerX = 100f / Screen.width; float scalerY = 100f / Screen.height;
// calculate and use deltas float mouseDeltaY = Input.mousePosition.y - prevMousePos.y; float mouseDeltaX = Input.mousePosition.x - prevMousePos.x;
// scale based on screen size vert += ( mouseDeltaY * speedY ) * scalerY; horz += ( mouseDeltaX * speedX ) * scalerX;
// store this mouse position for the next time we’re here
prevMousePos= Input.mousePosition;
// set up some Boolean values for up, down, left and right Up = ( vert>0 ); Down = ( vert<0 ); Left = ( horz<0 ); Right = ( horz>0 );
// get fire / action buttons Fire1= Input.GetButton( "Fire1" );
}
public void LateUpdate()
{ // check inputs each LateUpdate() ready for the next tick
CheckInput();
}
}
{
private Vector2 prevMousePos;
private Vector2 mouseDelta;
private float speedX = 0.05f;
private float speedY = 0.1f;
public void Start ()
{
prevMousePos= Input.mousePosition;
}
public override void CheckInput ()
{
// get input data from vertical and horizontal axis and
// store them internally in vert and horz so we don't
// have to access them every time we need to relay input
// data out
// calculate a percentage amount to use per pixel float scalerX = 100f / Screen.width; float scalerY = 100f / Screen.height;
// calculate and use deltas float mouseDeltaY = Input.mousePosition.y - prevMousePos.y; float mouseDeltaX = Input.mousePosition.x - prevMousePos.x;
// scale based on screen size vert += ( mouseDeltaY * speedY ) * scalerY; horz += ( mouseDeltaX * speedX ) * scalerX;
// store this mouse position for the next time we’re here
prevMousePos= Input.mousePosition;
// set up some Boolean values for up, down, left and right Up = ( vert>0 ); Down = ( vert<0 ); Left = ( horz<0 ); Right = ( horz>0 );
// get fire / action buttons Fire1= Input.GetButton( "Fire1" );
}
public void LateUpdate()
{ // check inputs each LateUpdate() ready for the next tick
CheckInput();
}
}
→myTransform.position= Vector3.Lerp( myTransform.position, followTarget.
position + targetOffset, moveSpeed * Time.deltaTime );
position + targetOffset, moveSpeed * Time.deltaTime );
0 myTransform.position= Vector3.Lerp( myTransform.position, followTarget.
position + targetOffset, moveSpeed * Time.deltaTime );
position + targetOffset, moveSpeed * Time.deltaTime );
0 public void SetTarget( Transform aTransform )
{
followTarget= aTransform;
}
{
followTarget= aTransform;
}
→void FixedUpdate ()
{ // grab the values we need to calculate grip
myRight=myTransform.right;
// calculate flat velocity
velo=myBody.velocity;
flatVelo.x=velo.x;
flatVelo.y=0;
flatVelo.z=velo.z;
// calculate how much we are sliding
slideSpeed=Vector3.Dot(myRight,flatVelo);
// build a new vector to compensate for the sliding TEMPvec3= myRight * (-slideSpeed * myMass * theGrip);
// apply the correctional force to the rigidbody myBody.AddForce(TEMPvec3 * Time.deltaTime);
}
}
{ // grab the values we need to calculate grip
myRight=myTransform.right;
// calculate flat velocity
velo=myBody.velocity;
flatVelo.x=velo.x;
flatVelo.y=0;
flatVelo.z=velo.z;
// calculate how much we are sliding
slideSpeed=Vector3.Dot(myRight,flatVelo);
// build a new vector to compensate for the sliding TEMPvec3= myRight * (-slideSpeed * myMass * theGrip);
// apply the correctional force to the rigidbody myBody.AddForce(TEMPvec3 * Time.deltaTime);
}
}
0 void FixedUpdate ()
{ // grab the values we need to calculate grip
myRight=myTransform.right;
// calculate flat velocity
velo=myBody.velocity;
flatVelo.x=velo.x;
flatVelo.y=0;
flatVelo.z=velo.z;
// calculate how much we are sliding
slideSpeed=Vector3.Dot(myRight,flatVelo);
// build a new vector to compensate for the sliding TEMPvec3= myRight * (-slideSpeed * myMass * theGrip);
// apply the correctional force to the rigidbody myBody.AddForce(TEMPvec3 * Time.deltaTime);
}
}
{ // grab the values we need to calculate grip
myRight=myTransform.right;
// calculate flat velocity
velo=myBody.velocity;
flatVelo.x=velo.x;
flatVelo.y=0;
flatVelo.z=velo.z;
// calculate how much we are sliding
slideSpeed=Vector3.Dot(myRight,flatVelo);
// build a new vector to compensate for the sliding TEMPvec3= myRight * (-slideSpeed * myMass * theGrip);
// apply the correctional force to the rigidbody myBody.AddForce(TEMPvec3 * Time.deltaTime);
}
}
→void Start ()
{
// cache some references to our rigidbody, mass and
// transform
myBody=rigidbody;
myMass=myBody.mass;
myTransform=transform;
}
{
// cache some references to our rigidbody, mass and
// transform
myBody=rigidbody;
myMass=myBody.mass;
myTransform=transform;
}
0 void Start ()
{
// cache some references to our rigidbody, mass and
// transform
myBody=rigidbody;
myMass=myBody.mass;
myTransform=transform;
}
{
// cache some references to our rigidbody, mass and
// transform
myBody=rigidbody;
myMass=myBody.mass;
myTransform=transform;
}
→public class PretendFriction : MonoBehavior
{
{
0 public class PretendFriction : MonoBehavior
{
{
→using UnityEngine;
using System.Collections;
public class PretendFriction : MonoBehavior
{
private Rigidbody myBody;
private Transform myTransform;
private float myMass;
private float slideSpeed;
private Vector3 velo;
private Vector3 flatVelo;
private Vector3 myRight;
private Vector3 TEMPvec3;
public float theGrip=100f;
void Start ()
{
// cache some references to our rigidbody, mass and
// transform
myBody=rigidbody;
myMass=myBody.mass;
myTransform=transform;
}
void FixedUpdate ()
{ // grab the values we need to calculate grip
myRight=myTransform.right;
// calculate flat velocity
velo=myBody.velocity;
flatVelo.x=velo.x;
flatVelo.y=0;
flatVelo.z=velo.z;
// calculate how much we are sliding
slideSpeed=Vector3.Dot(myRight,flatVelo);
// build a new vector to compensate for the sliding TEMPvec3= myRight * (-slideSpeed * myMass * theGrip);
// apply the correctional force to the rigidbody myBody.AddForce(TEMPvec3 * Time.deltaTime);
}
}
using System.Collections;
public class PretendFriction : MonoBehavior
{
private Rigidbody myBody;
private Transform myTransform;
private float myMass;
private float slideSpeed;
private Vector3 velo;
private Vector3 flatVelo;
private Vector3 myRight;
private Vector3 TEMPvec3;
public float theGrip=100f;
void Start ()
{
// cache some references to our rigidbody, mass and
// transform
myBody=rigidbody;
myMass=myBody.mass;
myTransform=transform;
}
void FixedUpdate ()
{ // grab the values we need to calculate grip
myRight=myTransform.right;
// calculate flat velocity
velo=myBody.velocity;
flatVelo.x=velo.x;
flatVelo.y=0;
flatVelo.z=velo.z;
// calculate how much we are sliding
slideSpeed=Vector3.Dot(myRight,flatVelo);
// build a new vector to compensate for the sliding TEMPvec3= myRight * (-slideSpeed * myMass * theGrip);
// apply the correctional force to the rigidbody myBody.AddForce(TEMPvec3 * Time.deltaTime);
}
}
0 using UnityEngine;
using System.Collections;
public class PretendFriction : MonoBehavior
{
private Rigidbody myBody;
private Transform myTransform;
private float myMass;
private float slideSpeed;
private Vector3 velo;
private Vector3 flatVelo;
private Vector3 myRight;
private Vector3 TEMPvec3;
public float theGrip=100f;
void Start ()
{
// cache some references to our rigidbody, mass and
// transform
myBody=rigidbody;
myMass=myBody.mass;
myTransform=transform;
}
void FixedUpdate ()
{ // grab the values we need to calculate grip
myRight=myTransform.right;
// calculate flat velocity
velo=myBody.velocity;
flatVelo.x=velo.x;
flatVelo.y=0;
flatVelo.z=velo.z;
// calculate how much we are sliding
slideSpeed=Vector3.Dot(myRight,flatVelo);
// build a new vector to compensate for the sliding TEMPvec3= myRight * (-slideSpeed * myMass * theGrip);
// apply the correctional force to the rigidbody myBody.AddForce(TEMPvec3 * Time.deltaTime);
}
}
using System.Collections;
public class PretendFriction : MonoBehavior
{
private Rigidbody myBody;
private Transform myTransform;
private float myMass;
private float slideSpeed;
private Vector3 velo;
private Vector3 flatVelo;
private Vector3 myRight;
private Vector3 TEMPvec3;
public float theGrip=100f;
void Start ()
{
// cache some references to our rigidbody, mass and
// transform
myBody=rigidbody;
myMass=myBody.mass;
myTransform=transform;
}
void FixedUpdate ()
{ // grab the values we need to calculate grip
myRight=myTransform.right;
// calculate flat velocity
velo=myBody.velocity;
flatVelo.x=velo.x;
flatVelo.y=0;
flatVelo.z=velo.z;
// calculate how much we are sliding
slideSpeed=Vector3.Dot(myRight,flatVelo);
// build a new vector to compensate for the sliding TEMPvec3= myRight * (-slideSpeed * myMass * theGrip);
// apply the correctional force to the rigidbody myBody.AddForce(TEMPvec3 * Time.deltaTime);
}
}
→Below is the Path_Spawner.cs script in full:
0 Ниже Path_Spawner в полном виде:
→public class Path_Spawner : MonoBehavior
{
public Waypoints_Controller waypointControl;
// should we start spawning based on distance from the camera? // if distanceBased is false, we will need to call this class from
// elsewhere, to spawn
public bool distanceBasedSpawnStart; // if we're using distance based spawning, at what distance should
// we start?
public float distanceFromCameraToSpawnAt = 35f;
// if the distanceBasedSpawnStart is false, we can have the path
// spawner just start spawning automatically
public bool shouldAutoStartSpawningOnLoad;
public float timeBetweenSpawns=1;
public int totalAmountToSpawn=10;
public bool shouldReversePath;
public GameObject[] spawnObjectPrefabs;
private int totalSpawnObjects;
private Transform myTransform;
private GameObject tempObj;
private int spawnCounter=0;
private int currentObjectNum;
private Transform cameraTransform;
private bool spawning;
public bool shouldSetSpeed;
public float speedToSet;
public bool shouldSetSmoothing;
public float smoothingToSet;
public bool shouldSetRotateSpeed;
public float rotateToSet;
private bool didInit;
void Start ()
{
Init();
}
void Init ()
{ // cache ref to our transform myTransform = transform;
// cache ref to the camera Camera mainCam = Camera.main;
if( mainCam==null )
return;
cameraTransform = mainCam.transform;
// tell waypoint controller if we want to reverse the path
// or not
waypointControl.SetReverseMode(shouldReversePath);
totalSpawnObjects= spawnObjectPrefabs.Length;
if(shouldAutoStartSpawningOnLoad)
StartWave(totalAmountToSpawn,timeBetweenSpawns);
}
public void OnDrawGizmosSelected()
{ Gizmos.color = new Color(0,0,1,0.5f);
Gizmos.DrawCube(transform.position,new Vector3(200,0,distance
FromCameraToSpawnAt));
}
public void Update()
{
float aDist=Mathf.Abs(myTransform.position.z-cameraTransform.
position.z);
if( distanceBasedSpawnStart && !spawning && aDist<distance
FromCameraToSpawnAt)
{
StartWave(totalAmountToSpawn,timeBetweenSpawns);
spawning=true;
}
}
public void StartWave(int HowMany, float timeBetweenSpawns)
{
spawnCounter=0;
totalAmountToSpawn=HowMany;
// reset
currentObjectNum=0;
CancelInvoke("doSpawn");
InvokeRepeating("doSpawn",timeBetweenSpawns,timeBetweenSpawns);
}
void doSpawn()
{
SpawnObject();
}
public void SpawnObject()
{
if(spawnCounter>=totalAmountToSpawn)
{
// tell your script that the wave is finished here
CancelInvoke("doSpawn");
this.enabled=false;
return;
}
// create an object
tempObj=SpawnController.Instance.SpawnGO(spawnObjectPrefabs
[currentObjectNum],myTransform.position,Quaternion.
identity);
// tell object to reverse its pathfinding, if required
tempObj.SendMessage("SetReversePath", shouldReversePath,
SendMessageOptions.DontRequireReceiver);
// tell spawned object to use this waypoint controller
tempObj.SendMessage("SetWayController",waypointControl,Send
MessageOptions.DontRequireReceiver);
// tell object to use this speed (again with no required
// receiver just in case)
if(shouldSetSpeed)
tempObj.SendMessage("SetSpeed",speedToSet,SendMessage
Options.DontRequireReceiver);
// tell object to use this speed (again with no required
// receiver just in case)
if(shouldSetSmoothing)
tempObj.SendMessage("SetPathSmoothingRate",smoothingToSet,
SendMessageOptions.DontRequireReceiver);
// tell object to use this speed (again with no required
// receiver just in case)
if(shouldSetRotateSpeed)
tempObj.SendMessage("SetRotateSpeed",rotateToSet,SendMessage
Options.DontRequireReceiver);
// increase the 'how many objects we have spawned' counter
spawnCounter++;
// increase the 'which object to spawn' counter
currentObjectNum++;
// check to see if we’ve reached the end of the spawn
// objects array if(currentObjectNum> totalSpawnObjects-1 )
currentObjectNum=0;
}
}
{
public Waypoints_Controller waypointControl;
// should we start spawning based on distance from the camera? // if distanceBased is false, we will need to call this class from
// elsewhere, to spawn
public bool distanceBasedSpawnStart; // if we're using distance based spawning, at what distance should
// we start?
public float distanceFromCameraToSpawnAt = 35f;
// if the distanceBasedSpawnStart is false, we can have the path
// spawner just start spawning automatically
public bool shouldAutoStartSpawningOnLoad;
public float timeBetweenSpawns=1;
public int totalAmountToSpawn=10;
public bool shouldReversePath;
public GameObject[] spawnObjectPrefabs;
private int totalSpawnObjects;
private Transform myTransform;
private GameObject tempObj;
private int spawnCounter=0;
private int currentObjectNum;
private Transform cameraTransform;
private bool spawning;
public bool shouldSetSpeed;
public float speedToSet;
public bool shouldSetSmoothing;
public float smoothingToSet;
public bool shouldSetRotateSpeed;
public float rotateToSet;
private bool didInit;
void Start ()
{
Init();
}
void Init ()
{ // cache ref to our transform myTransform = transform;
// cache ref to the camera Camera mainCam = Camera.main;
if( mainCam==null )
return;
cameraTransform = mainCam.transform;
// tell waypoint controller if we want to reverse the path
// or not
waypointControl.SetReverseMode(shouldReversePath);
totalSpawnObjects= spawnObjectPrefabs.Length;
if(shouldAutoStartSpawningOnLoad)
StartWave(totalAmountToSpawn,timeBetweenSpawns);
}
public void OnDrawGizmosSelected()
{ Gizmos.color = new Color(0,0,1,0.5f);
Gizmos.DrawCube(transform.position,new Vector3(200,0,distance
FromCameraToSpawnAt));
}
public void Update()
{
float aDist=Mathf.Abs(myTransform.position.z-cameraTransform.
position.z);
if( distanceBasedSpawnStart && !spawning && aDist<distance
FromCameraToSpawnAt)
{
StartWave(totalAmountToSpawn,timeBetweenSpawns);
spawning=true;
}
}
public void StartWave(int HowMany, float timeBetweenSpawns)
{
spawnCounter=0;
totalAmountToSpawn=HowMany;
// reset
currentObjectNum=0;
CancelInvoke("doSpawn");
InvokeRepeating("doSpawn",timeBetweenSpawns,timeBetweenSpawns);
}
void doSpawn()
{
SpawnObject();
}
public void SpawnObject()
{
if(spawnCounter>=totalAmountToSpawn)
{
// tell your script that the wave is finished here
CancelInvoke("doSpawn");
this.enabled=false;
return;
}
// create an object
tempObj=SpawnController.Instance.SpawnGO(spawnObjectPrefabs
[currentObjectNum],myTransform.position,Quaternion.
identity);
// tell object to reverse its pathfinding, if required
tempObj.SendMessage("SetReversePath", shouldReversePath,
SendMessageOptions.DontRequireReceiver);
// tell spawned object to use this waypoint controller
tempObj.SendMessage("SetWayController",waypointControl,Send
MessageOptions.DontRequireReceiver);
// tell object to use this speed (again with no required
// receiver just in case)
if(shouldSetSpeed)
tempObj.SendMessage("SetSpeed",speedToSet,SendMessage
Options.DontRequireReceiver);
// tell object to use this speed (again with no required
// receiver just in case)
if(shouldSetSmoothing)
tempObj.SendMessage("SetPathSmoothingRate",smoothingToSet,
SendMessageOptions.DontRequireReceiver);
// tell object to use this speed (again with no required
// receiver just in case)
if(shouldSetRotateSpeed)
tempObj.SendMessage("SetRotateSpeed",rotateToSet,SendMessage
Options.DontRequireReceiver);
// increase the 'how many objects we have spawned' counter
spawnCounter++;
// increase the 'which object to spawn' counter
currentObjectNum++;
// check to see if we’ve reached the end of the spawn
// objects array if(currentObjectNum> totalSpawnObjects-1 )
currentObjectNum=0;
}
}
0 public class Path_Spawner : MonoBehavior
{
public Waypoints_Controller waypointControl;
// should we start spawning based on distance from the camera? // if distanceBased is false, we will need to call this class from
// elsewhere, to spawn
public bool distanceBasedSpawnStart; // if we're using distance based spawning, at what distance should
// we start?
public float distanceFromCameraToSpawnAt = 35f;
// if the distanceBasedSpawnStart is false, we can have the path
// spawner just start spawning automatically
public bool shouldAutoStartSpawningOnLoad;
public float timeBetweenSpawns=1;
public int totalAmountToSpawn=10;
public bool shouldReversePath;
public GameObject[] spawnObjectPrefabs;
private int totalSpawnObjects;
private Transform myTransform;
private GameObject tempObj;
private int spawnCounter=0;
private int currentObjectNum;
private Transform cameraTransform;
private bool spawning;
public bool shouldSetSpeed;
public float speedToSet;
public bool shouldSetSmoothing;
public float smoothingToSet;
public bool shouldSetRotateSpeed;
public float rotateToSet;
private bool didInit;
void Start ()
{
Init();
}
void Init ()
{ // cache ref to our transform myTransform = transform;
// cache ref to the camera Camera mainCam = Camera.main;
if( mainCam==null )
return;
cameraTransform = mainCam.transform;
// tell waypoint controller if we want to reverse the path
// or not
waypointControl.SetReverseMode(shouldReversePath);
totalSpawnObjects= spawnObjectPrefabs.Length;
if(shouldAutoStartSpawningOnLoad)
StartWave(totalAmountToSpawn,timeBetweenSpawns);
}
public void OnDrawGizmosSelected()
{ Gizmos.color = new Color(0,0,1,0.5f);
Gizmos.DrawCube(transform.position,new Vector3(200,0,distance
FromCameraToSpawnAt));
}
public void Update()
{
float aDist=Mathf.Abs(myTransform.position.z-cameraTransform.
position.z);
if( distanceBasedSpawnStart && !spawning && aDist<distance
FromCameraToSpawnAt)
{
StartWave(totalAmountToSpawn,timeBetweenSpawns);
spawning=true;
}
}
public void StartWave(int HowMany, float timeBetweenSpawns)
{
spawnCounter=0;
totalAmountToSpawn=HowMany;
// reset
currentObjectNum=0;
CancelInvoke("doSpawn");
InvokeRepeating("doSpawn",timeBetweenSpawns,timeBetweenSpawns);
}
void doSpawn()
{
SpawnObject();
}
public void SpawnObject()
{
if(spawnCounter>=totalAmountToSpawn)
{
// tell your script that the wave is finished here
CancelInvoke("doSpawn");
this.enabled=false;
return;
}
// create an object
tempObj=SpawnController.Instance.SpawnGO(spawnObjectPrefabs
[currentObjectNum],myTransform.position,Quaternion.
identity);
// tell object to reverse its pathfinding, if required
tempObj.SendMessage("SetReversePath", shouldReversePath,
SendMessageOptions.DontRequireReceiver);
// tell spawned object to use this waypoint controller
tempObj.SendMessage("SetWayController",waypointControl,Send
MessageOptions.DontRequireReceiver);
// tell object to use this speed (again with no required
// receiver just in case)
if(shouldSetSpeed)
tempObj.SendMessage("SetSpeed",speedToSet,SendMessage
Options.DontRequireReceiver);
// tell object to use this speed (again with no required
// receiver just in case)
if(shouldSetSmoothing)
tempObj.SendMessage("SetPathSmoothingRate",smoothingToSet,
SendMessageOptions.DontRequireReceiver);
// tell object to use this speed (again with no required
// receiver just in case)
if(shouldSetRotateSpeed)
tempObj.SendMessage("SetRotateSpeed",rotateToSet,SendMessage
Options.DontRequireReceiver);
// increase the 'how many objects we have spawned' counter
spawnCounter++;
// increase the 'which object to spawn' counter
currentObjectNum++;
// check to see if we’ve reached the end of the spawn
// objects array if(currentObjectNum> totalSpawnObjects-1 )
currentObjectNum=0;
}
}
{
public Waypoints_Controller waypointControl;
// should we start spawning based on distance from the camera? // if distanceBased is false, we will need to call this class from
// elsewhere, to spawn
public bool distanceBasedSpawnStart; // if we're using distance based spawning, at what distance should
// we start?
public float distanceFromCameraToSpawnAt = 35f;
// if the distanceBasedSpawnStart is false, we can have the path
// spawner just start spawning automatically
public bool shouldAutoStartSpawningOnLoad;
public float timeBetweenSpawns=1;
public int totalAmountToSpawn=10;
public bool shouldReversePath;
public GameObject[] spawnObjectPrefabs;
private int totalSpawnObjects;
private Transform myTransform;
private GameObject tempObj;
private int spawnCounter=0;
private int currentObjectNum;
private Transform cameraTransform;
private bool spawning;
public bool shouldSetSpeed;
public float speedToSet;
public bool shouldSetSmoothing;
public float smoothingToSet;
public bool shouldSetRotateSpeed;
public float rotateToSet;
private bool didInit;
void Start ()
{
Init();
}
void Init ()
{ // cache ref to our transform myTransform = transform;
// cache ref to the camera Camera mainCam = Camera.main;
if( mainCam==null )
return;
cameraTransform = mainCam.transform;
// tell waypoint controller if we want to reverse the path
// or not
waypointControl.SetReverseMode(shouldReversePath);
totalSpawnObjects= spawnObjectPrefabs.Length;
if(shouldAutoStartSpawningOnLoad)
StartWave(totalAmountToSpawn,timeBetweenSpawns);
}
public void OnDrawGizmosSelected()
{ Gizmos.color = new Color(0,0,1,0.5f);
Gizmos.DrawCube(transform.position,new Vector3(200,0,distance
FromCameraToSpawnAt));
}
public void Update()
{
float aDist=Mathf.Abs(myTransform.position.z-cameraTransform.
position.z);
if( distanceBasedSpawnStart && !spawning && aDist<distance
FromCameraToSpawnAt)
{
StartWave(totalAmountToSpawn,timeBetweenSpawns);
spawning=true;
}
}
public void StartWave(int HowMany, float timeBetweenSpawns)
{
spawnCounter=0;
totalAmountToSpawn=HowMany;
// reset
currentObjectNum=0;
CancelInvoke("doSpawn");
InvokeRepeating("doSpawn",timeBetweenSpawns,timeBetweenSpawns);
}
void doSpawn()
{
SpawnObject();
}
public void SpawnObject()
{
if(spawnCounter>=totalAmountToSpawn)
{
// tell your script that the wave is finished here
CancelInvoke("doSpawn");
this.enabled=false;
return;
}
// create an object
tempObj=SpawnController.Instance.SpawnGO(spawnObjectPrefabs
[currentObjectNum],myTransform.position,Quaternion.
identity);
// tell object to reverse its pathfinding, if required
tempObj.SendMessage("SetReversePath", shouldReversePath,
SendMessageOptions.DontRequireReceiver);
// tell spawned object to use this waypoint controller
tempObj.SendMessage("SetWayController",waypointControl,Send
MessageOptions.DontRequireReceiver);
// tell object to use this speed (again with no required
// receiver just in case)
if(shouldSetSpeed)
tempObj.SendMessage("SetSpeed",speedToSet,SendMessage
Options.DontRequireReceiver);
// tell object to use this speed (again with no required
// receiver just in case)
if(shouldSetSmoothing)
tempObj.SendMessage("SetPathSmoothingRate",smoothingToSet,
SendMessageOptions.DontRequireReceiver);
// tell object to use this speed (again with no required
// receiver just in case)
if(shouldSetRotateSpeed)
tempObj.SendMessage("SetRotateSpeed",rotateToSet,SendMessage
Options.DontRequireReceiver);
// increase the 'how many objects we have spawned' counter
spawnCounter++;
// increase the 'which object to spawn' counter
currentObjectNum++;
// check to see if we’ve reached the end of the spawn
// objects array if(currentObjectNum> totalSpawnObjects-1 )
currentObjectNum=0;
}
}
0 public ArrayList GetAllSpawnedTransforms()
{
return objectList;
}
}
{
return objectList;
}
}
→// here we just provide a convenient function to return the spawned
// objects gameobject rather than its transform public GameObject SpawnGO(GameObject anObject, Vector3 aPosition,
Quaternion aRotation)
{
if(objectList==null)
objectList=new ArrayList();
// instantiate the object
tempGO=(GameObject)Instantiate(anObject, aPosition,
aRotation);
tempTrans= tempGO.transform;
// store this object in our list of objects
objectList.Add(tempTrans);
// return the object to whatever was calling
return tempGO;
}
// objects gameobject rather than its transform public GameObject SpawnGO(GameObject anObject, Vector3 aPosition,
Quaternion aRotation)
{
if(objectList==null)
objectList=new ArrayList();
// instantiate the object
tempGO=(GameObject)Instantiate(anObject, aPosition,
aRotation);
tempTrans= tempGO.transform;
// store this object in our list of objects
objectList.Add(tempTrans);
// return the object to whatever was calling
return tempGO;
}
0 // here we just provide a convenient function to return the spawned
// objects gameobject rather than its transform public GameObject SpawnGO(GameObject anObject, Vector3 aPosition,
Quaternion aRotation)
{
if(objectList==null)
objectList=new ArrayList();
// instantiate the object
tempGO=(GameObject)Instantiate(anObject, aPosition,
aRotation);
tempTrans= tempGO.transform;
// store this object in our list of objects
objectList.Add(tempTrans);
// return the object to whatever was calling
return tempGO;
}
// objects gameobject rather than its transform public GameObject SpawnGO(GameObject anObject, Vector3 aPosition,
Quaternion aRotation)
{
if(objectList==null)
objectList=new ArrayList();
// instantiate the object
tempGO=(GameObject)Instantiate(anObject, aPosition,
aRotation);
tempTrans= tempGO.transform;
// store this object in our list of objects
objectList.Add(tempTrans);
// return the object to whatever was calling
return tempGO;
}
→public Transform Spawn(GameObject anObject, Vector3 aPosition,
Quaternion aRotation)
{
if(objectList==null)
objectList=new ArrayList();
// instantiate the object
tempGO=(GameObject)Instantiate(anObject, aPosition,
aRotation);
tempTrans= tempGO.transform;
// store this object in our list of objects
objectList.Add(tempTrans);
// return the object to whatever was calling
return tempTrans;
}
Quaternion aRotation)
{
if(objectList==null)
objectList=new ArrayList();
// instantiate the object
tempGO=(GameObject)Instantiate(anObject, aPosition,
aRotation);
tempTrans= tempGO.transform;
// store this object in our list of objects
objectList.Add(tempTrans);
// return the object to whatever was calling
return tempTrans;
}
0 public Transform Spawn(GameObject anObject, Vector3 aPosition,
Quaternion aRotation)
{
if(objectList==null)
objectList=new ArrayList();
// instantiate the object
tempGO=(GameObject)Instantiate(anObject, aPosition,
aRotation);
tempTrans= tempGO.transform;
// store this object in our list of objects
objectList.Add(tempTrans);
// return the object to whatever was calling
return tempTrans;
}
Quaternion aRotation)
{
if(objectList==null)
objectList=new ArrayList();
// instantiate the object
tempGO=(GameObject)Instantiate(anObject, aPosition,
aRotation);
tempTrans= tempGO.transform;
// store this object in our list of objects
objectList.Add(tempTrans);
// return the object to whatever was calling
return tempTrans;
}
→public Transform GetPlayerTransform (int indexNum)
{
return (Transform)playerTransforms[indexNum];
}
{
return (Transform)playerTransforms[indexNum];
}
0 public Transform GetPlayerTransform (int indexNum)
{
return (Transform)playerTransforms[indexNum];
}
{
return (Transform)playerTransforms[indexNum];
}
Резюме
- Пол:
- мужчина
- Родной язык:
- русский
- С нами:
-
с 25 октября 2014 г.
(4377 дней)
Не является членом клуба. - Деятельность:
-
259 версий перевода
с общим рейтингом 0
