如何在每次杀死敌人时提高速度



我正努力做到每次杀死敌人时,我的玩家速度都会增加1。我一直在努力做到这一点,但我真的不知道自己在做什么。有人能帮我吗?

这是我的玩家动作脚本

using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float MovementSpeed = 5;
public float JumpForce = 5;
private Rigidbody2D _rigidbody;
private void Start()
{
_rigidbody = GetComponent<Rigidbody2D>();
}
private void Update()
{
//Movement
var movement = Input.GetAxis("Horizontal");
transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * MovementSpeed;
if (Input.GetButtonDown("Jump") && Mathf.Abs(_rigidbody.velocity.y) < 0.001f)
{
_rigidbody.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
}
}
}

这是我的敌人脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enemyScript : MonoBehaviour
{
public int health = 100;
private static float speed;
private static float jump;
public void TakeDamage(int damage)
{
health -= damage;
if (health <= 0)
{
Die();
speed += 1.0f;
jump += 1.0f;
}
}
void Die()
{
Destroy(gameObject);
speed = GetComponent<PlayerMovement>().MovementSpeed;
jump = GetComponent<PlayerMovement>().JumpForce;
}
}

抱歉,我的问题没有全部细节,玩家没有获得任何速度。我试过使用

GetComponent<PlayerMovement>().MovementSpeed += 1.0f;
GetComponent<PlayerMovement>().JumpForce += 1.0f;

现在我收到这个错误信息

NullReferenceException:对象引用未设置为对象的实例

很抱歉给带来不便

首先,使用GetComponent毫无意义,因为PlayerMovement没有附加到敌人的对象上。

然后

speed = GetComponent<PlayerMovement>().MovementSpeed;
jump = GetComponent<PlayerMovement>().JumpForce

也是错误的。。从玩家那里获得价值并将其存储在敌人的田地里有什么用?

如果只有一个玩家,你可以简单地使用FindObjectOfType并进行

void Die()
{
Destroy(gameObject);
FindObjectOfType<PlayerMovement>().MovementSpeed += 1.0f;
FindObjectOfType<PlayerMovement>().JumpForce += 1.0f;
}

或者使用Singleton模式,就像前面提到的文档所建议的那样,例如

public class PlayerMovement : MonoBehaviour
{
public static PlayerMovement Instance { get; private set;}
private void Awake ()
{
if(Instance && Instance!= this)
{
Destroy(gameObject);
return;
}
Instance = this;
}
...
}

然后简单地进行

void Die()
{
Destroy(gameObject);
PlayerMovement.Instance.MovementSpeed += 1.0f;
PlayerMovement.Instance.JumpForce += 1.0f;
}

我假设你想在玩家杀死敌人时增加玩家的跳跃力和速度。此外,如果你有任何错误,或者只是速度没有增加,你能详细说明这个问题吗?

请找到敌人脚本的内联响应。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enemyScript : MonoBehaviour
{
public int health = 100;
private static float speed;//This is enemy speed variable that you have declared
private static float jump;//This is enemy jump variable that you have declared
public void TakeDamage(int damage)
{
health -= damage;
if (health <= 0)
{
Die();
//speed += 1.0f; Here you are increasing enemy speed and not playerspeed.
//jump += 1.0f; Same goes for jump.
}
}
void Die()
{
Destroy(gameObject);
//speed = GetComponent<PlayerMovement>().MovementSpeed; here you are assigning Player movement speed to enemy speed.
//jump = GetComponent<PlayerMovement>().JumpForce; here you are assigning Player movement jump to enemy jump.
//Instead try
GetComponent<PlayerMovement>().MovementSpeed += 1.0f;
GetComponent<PlayerMovement>().JumpForce += 1.0f;
}
}

你也可以使用

调试。Log(您的移动速度变量(;

检查玩家速度是否正在提高。

最新更新