簡體   English   中英

Unity C#,如何使子畫面僅移​​動一次?

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

我只想讓精靈移動一次,但是事情的方式卻是無限移動。 以下是我的腳本:

主要的“戰斗”腳本

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";

對於所有可能的結果等

以及處理移動精靈腳本

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");
    }



}
}

我嘗試從Battle中禁用OnHitPositionChanges腳本,然后在每次達到結果時都啟用它,但它不起作用。

您正在尋找Animation.SetTrigger 從Unity文檔中:

//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");
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM