Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在每次杀死敌人时提高速度?_C#_Unity3d - Fatal编程技术网

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

C# 如何在每次杀死敌人时提高速度?,c#,unity3d,C#,Unity3d,我正在努力使我的玩家的速度每杀死一个敌人增加1。我一直想这么做,但我真的不知道我在做什么。有人能帮我吗 这是我的玩家动作脚本 using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { public float MovementSpeed = 5; public float JumpForce = 5; private Rigidbo

我正在努力使我的玩家的速度每杀死一个敌人增加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);
        }
    }
}
使用System.Collections.Generic;
使用UnityEngine;
公共类玩家运动:单一行为
{
公众浮标移动速度=5;
公众浮力=5;
私有刚体2d_刚体;
私有void Start()
{
_刚体=GetComponent();
}
私有void更新()
{
//运动
var移动=输入。GetAxis(“水平”);
transform.position+=新矢量3(移动,0,0)*Time.deltaTime*移动速度;
if(Input.GetButtonDown(“Jump”)和&Mathf.Abs(_righidbody.velocity.y)<0.001f)
{
_刚体.AddForce(新矢量2(0,跳跃力),ForceMode2D.pulse);
}
}
}
这是我的敌人剧本:

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;
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类敌人脚本:MonoBehavior
{
公共卫生=100;
专用静态浮动速度;
私有静态浮跳;
公共空间损坏(内部损坏)
{
健康-=损害;

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

请查找敌人脚本的内联响应

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;

}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类敌人脚本:MonoBehavior
{
公共卫生=100;
私有静态浮点速度;//这是您声明的敌方速度变量
private static float jump;//这是您声明的敌方跳转变量
公共空间损坏(内部损坏)
{
健康-=损害;

如果(健康首先,使用
GetComponent
是没有意义的,因为
PlayerMovement
没有附加到敌人的物体上

然后


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

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

一个敌人在它上面会有一个<代码> PraseReals[/Cult]组件,这是违反直觉的。考虑找到一种方法让玩家知道它杀死了什么东西。不清楚如何调用<代码> TakeDamage < /C>函数,但是假设玩家调用它,你可以返回一个值来指示死亡。=只是伤害。你的场景中只有一个玩家吗?@derHugo有一个玩家为什么
PlayerMovement
组件会附加到敌人身上?@derHugo我假设他想在敌人死亡时提高玩家速度,我不知道如何承受伤害()函数由他调用。在敌人对象上使用
GetComponent
仍然没有意义。你摧毁了同一条线before@derHugo哦,对不起,我也在学习统一,我没有想到敌人的目标会被摧毁。如果你能解释一个更好的方法,我将不胜感激。再次感谢
void Die()
{
    Destroy(gameObject);
    FindObjectOfType<PlayerMovement>().MovementSpeed += 1.0f;
    FindObjectOfType<PlayerMovement>().JumpForce += 1.0f;
}
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;
}