- Home /
Problem is not reproducible or outdated
Cleaner scripting
I have posted this in the forums but I got no response. Hopefully I will have better luck here.
Copy and paste :)
I seem to have hit a brick wall in my game. I am trying to create a simple editor mode that allows the player to select an object from a selection grid, it is set as a cursor. After that if the player presses spacebar the object is then called in from the sky.
The best example of what I want is this video: Cortex Command run-through (YouTube)
However what I have in mind is a mix of what you see in this editor - in the way that at the start of a match in Cortex Command your selected object is your cursor. However instead of it being placed directly as you position it, it is sent from the sky. Also at the moment (at least) I am not interested in allowing the player to call in multiple objects at the same time. So in that sense it is also simpler.
Here is part of my code (the script is named Cursor):
var cursor1 : Transform; //Prefabs var cursor2 : Transform; //Prefabs var cursor3 : Transform; //Prefabs
static var NoCursorIsAlive = true; static var Cursor1Spawn : boolean = false; static var Cursor2Spawn : boolean = false; static var Cursor3Spawn : boolean = false;
function Update () { if (NoCursorIsAlive == true && Spawner.selectedPrefab == 1) { var prefab1 : Transform = cursor1; //Start off with the first prefab var Spawned1 = Instantiate(cursor1, transform.position, transform.rotation); //Spawn the cursor and keep that spawned cursor trackable Spawned1.parent = transform; //Add the cursor as a child NoCursorIsAlive = false; //There is now a cursor alive Cursor1Spawn = true; }
if (Cursor1Spawn == true && Spawner.selectedPrefab == 2) { var prefab2 : Transform = cursor2; //Start off with the first prefab var Spawned2 = Instantiate(cursor2, transform.position, transform.rotation); Spawned2.parent = transform; //Add the cursor as a child Cursor2Spawn = true; Cursor1Spawn = false; }
if (Cursor2Spawn == true && Spawner.selectedPrefab == 3) { var prefab3 : Transform = cursor3; //Start off with the first prefab var Spawned3 = Instantiate(cursor3, transform.position, transform.rotation); Spawned3.parent = transform; //Add the cursor as a child Cursor2Spawn = false; NoCursorIsAlive = true; }}
There is a spawner script is the second part of this editor. It looks for input and spawns the object at the current position of the cursor, but of course I can easily call it in from the sky later (that part isn't important). As well as a special script for each separate prefab that is spawn-able, as you can see I really need to improve the efficiency of my coding skills.
Spawner Script:
var prefab1 : GameObject; var prefab2 : GameObject; var prefab3 : GameObject;
var movespeed : float = 5;
static var selectedPrefab : int = 1; var ThisOneIsForYou = 1;
function Update () {
//On press Z or X add to the selected prefab index if (Input.GetButtonDown("EditorCyclePrefab")) { if (selectedPrefab > 2) { selectedPrefab = 1; ThisOneIsForYou = 1; } else { selectedPrefab++; ThisOneIsForYou++; }}
if (selectedPrefab == 1) {
if (Input.GetButtonDown("Spawn")) {
Instantiate(prefab1, transform.position, transform.rotation);
}}
if (selectedPrefab == 2) {
if (Input.GetButtonDown("Spawn")) {
Instantiate(prefab2, transform.position, transform.rotation);
}}
if (selectedPrefab == 3) {
if (Input.GetButtonDown("Spawn")) {
Instantiate(prefab3, transform.position, transform.rotation);
}}
//Right Move
if (Input.GetAxis("Horizontal") < 0) {
transform.position.x += movespeed * Time.deltaTime;
}
//Left
if (Input.GetAxis("Horizontal") > 0) {
transform.position.x -= movespeed * Time.deltaTime;
}
//Up
if (Input.GetAxis("Vertical") > 0) {
transform.position.y += movespeed * Time.deltaTime;
}
//Down
if (Input.GetAxis("Vertical") < 0) {
transform.position.y -= movespeed * Time.deltaTime;
}
}
Capsule Delete Script:
function Update () {
if (Spawner.selectedPrefab == 2) {
Destroy(gameObject);
}}
Is there a better method for this? Can anyone give me some clues on how I should go about it in a cleaner fashion? Bear in mind I only am displaying these scripts to show you what I am doing and hopefully make it easier for you to recommend a different method.
Thank you for your time.
Follow this Question
Related Questions
Memory management and C# do's and don'ts? 5 Answers
what's the best way to reduce draw calls? 9 Answers
GL drawing optimization 0 Answers
Composite Design Pattern in Unity, Redundant? 1 Answer
Low Priority Methods In LateUpdate? 1 Answer