简体   繁体   English

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

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

I'm trying to make it so that every time I kill an enemy, my player's speed increases by 1. I've been trying to do this but I don't really know what I'm doing.我正在努力让每次我杀死一个敌人时,我的玩家的速度都会增加 1。我一直在尝试这样做,但我真的不知道我在做什么。 Can somebody help me?有人可以帮助我吗?

Here is my player movement script这是我的玩家移动脚本

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);
        }
    }
}

Here is my Enemy script:这是我的敌人脚本:

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;
    }
}

Sorry, my question didn't have all the details, the player is not gaining any speed.抱歉,我的问题没有提供所有细节,玩家没有获得任何速度。 I tried using我尝试使用

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

and now I'm getting this error message现在我收到此错误消息

NullReferenceException: Object reference not set to an instance of an object NullReferenceException:Object 引用未设置为 object 的实例

Sorry for the inconvenience带来不便敬请谅解

I am assuming you want to increase the jumpforce and speed of your player when the player kills an enemy.我假设您想在玩家杀死敌人时增加玩家的跳跃力和速度。 Also, Could you please elaborate the question if you are getting any error or just the speed is not increasing?另外,如果您遇到任何错误或只是速度没有增加,您能否详细说明问题?

Please find the inline response for the Enemy Script.请找到 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;

}
}

Also you can use你也可以使用

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

to check if the player speed is being increased or not.检查玩家速度是否正在增加。

First of all it makes no sense to use GetComponent since the PlayerMovement is not attached to your enemy objects.首先,使用GetComponent是没有意义的,因为PlayerMovement没有附加到你的敌人对象上。

Then然后

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

is also the wrong way round.. what use would it be to take the value from the player and store it in a field of the enemy?也是错误的方式..从玩家那里获取价值并将其存储在敌人的领域有什么用?

If there is only one player anyway you could simply use FindObjectOfType and do如果无论如何只有一个玩家,您可以简单地使用FindObjectOfType并执行

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

Or alternatively use a Singleton Pattern as actually even suggested by before mentioned docs like eg或者或者使用 Singleton 模式,正如之前提到的文档所建议的那样,例如

public class PlayerMovement : MonoBehaviour
{
    public static PlayerMovement Instance { get; private set;}

    private void Awake ()
    {
        if(Instance && Instance!= this)
        {
            Destroy(gameObject);
            return;
        }

        Instance = this;
    }

    ...
}

and then simply do然后简单地做

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