Синтаксис:
Используется javascript
//This script handles the player's controls, movment, speed, health, shield. gun/rocket/laser damage and rate of fire. It also handles the HUD that shows on the top.
var Health:float = 100; //The player's hit points
var Shield:float = 100; //The player's shield points
var ShieldRegenerate:float = 0.01; //How fast the shield refills
var MaxSpeed:float = 1; //Maximum Speed of the player
var Acceleration:float = 0.1; //Acceleration of the player, how fast he gets to maximum SpeedX
var Reticle:Transform; //The reticle object, the mouse cursor graphic
var GunShot:Transform; //The object that is shot from a gun
var RocketShot:Transform; //The object that is shot from a rocket
var GunAmmo:int = 100; //How many bullets you have left
var RocketAmmo:int = 20; //How many rockets you have left
var GunShotMuzzle:Transform; //The muzzle effect that is created when shooting from a gun
var RocketShotMuzzle:Transform; //The muzzle effect that is created when shooting from a rocket
var GunShotBarrel:Transform; //The barrel object that ejects when shooting a bullet
var GunShotSpeed:float = 100; //How fast a gun shot moves
var GunShotRate:float = 0.2; //gun shot rate of fire
var GunShotSpread:float = 5; //how inaccurate a gun shot is
var GunShotDamage:float = 1; //How much damage the gun shot causes
var RocketShotRate:float = 0.2; //gun shot rate of fire
var RocketShotDamage:float = 20; //How much damage the gun shot causes
var CurrentWeapon:int = 1; //The currently equipped weapon. There are three: 1-Gun, 2-Rocket
var ShieldEffect:Transform; //The shield effect displayed when a shield is hit by a projectile
var ShieldRange:float = 0.5; //The shield's defense range. How far the shield blocks a projectile from
var BoxMovementLimit:Vector2 = Vector2(6,6); //A limit for the player's movement within a boxed area
var DeadEffect:Transform; //The effect created when the player is destroyed
//Sounds
var GunShotSound:AudioClip;
var GunShotEndSound:AudioClip;
var RocketShotSound:AudioClip;
var ShieldDamageSound:AudioClip;
var HealthDamageSound:AudioClip;
private var TimeSinceLastGunShot:float = 0; //How much time passed since the last shot?
private var TimeSinceLastRocketShot:float = 0; //How much time passed since the last shot?
private var SpeedX:float = 0; //Current SpeedX of the player, left/right speed
private var SpeedY:float = 0; //Current SpeedY of the player, up/down speed
private var Speed:float; //Current overall speed
private var MaxSpeedX:float; //Maximum SpeedX of the player
private var MaxSpeedY:float; //Maximum SpeedY of the player
private var LowerBody:Transform; //The player's lower body, the legs
private var UpperBody:Transform; //The player's upper body, the torso and the weapons
private var RightGun:Transform; //The Upper body's right gun
private var RightGunTip:Transform; //The Upper body's right gun tip
private var Rocket:Transform; //The Upper body's rocket launcher
private var RotateTarget:Vector3; //The player's lower body target rotation. The lower body will always rotate in this direction
private var GunTipRotation:float = 0; //How fast the gun tip rotates when you shoot
private var ObjectCopy:Object; //A general object copy used when we want to give instantiated (created) transforms some attributes
private var MuzzleCopy:Object; //A copy of the muzzle object
private var MaxHealth:float; //the player's maximum health
private var MaxShield:float; //the player's maximum shield
private var HighScore:int = 0; //Current high score
private var DisplayedScore:int = 0; //Current highscore displayed in the HUD at the top of the screen
private var AreWePaused:boolean = false; //Are we paused? Press R or P to pause the game.
function Start()
{
MaxSpeedX = MaxSpeedY = MaxSpeed; //Set the maximum speed
MaxHealth = Health;
MaxShield = Shield;
//Set the different functional parts of the mech, so we can access them quickly later
LowerBody = transform.Find("LowerBody"); //Find the player's lower body and put it in a variable, so we can quickly access it later
UpperBody = transform.Find("UpperBody"); //Find the player's upper body and put it in a variable, so we can quickly access it later
RightGun = transform.Find("UpperBody/RightGun"); //Find the upper body's left gun
RightGunTip = transform.Find("UpperBody/RightGunTip"); //Find the upper body's left gun tip
Rocket = transform.Find("UpperBody/Rocket"); //Find the upper body's rocket
HUDPosX = Screen.width; //The x position of the HUD
HUDPosY = 0; //The y position of the HUD
}
function Update ()
{
//PAUSE AND UNPAUSE THE GAME
//Press R or P to pause the game and access the upgrade shop
if ( Input.GetKeyDown("r") || Input.GetKeyDown("p") )
{
if ( AreWePaused == false )
{
AreWePaused = true; //We are now in the upgrade shop
Time.timeScale = 0; //Set the game's speed to 0. Pause
}
else
{
AreWePaused = false; //We are now out of the upgrade shop
Time.timeScale = 1; //Set the game's speed to 1. UnPause
}
}
//If we are not paused, then you can play and control the player
if ( AreWePaused == false )
{
//MOVING LEFT AND RIGHT
//If you press "d", start SpeedXing to the right
if( Input.GetKey("d") )
{
if ( SpeedX < MaxSpeedX ) //If you haven't reached the maximum SpeedX, keep accelerating
{
SpeedX += Acceleration; //Increase current SpeedX
}
else
{
SpeedX = MaxSpeedX; //If you've reached the maximum speed, stay at it
}
//Set rotation target for the player to be Right
RotateTarget = Vector3(270,180,0);
}
else if( Input.GetKey("a") ) //If you press "a", start SpeedXing to the left. This is the opposite of going to the right, so all values are negative
{
if ( SpeedX > -MaxSpeedX ) //If you haven't reached the maximum SpeedX, keep accelerating
{
SpeedX -= Acceleration; //Increase current SpeedX
}
else
{
SpeedX = -MaxSpeedX; //If you've reached the maximum speed, stay at it
}
//Set rotation target for the player to be Left
RotateTarget = Vector3(270,0,0);
}
else //If you're not pressing "a" or "d", slow down
{
SpeedX *= 0.9; //decrease SpeedX
}
//Move the player in to the left/right based on his SpeedX,
transform.Translate(Vector3.right * SpeedX * Time.deltaTime, Space.World);
if ( transform.position.x > BoxMovementLimit.x ) transform.position.x = BoxMovementLimit.x;
if ( transform.position.x < -BoxMovementLimit.x ) transform.position.x = -BoxMovementLimit.x;
//MOVING UP AND DOWN
//If you press "w", start moving up
if( Input.GetKey("w") )
{
if ( SpeedY < MaxSpeedY ) //If you haven't reached the maximum SpeedY, keep accelerating
{
SpeedY += Acceleration; //Increase current SpeedY
}
else
{
SpeedY = MaxSpeedY; //If you've reached the maximum speed, stay at it
}
//Set rotation target for the player to be Up
RotateTarget = Vector3(270,90,0);
}
else if( Input.GetKey("s") ) //If you press "s", start moving down. This is the opposite of going up, so all values are negative
{
if ( SpeedY > -MaxSpeedY ) //If you haven't reached the maximum SpeedY, keep accelerating
{
SpeedY -= Acceleration; //Increase current SpeedY
}
else
{
SpeedY = -MaxSpeedY; //If you've reached the maximum speed, stay at it
}
//Set rotation target for the player to be Down
RotateTarget = Vector3(270,270,0);
}
else //If you're not pressing "w" or "s", slow down
{
SpeedY *= 0.9; //decrease SpeedY
}
//Move the player in to the left/right based on his SpeedY
transform.Translate(Vector3.forward * SpeedY * Time.deltaTime, Space.World);
if ( transform.position.z > BoxMovementLimit.y ) transform.position.z = BoxMovementLimit.y;
if ( transform.position.z < -BoxMovementLimit.y) transform.position.z = -BoxMovementLimit.y;
//SET THE ROTATION OF THE PLAYER FOR DIAGONAL ROTATIONS
//Checking special diagonal cases, so the rotation is correct
if( Input.GetKey("w") && Input.GetKey("a") && Input.GetKey("d") )
{
//Set rotation target for the player to be Upper Right
RotateTarget = Vector3(270,135,0);
}
else if( Input.GetKey("w") && Input.GetKey("a") )
{
//Set rotation target for the player to be Upper Left
RotateTarget = Vector3(270,45,0);
}
else if( Input.GetKey("w") && Input.GetKey("d") )
{
//Set rotation target for the player to be Upper Right
RotateTarget = Vector3(270,135,0);
}
else if( Input.GetKey("s") && Input.GetKey("d") )
{
//Set rotation target for the player to be Lower Left
RotateTarget = Vector3(270,225,0);
}
else if( Input.GetKey("s") && Input.GetKey("a") )
{
//Set rotation target for the player to be Lower Right
RotateTarget = Vector3(270,315,0);
}
//Rotate the player in the direction of RotateTarget
transform.eulerAngles.y -= (LowerBody.eulerAngles.y - RotateTarget.y) * 0.1;
//ANIMATE THE PLAYER'S LOWER BODY
//Animate the player's lower body based on his speed
LowerBody.animation.Play("run");
//Set the animation speed of the lower body based on the player's speed. If he's moving left/right set it based on SpeedX, otherwise set it based on SpeedY
if ( Mathf.Abs(SpeedX) > Mathf.Abs(SpeedY) )
{
LowerBody.animation["run"].speed = SpeedX * 2; //Animate the player's lower body
Speed = SpeedX;
//Add the walking speed to the DistanceWalked in the statstics object
GameObject.Find("Statistics").GetComponent("Statistics").CurrentDistanceWalked += Mathf.Abs(SpeedX);
}
else
{
LowerBody.animation["run"].speed = SpeedY * 2; //Animate the player's lower body
Speed = SpeedY;
//Add the walking speed to the DistanceWalked in the statstics object
GameObject.Find("Statistics").GetComponent("Statistics").CurrentDistanceWalked += Mathf.Abs(SpeedY);
}
//AIMING WITH THE MOUSE
// Generate a plane that intersects the transform's position with an upwards normal.
var playerPlane = new Plane(Vector3.up, transform.position + Vector3(0,0,0));
// Generate a ray from the cursor position
var RayCast = Camera.main.ScreenPointToRay (Input.mousePosition);
// Determine the point where the cursor ray intersects the plane.
var HitDist:float = 0;
// If the ray is parallel to the plane, Raycast will return false.
if ( playerPlane.Raycast (RayCast, HitDist))
{
// Get the point along the ray that hits the calculated distance.
var TargetPoint = RayCast.GetPoint(HitDist);
// Determine the target rotation. This is the rotation if the transform looks at the target point.
var targetRotation = Quaternion.LookRotation(TargetPoint - transform.position);
Reticle.position = TargetPoint; //Set the position of the reticle to be the same as the position of the mouse on the created plane
UpperBody.LookAt(Reticle.position + Vector3(0,0.75,0)); //Make the upper body look in the direction of the reticle
UpperBody.eulerAngles.x = 0; //Limit the upper body's rotation so it doesn't look straight down or up
}
//SHOOT GUNS
TimeSinceLastGunShot += Time.deltaTime; //calculate time since the last gunshot
TimeSinceLastRocketShot += Time.deltaTime; //calculate time since the last rocket shot
if ( CurrentWeapon == 1) //If you have the first weapon equipped (Gun), shoot it
{
//If you hold down the mouse button, keep shooting at a constant rate
if( Input.GetKey("mouse 0") )
{
if ( GunAmmo > 0 )
{
//If the time that passed since the last shot is bigger than the allowed rate of fire, shoot!
if ( TimeSinceLastGunShot > GunShotRate )
{
TimeSinceLastGunShot = 0; //reset time since last shot
if ( GunShot ) //Shooting the gun
{
//RIGHT GUN
ObjectCopy = Instantiate(GunShot, RightGun.position, RightGun.rotation); //Create a bullet object at the position of the right gun tip
ObjectCopy.Translate(RightGun.forward * Random.Range(-0.01,0.03), Space.Self); //Move the gun shot's starting position a little to the left or right within a random range
ObjectCopy.Translate(UpperBody.forward * 0.3, Space.World);
ObjectCopy.Rotate(RightGun.right * Random.Range(-GunShotSpread,GunShotSpread), Space.Self); //Move the gun shot's starting position a little to the left or right within a random range
ObjectCopy.rigidbody.AddForce(ObjectCopy.right * GunShotSpeed); //Add force to the bullet so it flies in the direction we're shooting
ObjectCopy.GetComponent("GunShot").Damage = GunShotDamage;
}
if ( GunShotMuzzle ) //Muzzle effect
{
//RIGHT GUN
ObjectCopy = Instantiate(GunShotMuzzle, RightGun.position, RightGun.rotation); //Create a muzzle effect for the gun shot
ObjectCopy.Translate(RightGun.right * -0.2 , Space.World);
ObjectCopy.localScale = Vector3(Random.Range(0.5,1),Random.Range(0.3,0.6),Random.Range(0.3,0.6)); //Give the muzzle effect a little random scale for a nice variation
Destroy(ObjectCopy.gameObject,0.1); //destroy the muzzle effect after a fraction of a second
}
if ( GunShotBarrel )
{
//RIGHT GUN
ObjectCopy = Instantiate(GunShotBarrel, RightGun.position, RightGun.rotation); //Create a bullet object at the position of the right gun tip
ObjectCopy.Translate(RightGun.forward * Random.Range(-0.01,0.03), Space.Self); //Move the gun shot's starting position a little to the left or right within a random range
ObjectCopy.rigidbody.AddForce(ObjectCopy.up * -30); //Add force to the bullet so it flies in the direction we're shooting
ObjectCopy.rigidbody.AddForce(ObjectCopy.right * -80); //Add force to the bullet so it flies in the direction we're shooting
Destroy(ObjectCopy.gameObject,10); //destroy the muzzle effect after a fraction of a second
}
//reduce 2 from ammo
GunAmmo -= 2;
//add to the gun shot statistic
GameObject.Find("Statistics").GetComponent("Statistics").CurrentGunShots += 2;
//Play gun shot sound
audio.PlayOneShot(GunShotSound);
}
GunTipRotation = 1400; //Set the gun tip rotation speed
}
}
if( Input.GetKeyUp("mouse 0") && TimeSinceLastGunShot < 1 )
{
audio.PlayOneShot(GunShotEndSound);
}
}
else if ( CurrentWeapon == 2 ) //If you have the second weapon equipped (Rocket), shoot it
{
if ( RocketAmmo > 0 )
{
//If you hold down the mouse button, keep shooting at a constant rate
if( Input.GetKey("mouse 0") )
{
//If the time that passed since the last shot is bigger than the allowed rate of fire, shoot!
if ( TimeSinceLastRocketShot > RocketShotRate )
{
TimeSinceLastRocketShot = 0; //reset time since last shot
if ( RocketShot ) //Shooting the rocket
{
//LEFT GUN
ObjectCopy = Instantiate(RocketShot, Rocket.position, Rocket.rotation); //Create a bullet object at the position of the left gun tip
ObjectCopy.GetComponent("RocketShot").Damage = RocketShotDamage;
if ( Random.value > 0.5 ) ObjectCopy.Translate(Rocket.right * -0.05 , Space.World);
else ObjectCopy.Translate(Rocket.right * 0.05 , Space.World);
if ( Random.value > 0.5 ) ObjectCopy.Translate(Rocket.forward * 0.05 , Space.World);
ObjectCopy.rigidbody.AddForce(ObjectCopy.up * -150); //Add force to the bullet so it flies in the direction we're shooting
}
if ( RocketShotMuzzle ) //Muzzle effect
{
//LEFT GUN
MuzzleCopy = Instantiate(RocketShotMuzzle, ObjectCopy.position, UpperBody.rotation); //Create a muzzle effect for the gun shot
MuzzleCopy.Translate(MuzzleCopy.transform.forward * 0.1, Space.World);
}
RocketAmmo--;
//add to te rocket shot statistic
GameObject.Find("Statistics").GetComponent("Statistics").CurrentRocketShots += 1;
//Play gun shot sound
audio.PlayOneShot(RocketShotSound);
}
}
}
}
}
//Rotate the gun tip, and slow down its rotation gradually until it stops
if ( GunTipRotation > 0 )
{
RightGunTip.Rotate(Vector3.right, GunTipRotation * Time.deltaTime, Space.Self); //rotate the right gun tip
GunTipRotation *= 0.95; //slow down the gun tip's rotation value
}
//CHANGE CURRENT WEAPON WITH MOUSE WHEEL
if ( Input.GetAxis("Mouse ScrollWheel") )
{
if ( CurrentWeapon < 2 )
{
CurrentWeapon++; //Go to next weapon
}
else
{
CurrentWeapon = 1; //Go back to first weapon
}
//print(CurrentWeapon);
}
//CHANGE CURRENT WEAPON WITH 1,2,3 KEYS
if ( Input.GetKeyDown("1") && CurrentWeapon != 1 ) CurrentWeapon = 1; //Go to weapon 1
if ( Input.GetKeyDown("2") && CurrentWeapon != 2 ) CurrentWeapon = 2; //Go to weapon 2
//REGENERATE SHIELD
if ( Shield < MaxShield )
{
Shield += ShieldRegenerate;
}
else if ( Shield != MaxShield )
{
Shield = MaxShield;
}
Mathf.Clamp(Shield, 0, MaxShield);
//Make the upperbosy burn when its health is low
if ( Health <= MaxHealth * 0.5 ) gameObject.Find("UpperBody/Burn1").particleEmitter.emit = true;
else gameObject.Find("UpperBody/Burn1").particleEmitter.emit = false;
if ( Health <= MaxHealth * 0.4 ) gameObject.Find("UpperBody/Burn2").particleEmitter.emit = true;
else gameObject.Find("UpperBody/Burn2").particleEmitter.emit = false;
if ( Health <= MaxHealth * 0.3 ) gameObject.Find("UpperBody/Burn3").particleEmitter.emit = true;
else gameObject.Find("UpperBody/Burn3").particleEmitter.emit = false;
if ( Health <= MaxHealth * 0.2 ) gameObject.Find("UpperBody/Burn4").particleEmitter.emit = true;
else gameObject.Find("UpperBody/Burn4").particleEmitter.emit = false;
if ( Health <= MaxHealth * 0.1 ) gameObject.Find("UpperBody/Burn5").particleEmitter.emit = true;
else gameObject.Find("UpperBody/Burn5").particleEmitter.emit = false;
//DIE
if ( Health <= 0 )
{
Instantiate(DeadEffect, transform.position, transform.rotation);
//Seperate all the upperbody parts and throw them in all directions
RightGun.gameObject.AddComponent(Rigidbody);
RightGun.rigidbody.AddForceAtPosition(Vector3.one * -50, transform.position);
RightGun.gameObject.AddComponent(BoxCollider);
RightGun.parent = null;
RightGunTip.gameObject.AddComponent(Rigidbody);
RightGunTip.rigidbody.AddForceAtPosition(Vector3.one * -50, transform.position);
RightGunTip.gameObject.AddComponent(BoxCollider);
RightGunTip.parent = null;
Rocket.gameObject.AddComponent(Rigidbody);
Rocket.rigidbody.AddForceAtPosition(Vector3.one * -50, transform.position);
Rocket.gameObject.AddComponent(BoxCollider);
Rocket.parent = null;
//Add the time played to the statistics object
GameObject.Find("Statistics").GetComponent("Statistics").CurrentTimePlayed = Time.timeSinceLevelLoad;
Destroy(gameObject);
}
}
//This script displays the HUD, with current weapon, ammo left, Health, Shield, and score
var GUIskin:GUISkin; //The skin gui we'll use
var HUDTexture:Texture2D; //The base of the HUD texture
var HUDHealthTexture:Texture2D; //The health bar
var HUDShieldTexture:Texture2D; //The shield bar
var HUDGunTexture:Texture2D; //the gun weapon icon
var HUDRocketTexture:Texture2D; //the rocket weapon icon
//The position of the HUD
private var HUDPosX:float = 0;
private var HUDPosY:float = 0;
function OnGUI()
{
GUI.skin = GUIskin; //The skin gui we'll use
HUDPosX -= (HUDPosX - (Screen.width - HUDTexture.width)) * 0.1; //Move the HUD from offscreen to its position on the top of the screen
GUI.DrawTexture(Rect(HUDPosX,HUDPosY,256,256), HUDTexture); //Draw the HUD texture
if ( Health > 0 && Health <= MaxHealth ) GUI.Box ( Rect(HUDPosX + 17,HUDPosY + 11,152 * (Health/MaxHealth),32 ), "", "Health"); //Draw the health bar only if it's within the limits of MaxHealth
if ( Shield > 0 && Shield <= MaxShield ) GUI.Box ( Rect(HUDPosX + 17,HUDPosY + 64,152 * (Shield/MaxShield),32 ), "", "Shield"); //Draw the shiled bar only if it's within the limits of MaxShiled
if ( CurrentWeapon == 1 ) //If the gun is equipped
{
GUI.DrawTexture(Rect(HUDPosX + 147,HUDPosY - 21,128,128), HUDGunTexture); //Draw the gun icon
//AMMO
GUI.Box ( Rect(HUDPosX + 182,HUDPosY + 88,64,32 ), GunAmmo.ToString(), "Ammo"); //Draw the ammo text
}
else if ( CurrentWeapon == 2 ) //If the rocket is equipped
{
GUI.DrawTexture(Rect(HUDPosX + 147,HUDPosY - 21,128,128), HUDRocketTexture); //Draw the rocket icon
//AMMO
GUI.Box ( Rect(HUDPosX + 182,HUDPosY + 88,64,32 ), RocketAmmo.ToString(), "Ammo"); //Draw the ammo text
}
else if ( CurrentWeapon == 3 ) //If the laser is equipped
{
//AMMO
GUI.Box ( Rect(HUDPosX + 182,HUDPosY + 88,64,32 ), "-", "Ammo"); //Draw the ammo text
}
//Animate the score increasing until it reaches the value of HighScore
if ( DisplayedScore < HighScore )
{
DisplayedScore += 10;
}
else if ( DisplayedScore > HighScore )
{
DisplayedScore -= 100;
}
GUI.Box ( Rect(5,5,192,64 ), DisplayedScore.ToString()); //Show the display score
}
var Health:float = 100; //The player's hit points
var Shield:float = 100; //The player's shield points
var ShieldRegenerate:float = 0.01; //How fast the shield refills
var MaxSpeed:float = 1; //Maximum Speed of the player
var Acceleration:float = 0.1; //Acceleration of the player, how fast he gets to maximum SpeedX
var Reticle:Transform; //The reticle object, the mouse cursor graphic
var GunShot:Transform; //The object that is shot from a gun
var RocketShot:Transform; //The object that is shot from a rocket
var GunAmmo:int = 100; //How many bullets you have left
var RocketAmmo:int = 20; //How many rockets you have left
var GunShotMuzzle:Transform; //The muzzle effect that is created when shooting from a gun
var RocketShotMuzzle:Transform; //The muzzle effect that is created when shooting from a rocket
var GunShotBarrel:Transform; //The barrel object that ejects when shooting a bullet
var GunShotSpeed:float = 100; //How fast a gun shot moves
var GunShotRate:float = 0.2; //gun shot rate of fire
var GunShotSpread:float = 5; //how inaccurate a gun shot is
var GunShotDamage:float = 1; //How much damage the gun shot causes
var RocketShotRate:float = 0.2; //gun shot rate of fire
var RocketShotDamage:float = 20; //How much damage the gun shot causes
var CurrentWeapon:int = 1; //The currently equipped weapon. There are three: 1-Gun, 2-Rocket
var ShieldEffect:Transform; //The shield effect displayed when a shield is hit by a projectile
var ShieldRange:float = 0.5; //The shield's defense range. How far the shield blocks a projectile from
var BoxMovementLimit:Vector2 = Vector2(6,6); //A limit for the player's movement within a boxed area
var DeadEffect:Transform; //The effect created when the player is destroyed
//Sounds
var GunShotSound:AudioClip;
var GunShotEndSound:AudioClip;
var RocketShotSound:AudioClip;
var ShieldDamageSound:AudioClip;
var HealthDamageSound:AudioClip;
private var TimeSinceLastGunShot:float = 0; //How much time passed since the last shot?
private var TimeSinceLastRocketShot:float = 0; //How much time passed since the last shot?
private var SpeedX:float = 0; //Current SpeedX of the player, left/right speed
private var SpeedY:float = 0; //Current SpeedY of the player, up/down speed
private var Speed:float; //Current overall speed
private var MaxSpeedX:float; //Maximum SpeedX of the player
private var MaxSpeedY:float; //Maximum SpeedY of the player
private var LowerBody:Transform; //The player's lower body, the legs
private var UpperBody:Transform; //The player's upper body, the torso and the weapons
private var RightGun:Transform; //The Upper body's right gun
private var RightGunTip:Transform; //The Upper body's right gun tip
private var Rocket:Transform; //The Upper body's rocket launcher
private var RotateTarget:Vector3; //The player's lower body target rotation. The lower body will always rotate in this direction
private var GunTipRotation:float = 0; //How fast the gun tip rotates when you shoot
private var ObjectCopy:Object; //A general object copy used when we want to give instantiated (created) transforms some attributes
private var MuzzleCopy:Object; //A copy of the muzzle object
private var MaxHealth:float; //the player's maximum health
private var MaxShield:float; //the player's maximum shield
private var HighScore:int = 0; //Current high score
private var DisplayedScore:int = 0; //Current highscore displayed in the HUD at the top of the screen
private var AreWePaused:boolean = false; //Are we paused? Press R or P to pause the game.
function Start()
{
MaxSpeedX = MaxSpeedY = MaxSpeed; //Set the maximum speed
MaxHealth = Health;
MaxShield = Shield;
//Set the different functional parts of the mech, so we can access them quickly later
LowerBody = transform.Find("LowerBody"); //Find the player's lower body and put it in a variable, so we can quickly access it later
UpperBody = transform.Find("UpperBody"); //Find the player's upper body and put it in a variable, so we can quickly access it later
RightGun = transform.Find("UpperBody/RightGun"); //Find the upper body's left gun
RightGunTip = transform.Find("UpperBody/RightGunTip"); //Find the upper body's left gun tip
Rocket = transform.Find("UpperBody/Rocket"); //Find the upper body's rocket
HUDPosX = Screen.width; //The x position of the HUD
HUDPosY = 0; //The y position of the HUD
}
function Update ()
{
//PAUSE AND UNPAUSE THE GAME
//Press R or P to pause the game and access the upgrade shop
if ( Input.GetKeyDown("r") || Input.GetKeyDown("p") )
{
if ( AreWePaused == false )
{
AreWePaused = true; //We are now in the upgrade shop
Time.timeScale = 0; //Set the game's speed to 0. Pause
}
else
{
AreWePaused = false; //We are now out of the upgrade shop
Time.timeScale = 1; //Set the game's speed to 1. UnPause
}
}
//If we are not paused, then you can play and control the player
if ( AreWePaused == false )
{
//MOVING LEFT AND RIGHT
//If you press "d", start SpeedXing to the right
if( Input.GetKey("d") )
{
if ( SpeedX < MaxSpeedX ) //If you haven't reached the maximum SpeedX, keep accelerating
{
SpeedX += Acceleration; //Increase current SpeedX
}
else
{
SpeedX = MaxSpeedX; //If you've reached the maximum speed, stay at it
}
//Set rotation target for the player to be Right
RotateTarget = Vector3(270,180,0);
}
else if( Input.GetKey("a") ) //If you press "a", start SpeedXing to the left. This is the opposite of going to the right, so all values are negative
{
if ( SpeedX > -MaxSpeedX ) //If you haven't reached the maximum SpeedX, keep accelerating
{
SpeedX -= Acceleration; //Increase current SpeedX
}
else
{
SpeedX = -MaxSpeedX; //If you've reached the maximum speed, stay at it
}
//Set rotation target for the player to be Left
RotateTarget = Vector3(270,0,0);
}
else //If you're not pressing "a" or "d", slow down
{
SpeedX *= 0.9; //decrease SpeedX
}
//Move the player in to the left/right based on his SpeedX,
transform.Translate(Vector3.right * SpeedX * Time.deltaTime, Space.World);
if ( transform.position.x > BoxMovementLimit.x ) transform.position.x = BoxMovementLimit.x;
if ( transform.position.x < -BoxMovementLimit.x ) transform.position.x = -BoxMovementLimit.x;
//MOVING UP AND DOWN
//If you press "w", start moving up
if( Input.GetKey("w") )
{
if ( SpeedY < MaxSpeedY ) //If you haven't reached the maximum SpeedY, keep accelerating
{
SpeedY += Acceleration; //Increase current SpeedY
}
else
{
SpeedY = MaxSpeedY; //If you've reached the maximum speed, stay at it
}
//Set rotation target for the player to be Up
RotateTarget = Vector3(270,90,0);
}
else if( Input.GetKey("s") ) //If you press "s", start moving down. This is the opposite of going up, so all values are negative
{
if ( SpeedY > -MaxSpeedY ) //If you haven't reached the maximum SpeedY, keep accelerating
{
SpeedY -= Acceleration; //Increase current SpeedY
}
else
{
SpeedY = -MaxSpeedY; //If you've reached the maximum speed, stay at it
}
//Set rotation target for the player to be Down
RotateTarget = Vector3(270,270,0);
}
else //If you're not pressing "w" or "s", slow down
{
SpeedY *= 0.9; //decrease SpeedY
}
//Move the player in to the left/right based on his SpeedY
transform.Translate(Vector3.forward * SpeedY * Time.deltaTime, Space.World);
if ( transform.position.z > BoxMovementLimit.y ) transform.position.z = BoxMovementLimit.y;
if ( transform.position.z < -BoxMovementLimit.y) transform.position.z = -BoxMovementLimit.y;
//SET THE ROTATION OF THE PLAYER FOR DIAGONAL ROTATIONS
//Checking special diagonal cases, so the rotation is correct
if( Input.GetKey("w") && Input.GetKey("a") && Input.GetKey("d") )
{
//Set rotation target for the player to be Upper Right
RotateTarget = Vector3(270,135,0);
}
else if( Input.GetKey("w") && Input.GetKey("a") )
{
//Set rotation target for the player to be Upper Left
RotateTarget = Vector3(270,45,0);
}
else if( Input.GetKey("w") && Input.GetKey("d") )
{
//Set rotation target for the player to be Upper Right
RotateTarget = Vector3(270,135,0);
}
else if( Input.GetKey("s") && Input.GetKey("d") )
{
//Set rotation target for the player to be Lower Left
RotateTarget = Vector3(270,225,0);
}
else if( Input.GetKey("s") && Input.GetKey("a") )
{
//Set rotation target for the player to be Lower Right
RotateTarget = Vector3(270,315,0);
}
//Rotate the player in the direction of RotateTarget
transform.eulerAngles.y -= (LowerBody.eulerAngles.y - RotateTarget.y) * 0.1;
//ANIMATE THE PLAYER'S LOWER BODY
//Animate the player's lower body based on his speed
LowerBody.animation.Play("run");
//Set the animation speed of the lower body based on the player's speed. If he's moving left/right set it based on SpeedX, otherwise set it based on SpeedY
if ( Mathf.Abs(SpeedX) > Mathf.Abs(SpeedY) )
{
LowerBody.animation["run"].speed = SpeedX * 2; //Animate the player's lower body
Speed = SpeedX;
//Add the walking speed to the DistanceWalked in the statstics object
GameObject.Find("Statistics").GetComponent("Statistics").CurrentDistanceWalked += Mathf.Abs(SpeedX);
}
else
{
LowerBody.animation["run"].speed = SpeedY * 2; //Animate the player's lower body
Speed = SpeedY;
//Add the walking speed to the DistanceWalked in the statstics object
GameObject.Find("Statistics").GetComponent("Statistics").CurrentDistanceWalked += Mathf.Abs(SpeedY);
}
//AIMING WITH THE MOUSE
// Generate a plane that intersects the transform's position with an upwards normal.
var playerPlane = new Plane(Vector3.up, transform.position + Vector3(0,0,0));
// Generate a ray from the cursor position
var RayCast = Camera.main.ScreenPointToRay (Input.mousePosition);
// Determine the point where the cursor ray intersects the plane.
var HitDist:float = 0;
// If the ray is parallel to the plane, Raycast will return false.
if ( playerPlane.Raycast (RayCast, HitDist))
{
// Get the point along the ray that hits the calculated distance.
var TargetPoint = RayCast.GetPoint(HitDist);
// Determine the target rotation. This is the rotation if the transform looks at the target point.
var targetRotation = Quaternion.LookRotation(TargetPoint - transform.position);
Reticle.position = TargetPoint; //Set the position of the reticle to be the same as the position of the mouse on the created plane
UpperBody.LookAt(Reticle.position + Vector3(0,0.75,0)); //Make the upper body look in the direction of the reticle
UpperBody.eulerAngles.x = 0; //Limit the upper body's rotation so it doesn't look straight down or up
}
//SHOOT GUNS
TimeSinceLastGunShot += Time.deltaTime; //calculate time since the last gunshot
TimeSinceLastRocketShot += Time.deltaTime; //calculate time since the last rocket shot
if ( CurrentWeapon == 1) //If you have the first weapon equipped (Gun), shoot it
{
//If you hold down the mouse button, keep shooting at a constant rate
if( Input.GetKey("mouse 0") )
{
if ( GunAmmo > 0 )
{
//If the time that passed since the last shot is bigger than the allowed rate of fire, shoot!
if ( TimeSinceLastGunShot > GunShotRate )
{
TimeSinceLastGunShot = 0; //reset time since last shot
if ( GunShot ) //Shooting the gun
{
//RIGHT GUN
ObjectCopy = Instantiate(GunShot, RightGun.position, RightGun.rotation); //Create a bullet object at the position of the right gun tip
ObjectCopy.Translate(RightGun.forward * Random.Range(-0.01,0.03), Space.Self); //Move the gun shot's starting position a little to the left or right within a random range
ObjectCopy.Translate(UpperBody.forward * 0.3, Space.World);
ObjectCopy.Rotate(RightGun.right * Random.Range(-GunShotSpread,GunShotSpread), Space.Self); //Move the gun shot's starting position a little to the left or right within a random range
ObjectCopy.rigidbody.AddForce(ObjectCopy.right * GunShotSpeed); //Add force to the bullet so it flies in the direction we're shooting
ObjectCopy.GetComponent("GunShot").Damage = GunShotDamage;
}
if ( GunShotMuzzle ) //Muzzle effect
{
//RIGHT GUN
ObjectCopy = Instantiate(GunShotMuzzle, RightGun.position, RightGun.rotation); //Create a muzzle effect for the gun shot
ObjectCopy.Translate(RightGun.right * -0.2 , Space.World);
ObjectCopy.localScale = Vector3(Random.Range(0.5,1),Random.Range(0.3,0.6),Random.Range(0.3,0.6)); //Give the muzzle effect a little random scale for a nice variation
Destroy(ObjectCopy.gameObject,0.1); //destroy the muzzle effect after a fraction of a second
}
if ( GunShotBarrel )
{
//RIGHT GUN
ObjectCopy = Instantiate(GunShotBarrel, RightGun.position, RightGun.rotation); //Create a bullet object at the position of the right gun tip
ObjectCopy.Translate(RightGun.forward * Random.Range(-0.01,0.03), Space.Self); //Move the gun shot's starting position a little to the left or right within a random range
ObjectCopy.rigidbody.AddForce(ObjectCopy.up * -30); //Add force to the bullet so it flies in the direction we're shooting
ObjectCopy.rigidbody.AddForce(ObjectCopy.right * -80); //Add force to the bullet so it flies in the direction we're shooting
Destroy(ObjectCopy.gameObject,10); //destroy the muzzle effect after a fraction of a second
}
//reduce 2 from ammo
GunAmmo -= 2;
//add to the gun shot statistic
GameObject.Find("Statistics").GetComponent("Statistics").CurrentGunShots += 2;
//Play gun shot sound
audio.PlayOneShot(GunShotSound);
}
GunTipRotation = 1400; //Set the gun tip rotation speed
}
}
if( Input.GetKeyUp("mouse 0") && TimeSinceLastGunShot < 1 )
{
audio.PlayOneShot(GunShotEndSound);
}
}
else if ( CurrentWeapon == 2 ) //If you have the second weapon equipped (Rocket), shoot it
{
if ( RocketAmmo > 0 )
{
//If you hold down the mouse button, keep shooting at a constant rate
if( Input.GetKey("mouse 0") )
{
//If the time that passed since the last shot is bigger than the allowed rate of fire, shoot!
if ( TimeSinceLastRocketShot > RocketShotRate )
{
TimeSinceLastRocketShot = 0; //reset time since last shot
if ( RocketShot ) //Shooting the rocket
{
//LEFT GUN
ObjectCopy = Instantiate(RocketShot, Rocket.position, Rocket.rotation); //Create a bullet object at the position of the left gun tip
ObjectCopy.GetComponent("RocketShot").Damage = RocketShotDamage;
if ( Random.value > 0.5 ) ObjectCopy.Translate(Rocket.right * -0.05 , Space.World);
else ObjectCopy.Translate(Rocket.right * 0.05 , Space.World);
if ( Random.value > 0.5 ) ObjectCopy.Translate(Rocket.forward * 0.05 , Space.World);
ObjectCopy.rigidbody.AddForce(ObjectCopy.up * -150); //Add force to the bullet so it flies in the direction we're shooting
}
if ( RocketShotMuzzle ) //Muzzle effect
{
//LEFT GUN
MuzzleCopy = Instantiate(RocketShotMuzzle, ObjectCopy.position, UpperBody.rotation); //Create a muzzle effect for the gun shot
MuzzleCopy.Translate(MuzzleCopy.transform.forward * 0.1, Space.World);
}
RocketAmmo--;
//add to te rocket shot statistic
GameObject.Find("Statistics").GetComponent("Statistics").CurrentRocketShots += 1;
//Play gun shot sound
audio.PlayOneShot(RocketShotSound);
}
}
}
}
}
//Rotate the gun tip, and slow down its rotation gradually until it stops
if ( GunTipRotation > 0 )
{
RightGunTip.Rotate(Vector3.right, GunTipRotation * Time.deltaTime, Space.Self); //rotate the right gun tip
GunTipRotation *= 0.95; //slow down the gun tip's rotation value
}
//CHANGE CURRENT WEAPON WITH MOUSE WHEEL
if ( Input.GetAxis("Mouse ScrollWheel") )
{
if ( CurrentWeapon < 2 )
{
CurrentWeapon++; //Go to next weapon
}
else
{
CurrentWeapon = 1; //Go back to first weapon
}
//print(CurrentWeapon);
}
//CHANGE CURRENT WEAPON WITH 1,2,3 KEYS
if ( Input.GetKeyDown("1") && CurrentWeapon != 1 ) CurrentWeapon = 1; //Go to weapon 1
if ( Input.GetKeyDown("2") && CurrentWeapon != 2 ) CurrentWeapon = 2; //Go to weapon 2
//REGENERATE SHIELD
if ( Shield < MaxShield )
{
Shield += ShieldRegenerate;
}
else if ( Shield != MaxShield )
{
Shield = MaxShield;
}
Mathf.Clamp(Shield, 0, MaxShield);
//Make the upperbosy burn when its health is low
if ( Health <= MaxHealth * 0.5 ) gameObject.Find("UpperBody/Burn1").particleEmitter.emit = true;
else gameObject.Find("UpperBody/Burn1").particleEmitter.emit = false;
if ( Health <= MaxHealth * 0.4 ) gameObject.Find("UpperBody/Burn2").particleEmitter.emit = true;
else gameObject.Find("UpperBody/Burn2").particleEmitter.emit = false;
if ( Health <= MaxHealth * 0.3 ) gameObject.Find("UpperBody/Burn3").particleEmitter.emit = true;
else gameObject.Find("UpperBody/Burn3").particleEmitter.emit = false;
if ( Health <= MaxHealth * 0.2 ) gameObject.Find("UpperBody/Burn4").particleEmitter.emit = true;
else gameObject.Find("UpperBody/Burn4").particleEmitter.emit = false;
if ( Health <= MaxHealth * 0.1 ) gameObject.Find("UpperBody/Burn5").particleEmitter.emit = true;
else gameObject.Find("UpperBody/Burn5").particleEmitter.emit = false;
//DIE
if ( Health <= 0 )
{
Instantiate(DeadEffect, transform.position, transform.rotation);
//Seperate all the upperbody parts and throw them in all directions
RightGun.gameObject.AddComponent(Rigidbody);
RightGun.rigidbody.AddForceAtPosition(Vector3.one * -50, transform.position);
RightGun.gameObject.AddComponent(BoxCollider);
RightGun.parent = null;
RightGunTip.gameObject.AddComponent(Rigidbody);
RightGunTip.rigidbody.AddForceAtPosition(Vector3.one * -50, transform.position);
RightGunTip.gameObject.AddComponent(BoxCollider);
RightGunTip.parent = null;
Rocket.gameObject.AddComponent(Rigidbody);
Rocket.rigidbody.AddForceAtPosition(Vector3.one * -50, transform.position);
Rocket.gameObject.AddComponent(BoxCollider);
Rocket.parent = null;
//Add the time played to the statistics object
GameObject.Find("Statistics").GetComponent("Statistics").CurrentTimePlayed = Time.timeSinceLevelLoad;
Destroy(gameObject);
}
}
//This script displays the HUD, with current weapon, ammo left, Health, Shield, and score
var GUIskin:GUISkin; //The skin gui we'll use
var HUDTexture:Texture2D; //The base of the HUD texture
var HUDHealthTexture:Texture2D; //The health bar
var HUDShieldTexture:Texture2D; //The shield bar
var HUDGunTexture:Texture2D; //the gun weapon icon
var HUDRocketTexture:Texture2D; //the rocket weapon icon
//The position of the HUD
private var HUDPosX:float = 0;
private var HUDPosY:float = 0;
function OnGUI()
{
GUI.skin = GUIskin; //The skin gui we'll use
HUDPosX -= (HUDPosX - (Screen.width - HUDTexture.width)) * 0.1; //Move the HUD from offscreen to its position on the top of the screen
GUI.DrawTexture(Rect(HUDPosX,HUDPosY,256,256), HUDTexture); //Draw the HUD texture
if ( Health > 0 && Health <= MaxHealth ) GUI.Box ( Rect(HUDPosX + 17,HUDPosY + 11,152 * (Health/MaxHealth),32 ), "", "Health"); //Draw the health bar only if it's within the limits of MaxHealth
if ( Shield > 0 && Shield <= MaxShield ) GUI.Box ( Rect(HUDPosX + 17,HUDPosY + 64,152 * (Shield/MaxShield),32 ), "", "Shield"); //Draw the shiled bar only if it's within the limits of MaxShiled
if ( CurrentWeapon == 1 ) //If the gun is equipped
{
GUI.DrawTexture(Rect(HUDPosX + 147,HUDPosY - 21,128,128), HUDGunTexture); //Draw the gun icon
//AMMO
GUI.Box ( Rect(HUDPosX + 182,HUDPosY + 88,64,32 ), GunAmmo.ToString(), "Ammo"); //Draw the ammo text
}
else if ( CurrentWeapon == 2 ) //If the rocket is equipped
{
GUI.DrawTexture(Rect(HUDPosX + 147,HUDPosY - 21,128,128), HUDRocketTexture); //Draw the rocket icon
//AMMO
GUI.Box ( Rect(HUDPosX + 182,HUDPosY + 88,64,32 ), RocketAmmo.ToString(), "Ammo"); //Draw the ammo text
}
else if ( CurrentWeapon == 3 ) //If the laser is equipped
{
//AMMO
GUI.Box ( Rect(HUDPosX + 182,HUDPosY + 88,64,32 ), "-", "Ammo"); //Draw the ammo text
}
//Animate the score increasing until it reaches the value of HighScore
if ( DisplayedScore < HighScore )
{
DisplayedScore += 10;
}
else if ( DisplayedScore > HighScore )
{
DisplayedScore -= 100;
}
GUI.Box ( Rect(5,5,192,64 ), DisplayedScore.ToString()); //Show the display score
}
И вот второй:
Синтаксис:
Используется csharp
using UnityEngine;
using System.Collections;
public class Constructor : MonoBehaviour {
public GameObject player_example;
public Transform drednoughtBody;
private GameObject drednought;
private GameObject skelet;
private Transform player_torso;
void Start () {
player_example = Instantiate(player_example, drednought.transform.position, drednought.transform.rotation) as GameObject;
player_torso = Instantiate(drednoughtBody.Find("player_torso_flamer"), player_example.transform.position, player_example.transform.rotation) as Transform;
player_example.name = "base";
player_torso.name = "player_torso_flamer";
player_torso.parent = player_example.transform;
player_example.transform.parent = GameObject.Find("drednought").transform;
}
void Update () {
if (Input.GetKeyDown ("1"))"Change head"
{
if (player_torso.name == "player_torso_flamer")
{
Destroy(GameObject.Find(drednought.name + "/base/player_torso_flamer"));
player_torso = Instantiate(drednoughtBody.Find("player_torso_minigun"), player_torso.position, player_example.transform.rotation) as Transform;
player_torso.name = "player_torso_minigun";
}
else
{
Destroy(GameObject.Find(drednought.name + "/base/player_torso_minigun"));
player_torso = Instantiate(drednoughtBody.Find("player_torso_flamer"), player_torso.position, player_example.transform.rotation) as Transform;
player_torso.name = "player_torso_flamer";
}
player_torso.parent = player_example.transform;
}
}
}
using System.Collections;
public class Constructor : MonoBehaviour {
public GameObject player_example;
public Transform drednoughtBody;
private GameObject drednought;
private GameObject skelet;
private Transform player_torso;
void Start () {
player_example = Instantiate(player_example, drednought.transform.position, drednought.transform.rotation) as GameObject;
player_torso = Instantiate(drednoughtBody.Find("player_torso_flamer"), player_example.transform.position, player_example.transform.rotation) as Transform;
player_example.name = "base";
player_torso.name = "player_torso_flamer";
player_torso.parent = player_example.transform;
player_example.transform.parent = GameObject.Find("drednought").transform;
}
void Update () {
if (Input.GetKeyDown ("1"))"Change head"
{
if (player_torso.name == "player_torso_flamer")
{
Destroy(GameObject.Find(drednought.name + "/base/player_torso_flamer"));
player_torso = Instantiate(drednoughtBody.Find("player_torso_minigun"), player_torso.position, player_example.transform.rotation) as Transform;
player_torso.name = "player_torso_minigun";
}
else
{
Destroy(GameObject.Find(drednought.name + "/base/player_torso_minigun"));
player_torso = Instantiate(drednoughtBody.Find("player_torso_flamer"), player_torso.position, player_example.transform.rotation) as Transform;
player_torso.name = "player_torso_flamer";
}
player_torso.parent = player_example.transform;
}
}
}
По отдельности скрипты работают замечательно. А вместе... По сути второй скрипт стоял на камере, и создавал новый объект,которым я мог управлять( всмысле менять верхнюю часть). Первый скрипт собственно управление игроком. Подскажите мне как их объединить, или же как использовать не объединяя, просто в Js знаний мало, поэтому всунуть не получилось как бы. Как видите подмена функции по нажатию клавиши у меня есть, как бы мне сделать еще и подмену меша на js я не знаю. Можете просто хотя бы дать конвертер, из c# в js. правда я тут пробовал один, js в c#, ошибок море