I am trying to achieve the same functionality as Invoker’s orbs in Dota 2. My player character has 3 balls hovering over him ( 0 to begin with).
Each time “R”,“G” or “B” is pressed, a ball of the corresponding color (red,green or blue) is created in predefined positions. Lets call them pos1, pos2 and pos3.
After the third input, wherein a ball is created at pos3, if the user provides yet another input, I want the corresponding ball to appear at pos1 , the 5th input at pos2 and so on.
Complete beginner here, I want to know how i can destroy the ball at pos1 created by the first input before creating the ball at the same position from the 4th input. This is what my code currently looks like. I would also be really thankful if someone could tell me a better/simpler approach to the problem
using UnityEngine;
using System.Collections;
using System;
public class orbControl : MonoBehaviour {
int currentOrb = 0;
int maxOrbs = 2;
public Transform[] spawnPos;
public GameObject airOrb;
public GameObject fireOrb;
public GameObject waterOrb;
void Update()
{
if (Input.GetKeyDown ("q") && currentOrb <= maxOrbs)
{
spawnAirOrb ();
currentOrb++;
if (currentOrb > maxOrbs)
{
currentOrb = (currentOrb % maxOrbs) -1;
}
}
if (Input.GetKeyDown ("w") && currentOrb <= maxOrbs)
{
spawnFireOrb ();
currentOrb++;
if (currentOrb > maxOrbs)
{
currentOrb = (currentOrb % maxOrbs) -1;
}
}
if (Input.GetKeyDown ("e") && currentOrb <= maxOrbs)
{
spawnWaterOrb ();
currentOrb++;
if (currentOrb > 2)
{
currentOrb = (currentOrb % maxOrbs) -1;
}
}
}
void spawnAirOrb( )
{
GameObject spawnedOrb = Instantiate (airOrb, spawnPos [currentOrb ].position, spawnPos [currentOrb ].rotation) as GameObject;
}
void spawnFireOrb( )
{
GameObject spawnedOrb = Instantiate (fireOrb, spawnPos [currentOrb ].position, spawnPos [currentOrb ].rotation) as GameObject;
}
void spawnWaterOrb( )
{
GameObject spawnedOrb = Instantiate (waterOrb, spawnPos [currentOrb ].position, spawnPos [currentOrb ].rotation) as GameObject;
}
}
TBruce
2
The following tested script will destroy the orb before a new one is created and place the new one in the proper location. I added an extra bool reference variable to allow for the placing of the orb that is being orbited . it will place the orb in the exact location as the old one.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public class OrbControl : MonoBehaviour
{
int currentOrb = 0;
int maxOrbs = 3;
public Transform[] spawnPos;
public GameObject airOrb;
public GameObject fireOrb;
public GameObject waterOrb;
public bool allowOrbit = false; // if set to true then orbiting functionality needs to be added
public bool overRideCurrentOrb = false; // if set to true then "currentOrb" will not be used
void Start()
{
if (maxOrbs < spawnPos.Length)
{
maxOrbs = spawnPos.Length;
}
}
void Update()
{
if (Input.GetKeyDown ("q") && currentOrb < maxOrbs)
{
spawnAirOrb ();
}
if (Input.GetKeyDown ("w") && currentOrb < maxOrbs)
{
spawnFireOrb ();
}
if (Input.GetKeyDown ("e") && currentOrb < maxOrbs)
{
spawnWaterOrb ();
}
}
void spawnAirOrb()
{
spawnOrb(airOrb, 0);
}
void spawnFireOrb()
{
spawnOrb(fireOrb, 1);
}
void spawnWaterOrb()
{
spawnOrb(waterOrb, 2);
}
void spawnOrb(GameObject orb, int index)
{
if (!overRideCurrentOrb)
{
index = currentOrb;
}
if (orb != null)
{
GameObject oldOrb = GameObject.Find(orb.name + "(Clone)");
Vector3 position = spawnPos [index].position;
Quaternion rotation = spawnPos [index].rotation;
if (oldOrb != null)
{
if (allowOrbit)
{
// if orbiting is allowed then set the position and rotation values to the current object
position = oldOrb.transform.position;
rotation = oldOrb.transform.rotation;
}
Destroy(oldOrb);
}
GameObject spawnedOrb = (GameObject)Instantiate (orb, position, rotation);
currentOrb++;
if (currentOrb >= maxOrbs)
{
currentOrb = 0;
}
}
}
}
Note: There was a possible issue with logic where an individual orb could be created many times without creating others. The effect would create the new orb in a location of a different color sometimes overlapping another orb. I do not know if this was intended or not, but just in case I added the reference variable overRideCurrentOrb (defaults to false) which would not use the currentOrb value to get the position and rotation.