- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试将 Unity Survival Shooter 游戏部署到 Android 设备。游戏运行良好,除了当我射击敌人时,它不会受到任何伤害也不会死亡。这是我的 PlayerShooting 脚本。
using UnityEngine;
using System.Threading;
using UnityStandardAssets.CrossPlatformInput;
namespace CompleteProject
{
public class PlayerShooting : MonoBehaviour
{
public int damagePerShot = 20; // The damage inflicted by each bullet.
public float timeBetweenBullets = 0.15f; // The time between each shot.
public float range = 100f; // The distance the gun can fire.
float timer; // A timer to determine when to fire.
Ray shootRay; // A ray from the gun end forwards.
RaycastHit shootHit; // A raycast hit to get information about what was hit.
int shootableMask; // A layer mask so the raycast only hits things on the shootable layer.
ParticleSystem gunParticles; // Reference to the particle system.
LineRenderer gunLine; // Reference to the line renderer.
AudioSource gunAudio; // Reference to the audio source.
Light gunLight; // Reference to the light component.
public Light faceLight; // Duh
float effectsDisplayTime = 0.2f; // The proportion of the timeBetweenBullets that the effects will display for.
public float timertime = 0.0f;
void Awake ()
{
// Create a layer mask for the Shootable layer.
shootableMask = LayerMask.GetMask ("Shootable");
// Set up the references.
gunParticles = GetComponent<ParticleSystem> ();
gunLine = GetComponent <LineRenderer> ();
gunAudio = GetComponent<AudioSource> ();
gunLight = GetComponent<Light> ();
//faceLight = GetComponentInChildren<Light> ();
}
void Update ()
{
// Add the time since Update was last called to the timer.
timer += Time.deltaTime;
#if !MOBILE_INPUT
// If the Fire1 button is being press and it's time to fire...
if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0)
{
// ... shoot the gun.
Shoot ();
}
#else
// If there is input on the shoot direction stick and it's time to fire.
bool check = CrossPlatformInputManager.GetButtonDown("JumpButton");
check = true;
//Debug.Log(check);
if(check == true)
// if ((CrossPlatformInputManager.GetAxisRaw("Move X") != 0 || CrossPlatformInputManager.GetAxisRaw("Move Y") != 0) && timer >= timeBetweenBullets)
{
// ... shoot the gun
//System.Threading.Thread.Sleep(2000);
timertime -= Time.deltaTime;
if(timertime < 0.0f)
{
Shoot();
timertime = 0.175f;
}
}
#endif
// If the timer has exceeded the proportion of timeBetweenBullets that the effects should be displayed for...
if(timer >= timeBetweenBullets * effectsDisplayTime)
{
// ... disable the effects.
DisableEffects ();
}
}
public void DisableEffects ()
{
// Disable the line renderer and the light.
gunLine.enabled = false;
faceLight.enabled = false;
gunLight.enabled = false;
}
public void Shoot ()
{
// Reset the timer.
Debug.Log("Inside Shoot");
timer = 0f;
// Play the gun shot audioclip.
gunAudio.Play ();
// Enable the lights.
gunLight.enabled = true;
faceLight.enabled = true;
// Stop the particles from playing if they were, then start the particles.
gunParticles.Stop ();
gunParticles.Play ();
// Enable the line renderer and set it's first position to be the end of the gun.
gunLine.enabled = true;
gunLine.SetPosition (0, transform.position);
// Set the shootRay so that it starts at the end of the gun and points forward from the barrel.
shootRay.origin = transform.position;
shootRay.direction = transform.forward;
// Perform the raycast against gameobjects on the shootable layer and if it hits something...
Debug.Log("Going inside Physics Raycast");
if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
{
// Try and find an EnemyHealth script on the gameobject hit.
Debug.Log("Inside Physics Raycast");
EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
Debug.Log (enemyHealth);
Debug.Log ("Checking if enemyHealth is not null");
// If the EnemyHealth component exist...
if(enemyHealth != null)
{
// ... the enemy should take damage.
Debug.Log("Inside enemyHealth != null if condition");
enemyHealth.TakeDamage (damagePerShot, shootHit.point);
}
// Set the second position of the line renderer to the point the raycast hit.
gunLine.SetPosition (1, shootHit.point);
}
// If the raycast didn't hit anything on the shootable layer...
else
{
// ... set the second position of the line renderer to the fullest extent of the gun's range.
gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
}
}
}
}
这是我的 EnemyHealth 脚本。
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
namespace CompleteProject
{
public class EnemyHealth : MonoBehaviour
{
public int startingHealth = 100; // The amount of health the enemy starts the game with.
public int currentHealth; // The current health the enemy has.
public float sinkSpeed = 2.5f; // The speed at which the enemy sinks through the floor when dead.
public int scoreValue = 10; // The amount added to the player's score when the enemy dies.
public AudioClip deathClip; // The sound to play when the enemy dies.
Animator anim; // Reference to the animator.
AudioSource enemyAudio; // Reference to the audio source.
ParticleSystem hitParticles; // Reference to the particle system that plays when the enemy is damaged.
CapsuleCollider capsuleCollider; // Reference to the capsule collider.
bool isDead; // Whether the enemy is dead.
bool isSinking; // Whether the enemy has started sinking through the floor.
void Awake ()
{
// Setting up the references.
anim = GetComponent <Animator> ();
enemyAudio = GetComponent <AudioSource> ();
hitParticles = GetComponentInChildren <ParticleSystem> ();
capsuleCollider = GetComponent <CapsuleCollider> ();
// Setting the current health when the enemy first spawns.
currentHealth = startingHealth;
}
void Update ()
{
// If the enemy should be sinking...
if(isSinking)
{
// ... move the enemy down by the sinkSpeed per second.
transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);
}
}
public void TakeDamage (int amount, Vector3 hitPoint)
{
// If the enemy is dead...
if(isDead)
// ... no need to take damage so exit the function.
return;
// Play the hurt sound effect.
enemyAudio.Play ();
// Reduce the current health by the amount of damage sustained.
currentHealth -= amount;
// Set the position of the particle system to where the hit was sustained.
hitParticles.transform.position = hitPoint;
// And play the particles.
hitParticles.Play();
// If the current health is less than or equal to zero...
if(currentHealth <= 0)
{
// ... the enemy is dead.
Death ();
}
}
void Death ()
{
// The enemy is dead.
isDead = true;
// Turn the collider into a trigger so shots can pass through it.
capsuleCollider.isTrigger = true;
// Tell the animator that the enemy is dead.
anim.SetTrigger ("Dead");
// Change the audio clip of the audio source to the death clip and play it (this will stop the hurt clip playing).
enemyAudio.clip = deathClip;
enemyAudio.Play ();
}
public void StartSinking ()
{
// Find and disable the Nav Mesh Agent.
GetComponent <UnityEngine.AI.NavMeshAgent> ().enabled = false;
// Find the rigidbody component and make it kinematic (since we use Translate to sink the enemy).
GetComponent <Rigidbody> ().isKinematic = true;
// The enemy should no sink.
isSinking = true;
// Increase the score by the enemy's score value.
ScoreManager.score += scoreValue;
// After 2 seconds destory the enemy.
Destroy (gameObject, 2f);
}
}
}
我把日志放在各处,发现 PlayerShooting 脚本中的 enemyHealth 对象总是返回 Null,即使我用子弹击中敌人也是如此。
需要知道为什么它总是返回 Null 以及我如何在击中敌人时返回 Null 以外的值。
最佳答案
您已经在使用 Layer 来确保光线转换仅命中 shootableMask 层上的游戏对象,该层是“Shootable”层,因此消除了层问题。
剩下的唯一可能的问题是 EnemyHealth
脚本未附加到游戏对象或某些游戏对象。
遍历所有游戏对象和预制件,在“可射击”层上选择每一个,然后确保 EnemyHealth
脚本附在他们身上。 GetComponent <EnemyHealth>()
永远不应该是 null
如果EnemyHealth
每一个都附有脚本。
关于c# - Unity Survival Shooter Enemy 不受伤害 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45896484/
这是对 SE 早期帖子的一种跟进:https://stats.stackexchange.com/questions/70858/right-censored-survival-fit-with-ja
运行生存分析,假设变量的 p 值具有统计显着性 - 假设与结果呈正相关。但是,根据 Schoenfeld 残差,违反了比例风险 (PH) 假设。 在纠正 PH 违规后,以下哪种情况可能发生? p 值可
我正在尝试使用生存包中的 tmerge 将几个时间相关协变量添加到数据集以进行生存分析。我的意思是按照主题的 vignette 中的建议按顺序添加每个,但是第一次添加的输出没有按我的预期工作。 更具体
我正在尝试使用 R survival 包估计从状态 1 到状态 2 转换的 Cox 比例风险模型,如下所示: Altman 1)) stop("Penalty terms cannot be in
TL;DR 我认为突变应该被杀死,但它幸存了下来。我正在寻找它发生的原因以及如何解决这两件事:(1)超时(2)突变幸存。 详细信息 我有一个 spring 网络应用程序,正在使用 testng 进行测
我正在尝试实现下面的库以适应方向变化: https://github.com/yigit/android-priority-jobqueue 这是我的配置: Configuration config
我正在尝试将 Unity Survival Shooter 游戏部署到 Android 设备。游戏运行良好,除了当我射击敌人时,它不会受到任何伤害也不会死亡。这是我的 PlayerShooting 脚
我将 azure redis 缓存用于某些性能监控服务。基本上,当页面加载等事件发生时,我会向 redis 发送一个 fire and forget 命令来记录该事件。我的目标是让我的应用程序正常运行
History API使得在浏览器历史记录中存储状态对象成为可能。现在试试 this demo (但它与其他任何行为相同,请选择您最喜欢的 :)): 点击一些链接建立一些历史 清除您的浏览器历史记录(
Angular.js 可以使用哪些方法来使 $rootScope.myVar 变量在页面重新加载后存活,除了将其放入 cookie 或使用 $http 请求定期从服务器检索? 最佳答案 为什么不使用某
下面,我将 R 函数的结果与我自己的代码进行比较。该算法仅包括最大化许多参数(此处为 19)的函数。我的代码定义了函数并使用 nlm 进行优化。幸运的是,两者都返回相同的结果。然而,R 函数非常快。因
我有一个小型 Java 11 示例,其中包含 JUnit 5 测试,结果如下: changed conditional boundary → SURVIVED 主类: public final cla
删除对“com.some.Filename::someMethodName”的调用 --> SURVIVED 在 Pitest 中意味着什么?这是否意味着如果删除该方法调用,代码仍然可以正常工作? 最
删除对“com.some.Filename::someMethodName”的调用 --> SURVIVED 在 Pitest 中意味着什么?这是否意味着如果删除该方法调用,代码仍然可以正常工作? 最
我正在为一项任务搞乱 Unity,我创建了一个“伤害链”,如果我造成伤害的速度足够快,伤害链就会增加。 然后,当伤害链足够高时,我想从背景音乐切换到 Metallica 的“Enter Sandman
我正在尝试使用 cgo 来使用来自 golang 的 C 代码,但在我的 hello-world 小测试中,我遇到了一些我无法理解或找不到更多信息的东西。 我从类似于 examples I've fo
与监视动态变化数据的“仪表板”应用程序中的网格一样,我的(Telerik)Kendo UI 网格每隔 60 秒定期刷新一次新数据: grid.dataSource.data(freshData); g
更新:自从发布这个问题后,突变覆盖失败就消失了,可能是由于坑中的错误修复? 所以这个题现在是无效的。我不知道在这种情况下该怎么办。删除问题?留在这里? 我正在使用 pit 进行突变测试。一个非常简单的
我有这个问题: ContextualVersionConflict: (pandas 0.22.0 (...), Requirement.parse('pandas=0.19'), {'scikit-
我是一名优秀的程序员,十分优秀!