Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Spaceship C# Script (version: 1.02)
// SPACE UNITY - Space Scene Construction Kit
// http://www.spaceunity.com
// (c) 2013 Stefan Persson

// DESCRIPTION:
// Thruster, steering and weapon control script for Spaceship prefab.

// INSTRUCTIONS:
// This script is attached to the Spaceship demo prefab. Configure parameters to suit your needs.

// PARAMETERS:
//  thrusters			(Thruster array containing reference to thrusters prefabs attached to the ship for propulsion)
//  rollRate			(multiplier for rolling the ship when steering left/right)	
//  yawRate				(multiplier for rudder/steering the ship when steering left/right)
//  pitchRate			(multiplier for pitch when steering up/down)
//  weaponMountPoints	(Vector3 array for mount points relative to ship where weapons will fire from)
//  laserShotPrefab		(reference to Laser Shot prefab, i.e. the laser bullet prefab to be instanitated)
//  soundEffectFire		(sound effect audio clip to be played when firing weapon)

// HINTS:
// The propulsion force of the thruster is configured for each attached thruster in the Thruster script.

// Version History
// 1.02 - Prefixed with SU_Spaceship to avoid naming conflicts.
// 1.01 - Initial Release.

using UnityEngine;
using System.Collections;

public class SU_Spaceship : MonoBehaviour 
{
	
	// Array of thrusters attached to the spaceship
	public SU_Thruster[] thrusters;
	// Specify the roll rate (multiplier for rolling the ship when steering left/right)	
	public float rollRate = 100.0f;
	// Specify the yaw rate (multiplier for rudder/steering the ship when steering left/right)
	public float yawRate = 30.0f;
	// Specify the pitch rate (multiplier for pitch when steering up/down)
	public float pitchRate = 100.0f;
	// Weapon mount points on ship (this is where lasers will be fired from)
	public Vector3[] weaponMountPoints;	
	// Laser shot prefab
	public Transform[] laserShotPrefab;
	// Laser shot sound effect
	public AudioClip[] soundEffectFire;

	public CameraFollow followCamera;
	// Private variables
	private Rigidbody _cacheRigidbody;
	private float shootFreq = 0.25f;
	private float lastShootTime = 0;
	private int weaponType = 0;

	private bool userControl = true;

	void Start () 
	{
		// Ensure that the thrusters in the array have been linked properly
		foreach (SU_Thruster _thruster in thrusters)
		{
			if (_thruster == null) 
			{
				Debug.LogError("Thruster array not properly configured. Attach thrusters to the game object and link them to the Thrusters array.");
			}			
		}
		
		// Cache reference to rigidbody to improve performance
		_cacheRigidbody = GetComponent<Rigidbody>();
		if (_cacheRigidbody == null) 
		{
			Debug.LogError("Spaceship has no rigidbody - the thruster scripts will fail. Add rigidbody component to the spaceship.");
		}
    }
    void Update () 
	{
		if(!userControl)return;

		lastShootTime+=Time.deltaTime;
		if(InputManage.touchInput&&InputManage.isButtonDown
		   ||InputManage.touchInput==false && Input.GetMouseButton(0))
		{
			if(lastShootTime>shootFreq)
			{
				lastShootTime=0;
				foreach (Vector3 _wmp in weaponMountPoints) 
				{
					// Calculate where the position is in world space for the mount point
					Vector3 _pos = transform.position + transform.right * _wmp.x + transform.up * _wmp.y + transform.forward * _wmp.z;
					// Instantiate the laser prefab at position with the spaceships rotation
					// Specify which transform it was that fired this round so we can ignore it for collision/hit
					//Transform _laserShot = (Transform) Instantiate(laserShotPrefab[weaponType], _pos, transform.rotation);
					//_laserShot.GetComponent<SU_LaserShot>().firedBy = transform;
					WeaponFireUtils.FireLaser(laserShotPrefab[weaponType], _pos, this.transform.rotation, this.gameObject, GameManager.ENEMY_TAG);
				}
				// Play sound effect when firing
				if (soundEffectFire[weaponType] != null) 
				{
					GetComponent<AudioSource>().PlayOneShot(soundEffectFire[weaponType]);
				}
			}
		}

		if(InputManage.touchInput == false && Input.GetKeyDown(KeyCode.F3))
		{
			foreach (SU_Thruster _thruster in thrusters) 
			{
				_thruster.SwitchThruster();
			}
		}
		if(InputManage.touchInput == false && Input.GetMouseButtonDown(1))
		{
			foreach(SU_Thruster _thruster in thrusters)
			{
				_thruster.StartBurst();
			}
		}
		if(InputManage.touchInput == false && Input.GetKeyDown(KeyCode.F2))
		{
			weaponType = (weaponType+1)%laserShotPrefab.Length;
			Weapon.weaponType = weaponType;
		}
	}

	void FixedUpdate () 
	{
		if(!userControl)return;
		// In the physics update...
		Vector2 left;
		if (InputManage.touchInput) 
		{
			left = InputManage.leftJoyPosition;
		}
		else 
		{
			//left = new Vector2(Input.GetAxis("Horizontal"),Input.GetAxis("Vertical"));
			Vector2 cen = new Vector2(Screen.width/2,Screen.height/2);
			Vector2 pos = Input.mousePosition;
			left = pos-cen;
			left.x=left.x/Screen.width*2;
			left.y=left.y/Screen.height*2;
		}

		//_cacheRigidbody.AddRelativeTorque(new Vector3(0,0,-left.x*rollRate*_cacheRigidbody.mass));
		//_cacheRigidbody.AddRelativeTorque(new Vector3(0,left.x*yawRate*_cacheRigidbody.mass,0));
		//_cacheRigidbody.AddRelativeTorque(new Vector3(-left.y*pitchRate*_cacheRigidbody.mass,0,0));
		transform.Rotate(-left.y*rollRate,left.x*yawRate,0,Space.Self);
	}
	public void Finish()
	{
		if(followCamera==null)
		{
			followCamera = GameObject.FindObjectOfType<CameraFollow>();
		}
		followCamera.ChangeToThirdPerson();

		userControl = false;
		followCamera.enabled = false;
		foreach (SU_Thruster _thruster in thrusters) 
		{
			_thruster.StartThruster();
		}
	}
}

Commits for Nextrek/3DSpace/Assets/SpaceUnity/_Demo/Scrips/SU_Spaceship.cs

Diff revisions: vs.
Revision Author Commited Message
168 Diff Diff LMancini picture LMancini Wed 08 Apr, 2015 12:33:35 +0000
142 Diff Diff DRuega picture DRuega Fri 14 Nov, 2014 08:43:04 +0000

Mancava un pezzo :P

126 Diff Diff DRuega picture DRuega Thu 06 Nov, 2014 14:45:25 +0000

Cleanup, warnings e altre minchiate :)

112 Diff Diff FMMortaroli picture FMMortaroli Thu 09 Oct, 2014 14:21:59 +0000
107 FMMortaroli picture FMMortaroli Thu 09 Oct, 2014 11:56:46 +0000