最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Speed boost power-up code not working. Power-up item not being consumed - Stack Overflow

programmeradmin2浏览0评论

My code has no errors but when I move my character in to the power-up it is not destroyed. The speed boost does not appear to work either when i move the player over the power-up item. I'm really not sure what the problem is but it might lie in the OnTriggerEnter I think. The tag has the same name on the power-up item so really cant figure out what's wrong.

private float speed;
private float boostTimer;
private bool boosting;
private bool moving;


void Start()
{
    moving = false;
    transform.Translate(1, 1, 1);

    speed = 5;
    boostTimer = 0;
}

if (boosting)
{
    boostTimer += Time.deltaTime;
    if (boostTimer >= 3)
    {
        speed = 5;
        boostTimer = 0;
        boosting = false;
     }
 }

 void OnTriggerEnter(Collider other)
 {
     if(other.tag == "SpeedBoost")
     {
         boosting = true;
         speed = 10;
         Destroy(other.gameObject);
     }
  }

  if (moving)
  {
     this.transform.Translate(new Vector3(Time.deltaTime * speed, 0, 0));
  }

My code has no errors but when I move my character in to the power-up it is not destroyed. The speed boost does not appear to work either when i move the player over the power-up item. I'm really not sure what the problem is but it might lie in the OnTriggerEnter I think. The tag has the same name on the power-up item so really cant figure out what's wrong.

private float speed;
private float boostTimer;
private bool boosting;
private bool moving;


void Start()
{
    moving = false;
    transform.Translate(1, 1, 1);

    speed = 5;
    boostTimer = 0;
}

if (boosting)
{
    boostTimer += Time.deltaTime;
    if (boostTimer >= 3)
    {
        speed = 5;
        boostTimer = 0;
        boosting = false;
     }
 }

 void OnTriggerEnter(Collider other)
 {
     if(other.tag == "SpeedBoost")
     {
         boosting = true;
         speed = 10;
         Destroy(other.gameObject);
     }
  }

  if (moving)
  {
     this.transform.Translate(new Vector3(Time.deltaTime * speed, 0, 0));
  }
Share Improve this question edited Jan 17 at 22:14 derHugo 91.3k9 gold badges91 silver badges135 bronze badges asked Jan 17 at 18:42 stratutravstratutrav 11 bronze badge 5
  • is the thing marked trigger? is it 3d? are you sure the tag matches? what debugging did you do? – BugFinder Commented Jan 17 at 20:51
  • This is 3d and the power up is marked as a trigger. – stratutrav Commented Jan 17 at 20:58
  • Please better watch your questions tags here ... This is not specifically about an IDE (visual-studio), collider is A Websocket signalling server written in the go language for WebRTC -> absolutely unrelated, move Usually refers to move semantics; consider using that tag instead. Move semantics is a programming language feature that allows a copy operation to be replaced by a more efficient "move"... -> absolutely unrelated, triggers are rules that perform actions or invoke functions in response to events such as matching conditions or database changes -> absolutely unrelated – derHugo Commented Jan 17 at 22:14
  • 1 And then your provided code wouldn't compile ... There are some code blocks outside of any methods... Please post your actual code as minimal reproducible example .. currently my guess would be the OnTriggerEnter is nested as a local function within the Update method so Unity's messaging system doesn't know about it so it isn't invoked by the physics .. it has to be on class level scope – derHugo Commented Jan 17 at 22:15
  • 1 Then also note that you move through Transform which can break physics and collision detection .. you should rather move through a Rigidbody – derHugo Commented Jan 18 at 8:42
Add a comment  | 

1 Answer 1

Reset to default -1

The code you've put here doesn't make sense because the if statements are just sitting in the class, but maybe you are implying that those if statements are in the Update method, not that this is your actual code.

Here's an example of how it would work in a 2D game. Is Trigger has to be checked to use the on trigger. You can also attach the debugger to unity and put a break point inside on trigger method to see if it gets called at all. Also have you made sure the powerup has a collider?

public class Player : MonoBehaviour
{
    private float _speed = 4.5f;
    private bool _isSpeedPowerupActive = false;

    //other code or a Start() method

    void Update()
    {         
     // define a new Vector3 called "direction" here if the player is moving

        if (_isSpeedPowerupActive)
        {
            transform.Translate(direction * 2 * _speed * Time.deltaTime);
        }
        else
        {
            transform.Translate(direction * _speed * Time.deltaTime);
        }
    }

    IEnumerator ExpirePowerup(float duration)
    {
        yield return new WaitForSeconds(duration);
        _isSpeedPowerupActive = false;

    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.tag == "Powerup")
        {
            _isSpeedPowerupActive = true;
            StartCoroutine(ExpirePowerup(5.0f)));
            Destroy(other.gameObject);
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论