我的脚本/游戏/事物使游戏对象向右移动,当我单击舞蹈(我创建的按钮)时,它会停止。然后当计数器(我可能不需要计数器,但我想等待 3 秒)达到 3 时(一旦你点击跳舞计数器开始)我的游戏对象应该继续向右移动。
如果您可以更正代码,那将是很酷的。如果你能纠正它并向我解释我做错了什么,那就更棒了。我刚开始在 Unity 上学习 C#。
using System;
using UnityEngine;
using System.Collections;
public class HeroMouvement : MonoBehaviour
{
public bool trigger = true;
public int counter = 0;
public bool timer = false;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{ //timer becomes true so i can inc the counter
if (timer == true)
{
counter++;
}
if (counter >= 3)
{
MoveHero();//goes to the function moveHero
}
if (trigger == true)
transform.Translate(Vector3.right * Time.deltaTime); //This moves the GameObject to the right
}
//The button you click to dance
void OnGUI()
{
if (GUI.Button(new Rect(10, 10, 50, 50), "Dance"))
{
trigger = false;
timer = true;//now that the timer is set a true once you click it,The uptade should see that its true and start the counter then the counter once it reaches 3 it goes to the MoveHero function
}
}
void MoveHero()
{ //Set the trigger at true so the gameobject can move to the right,the timer is at false and then the counter is reseted at 0.
trigger = true;
timer = false;
counter = 0;
}
}