c# - 出于某种原因,我的手榴弹没有初始化
问题描述
我有2个问题。(1) 出于某种原因,我的手榴弹没有初始化。(2) Transform.LookAt(Player) 不起作用。我正在尝试制作一个会投掷手榴弹的漂浮怪物。我知道 IEnumerator 运行(我放了一个 debug.Log),所以我知道手榴弹问题不是因为它没有运行。我不确定为什么 transform.Lookat 不起作用。谢谢您的帮助!
脚本:(对不起,如果代码不好,我是编程菜鸟)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ZKAttack_lvl3 : MonoBehaviour
{
public Transform Player;
public float MoveSpeed = 3.5f;
public float InRadius = 4.0f;
public float AttackRange = 1.0f;
private Coroutine hasCourutineRunYet;
public GameObject grenade;
public GameObject FloatingMonster;
private Animator anim;
private Rigidbody rigid;
private void Start()
{
anim = GetComponent<Animator>();
Player = GameObject.FindGameObjectsWithTag("Player")[0].transform;
rigid = GetComponent<Rigidbody>();
}
void Update()
{
transform.LookAt(Player);
float dstSqr = (Player.position - transform.position).sqrMagnitude;
bool inRadius = (dstSqr <= InRadius * InRadius);
bool inAttackRange = (dstSqr <= AttackRange * AttackRange);
anim.SetBool("inRadius", inRadius);
anim.SetBool("AttackingPlayer", inAttackRange);
if (inRadius)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
}
rigid.AddForce(1, 10, 1);
if (inAttackRange)
{
if (hasCourutineRunYet == null)
{
hasCourutineRunYet = StartCoroutine(GrenadeAttack());
}
}
}
IEnumerator GrenadeAttack()
{
GameObject bulletObject = Instantiate(grenade);
bulletObject.transform.position = FloatingMonster.transform.position + FloatingMonster.transform.forward;
bulletObject.transform.forward = FloatingMonster.transform.forward;
yield return new WaitForSeconds(2.0f);
}
}
解决方案
我发现它没有初始化的原因是因为线bulletObject.transform.forward = FloatingMonster.transform.forward;
。我认为这以某种方式阻碍了它。
推荐阅读
- python - 如何使 pipenv 安装
使用防火墙的 SSL 证书? - python - 如何在 Python 中读取、操作和写入一系列(少于 8 位)字节数组?
- c++ - 当程序在 WebGL 中使用 C++ Native Addon 和 gl gl.texImage2D 时,电子的渲染器进程崩溃
- rust - Rust 打印选项自定义结构
- r - 设置为零行值情人而不是 rowmean
- angular - Angular Reactive Forms - 带有文本搜索和切换的过滤列表
- python - 如何获得随机森林分类器中“预测”预测的类的“预测概率”?
- utf-8 - Findstr 无法识别第一行的字符串(编码 UTF-8 BOM)
- c# - 如何使用 C# 将字符串合并到 JSON
- eclipse - 如何在 VariablesView 插件 Eclipse 中访问双击/选择更改事件监听器?