【发布时间】:2018-10-11 11:46:31
【问题描述】:
大家好,我的射箭脚本有点问题。我的意思是我想在播放动画的最后一帧时射箭。我尝试通过设置一个 firePoint GameObject 来做到这一点,通过在我的动画选项卡中录制所需的帧来放置它。它当然被禁用,但在动画播放时启用,然后再次禁用。所以问题是: - 当我点击与我的射击输入匹配的按钮时,动画播放, - 我的实例化出现并产生多个箭头, - 当它被禁用时,它会停止产生箭头。
我只想制作一支箭。有人可以帮忙吗?
CombatScript.cs:
/* private bool shootBow;
* public bool needReload = false;
* public float reloadTime = 1.5f;
* public float realoadCD;
*/
public void RangeAttack()
{
if (needReload == false && gameObject.GetComponent<PlayerControls>().grounded == true && Input.GetButtonDown("Ranged"))
{
animator.SetTrigger("shootBow");
attack1 = false; // Melle attacks sets to false in case of lag or smth.
attack2 = false;
attack3 = false;
needReload = true;
if (needReload == true)
{
reloadCD = reloadTime;
}
}
if (reloadCD > 0 && needReload == true)
{
reloadCD -= Time.deltaTime;
}
if (reloadCD <= 0)
{
reloadCD = 0;
needReload = false;
}
if (firePoint.gameObject.activeSelf == true)
{
Instantiate(Missile, new Vector3(firePoint.position.x + 1, firePoint.position.y), firePoint.rotation);
Debug.Log("It's a bird, a plane, no.. it's arrow.");
}
}
箭头控制器.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ArrowController : MonoBehaviour {
public float speed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
GetComponent<Rigidbody2D>().velocity = new Vector2(speed, GetComponent<Rigidbody2D>().velocity.y);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Enemy")
{
Destroy(collision.gameObject);
}
Debug.Log("Arrow Broke");
Debug.Log(gameObject.name);
//Destroy(gameObject);
}
public void OnCollisionEnter2D(Collision2D collision)
{
}
}
我的情况示例:
真/假needReload语句示例:
在右边的 Inspector 你有我的玩家信息,在左边(或底部)Inspector 你有 Missile(Arrow)inspector
【问题讨论】:
-
你在哪里调用 RangeAttack() ?
-
是的,如果它是鼠标事件或输入,您需要将其更改为
OnMouseButtonDown或OnKeyDown' if you have it inOnMouseButton` 或OnKeyPress它会重复调用它,但使用向下或向上事件会每次点击只调用一次 -
@Z3RP - 我在 FixedUpdate() 中调用它
-
所以你的问题是你想在动画的最后一帧射一个箭头吗?
-
是的,我想让它完全像那样工作。