Unity C#,如何使子画面仅移​​动一次?

时间:2019-02-20 12:45:48

标签: c# unity3d

我想让子画面仅移​​动一次,但是事情的方式是它们无限移动。这是我的以下脚本:

主要“战斗”脚本

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脚本,然后在每次达到结果但不起作用时都启用它。

1 个答案:

答案 0 :(得分:0)

您正在寻找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");
        }
    }
}