I currently am creating a script that allows the player character to attack by shooting orbs at foes.
I have the current code to do that (not final code, for testing purposes):
if(Input.GetButton("Fire1"))
{
print("firing");
GameObject newOrb = (GameObject) Instantiate(MagicOrb, this.transform.position, Quaternion.identity);
BasicAttackOrb orbControl = newOrb.GetComponent<BasicAttackOrb>();
if (MoveHandler.facingRight)
orbControl.SetDirection(BasicAttackOrb.RIGHT);
else if (!MoveHandler.facingRight)
orbControl.SetDirection(BasicAttackOrb.LEFT);
}
So I create the new orb projectile, instantiate it, and then attempt to set the direction.
In the orb script:
public class BasicAttackOrb : MonoBehaviour
{
public const int LEFT = -1;
public const int RIGHT = 1;
public float orbSpeed = 15.0f;
public int direction;
// Use this for initialization
void Start ()
{
direction = 0;
}
// Update is called once per frame
void Update ()
{
if (direction == LEFT)
this.transform.Translate(-1.0f * orbSpeed * Time.deltaTime, 0.0f, 0.0f);
else if (direction == RIGHT)
this.transform.Translate(1.0f * orbSpeed * Time.deltaTime, 0.0f, 0.0f);
}
public void SetDirection(int dir)
{
direction = dir;
}
}
So, if the direction is RIGHT (aka 1) it flies right. If it's LEFT (aka -1) it flies left.
However, the orb direction stays at 0, and is never set. This indicates to me that the initial code above in the Input.GetButton("Fire1")
block is incorrect.
Am I accessing the GameObject incorrectly? I feel like it's just a slight coding error with objects but for the life of me I can't find it.
debug.log
to see if your MoveHandler works correctly: since you haveif
andelse if
, if direction isn't modified chances are that something isn't working there \$\endgroup\$