简体   繁体   中英

Unity C#, how do I make the sprites move only once?

I want to make the sprites move only once, but the way things are they keep moving infinitely. Here are my scripts below:

The main "battle" script :

public class Battle : MonoBehaviour {

public Sprite player1Sprite;
public Sprite player2Sprite;
public Sprite choicePlayer1;
public Sprite choicePlayer2;
public float currentTime = 0f;
float startingTime = 0f;
string attacker;
public string whoGotHit;
public bool battleExecuted=false;
ChangeSpritePlayer1 player1atk;
ChangeSpritePlayer2 player2atk;
ChangeSpritePlayer1Parry player1par;
ChangeSpritePlayer2Parry player2par;



void Start () {
    player1Sprite = GameObject.Find("Player1").GetComponent<SpriteRenderer>().sprite;
    player2Sprite = GameObject.Find("Player2").GetComponent<SpriteRenderer>().sprite;

    GameObject.Find("BattleManager").GetComponent<OnHitPositionChanges>().enabled = false;

    currentTime = startingTime; //setting the timer to 0
    player1atk = GameObject.Find("Player1").GetComponent<ChangeSpritePlayer1>();
    player2atk = GameObject.Find("Player2").GetComponent<ChangeSpritePlayer2>();
    player1par = GameObject.Find("Player1").GetComponent<ChangeSpritePlayer1Parry>();
    player2par = GameObject.Find("Player2").GetComponent<ChangeSpritePlayer2Parry>();
   // GameObject BattleManager = GameObject.Find("BattleManager");
    // OnHitPositionChanges move = BattleManager.GetComponent<OnHitPositionChanges>();
    GameObject Canvas = GameObject.Find("Canvas");//accessing the Countdown script to find out who attacks first
    Countdown coinflip = Canvas.GetComponent<Countdown>();
    if (coinflip.outcome == "Player 1 Attacks First")
    {
        attacker = "Player1";
    }
    else if (coinflip.outcome=="Player 2 Attacks First")
    {
        attacker = "Player2";
    }
    if (attacker == "Player1")
    {
        GameObject.Find("Player1").GetComponent<ChangeSpritePlayer1Parry>().enabled = false; //player 1 can't parry since he's attacking
        GameObject.Find("Player2").GetComponent<ChangeSpritePlayer2>().enabled = false; //player2 can't attack since he's defending
    }
    else if (attacker == "Player2")
    {
        GameObject.Find("Player1").GetComponent<ChangeSpritePlayer1>().enabled = false; //player1 can't attack since he's defending
        GameObject.Find("Player2").GetComponent<ChangeSpritePlayer2Parry>().enabled = false; //player2 can't defend since he's attacking
    }


}


void Update () {
    Timer(); //this timer goes up 
    GameObject.Find("BattleManager").GetComponent<OnHitPositionChanges>().enabled = true;
    battleOutcome();

    if (battleExecuted)
    {
        Debug.Log("It works");
        currentTime = 0;
        Timer();
        battleExecuted = false;
        GameObject.Find("BattleManager").GetComponent<OnHitPositionChanges>().enabled = false;
        battleOutcome();
    }

}
void Timer()
{
    currentTime += 1 * Time.deltaTime;
}

void battleOutcome()
{


    if (currentTime >= 4.6f && currentTime <= 5.6f)
    {

        choicePlayer1 = GameObject.Find("Player1").GetComponent<SpriteRenderer>().sprite;
        choicePlayer2 = GameObject.Find("Player2").GetComponent<SpriteRenderer>().sprite;


    }
    else if (currentTime > 6f)
    {

        if (attacker == "Player1")
        {
            if (player1atk.tempsprite.name == "Player1StrikeTop" && player2par.tempsprite.name == "Player2ParryTop")
            {
                player1Sprite = player1atk.tempsprite;
                player2Sprite = player2par.tempsprite;
                Debug.Log("Successful Parry (Top/Top)!");
                whoGotHit = "Player1";

etc for all possible outcomes

And the script that handles moving the sprite s:

public class OnHitPositionChanges : MonoBehaviour {
public GameObject Player1;
public GameObject Player2;
string victim;
public float speed = 5;

void Start() {


}


void Update()
{

    Player1 = GameObject.Find("Player1");
    Player2 = GameObject.Find("Player2");
    GameObject BattleManager = GameObject.Find("BattleManager"); //accessing the BattleManager object
    Battle whoGotHit = BattleManager.GetComponent<Battle>(); //finding out which player got hit
    if (whoGotHit.whoGotHit == "Player1")
    {

        victim = "Player1";
        Debug.Log("Player1 got hit");

    }
    else if (whoGotHit.whoGotHit == "Player2")
    {
        victim = "Player2";
        Debug.Log("Player2 got hit");
    }
    else if (whoGotHit.whoGotHit == "Nobody")
    {
        victim = "Nobody";
        Debug.Log("Nobody got hit");
    }
    if (victim == "Player1")
    {
        Player1.transform.position += Vector3.left * 1.0f;
        Player2.transform.position += Vector3.left * 1.0f;
        Debug.Log("Both players move to the left");
    }
    else if (victim == "Player2")
    {
        Player1.transform.position += Vector3.right * 1.0f;
        Player2.transform.position += Vector3.right * 1.0f;
        Debug.Log("Both players move to the right");
    }
    else if (victim == "Nobody")
    {
        Debug.Log("Nobody moves since nobody picked an option");
    }



}
}

I've tried disabling the OnHitPositionChanges script from Battle and then enabling it every time an outcome is reached but it doesn't work.

You're looking for Animation.SetTrigger . From Unity Documentation:

//Attach this script to a GameObject with an Animator component attached.
//For this example, create parameters in the Animator and name them “Crouch” and “Jump”
//Apply these parameters to your transitions between states

//This script allows you to trigger an Animator parameter and reset the other that could possibly still be active. Press the up and down arrow keys to do this.

using UnityEngine;

public class Example : MonoBehaviour
{
    Animator m_Animator;

    void Start()
    {
        //Get the Animator attached to the GameObject you are intending to animate.
        m_Animator = gameObject.GetComponent<Animator>();
    }

    void Update()
    {
        //Press the up arrow button to reset the trigger and set another one
        if (Input.GetKey(KeyCode.UpArrow))
        {
            //Reset the "Crouch" trigger
            m_Animator.ResetTrigger("Crouch");

            //Send the message to the Animator to activate the trigger parameter named "Jump"
            m_Animator.SetTrigger("Jump");
        }

        if (Input.GetKey(KeyCode.DownArrow))
        {
            //Reset the "Jump" trigger
            m_Animator.ResetTrigger("Jump");

            //Send the message to the Animator to activate the trigger parameter named "Crouch"
            m_Animator.SetTrigger("Crouch");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM