繁体   English   中英

每次击杀敌人如何提高速度?

[英]How do I increase speed every time I kill an enemy?

我正在努力让每次我杀死一个敌人时,我的玩家的速度都会增加 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:Object 引用未设置为 object 的实例

带来不便敬请谅解

我假设您想在玩家杀死敌人时增加玩家的跳跃力和速度。 另外,如果您遇到任何错误或只是速度没有增加,您能否详细说明问题?

请找到 Enemy Script 的内联响应。

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;

}
}

你也可以使用

Debug.Log(你的移动速度变量);

检查玩家速度是否正在增加。

首先,使用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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM