简体   繁体   中英

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. 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

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.

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

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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