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

c# - What is wrong with my logic when deleting a gameObject when it reaches a certain x value? - Stack Overflow

programmeradmin0浏览0评论

I am currently implementing the script for my bullet gameObject but the current logic does not destroy the bullet object when its x position reaches a value of 10.

using UnityEngine;

public class Bullet : MonoBehaviour {
    
    [Header("References")]
    [SerializeField] private Rigidbody2D rb;

    [Header("Attributes")]
    [SerializeField] private float bulletSpeed = 200f;
    [SerializeField] private int dmg = 1;

    private Transform target;

    public void SetTarget(Transform _target){
        target = _target;
    }

    private void FixedUpdate(){
        if(!target){
            return;
        }
        Vector2 direction = (target.position - transform.position).normalized;
        rb.linearVelocity= direction * bulletSpeed;
        
        if(transform.position.x > 10){
            Destroy(gameObject);
        }
    }

    void OnCollisionEnter2D(Collision2D other){
        other.gameObject.GetComponent<Health>().TakeDamage(dmg);
        Destroy(gameObject);
    }

}

I am currently implementing the script for my bullet gameObject but the current logic does not destroy the bullet object when its x position reaches a value of 10.

using UnityEngine;

public class Bullet : MonoBehaviour {
    
    [Header("References")]
    [SerializeField] private Rigidbody2D rb;

    [Header("Attributes")]
    [SerializeField] private float bulletSpeed = 200f;
    [SerializeField] private int dmg = 1;

    private Transform target;

    public void SetTarget(Transform _target){
        target = _target;
    }

    private void FixedUpdate(){
        if(!target){
            return;
        }
        Vector2 direction = (target.position - transform.position).normalized;
        rb.linearVelocity= direction * bulletSpeed;
        
        if(transform.position.x > 10){
            Destroy(gameObject);
        }
    }

    void OnCollisionEnter2D(Collision2D other){
        other.gameObject.GetComponent<Health>().TakeDamage(dmg);
        Destroy(gameObject);
    }

}
Share Improve this question edited Mar 3 at 6:49 Brian Rogers 130k31 gold badges311 silver badges310 bronze badges asked Mar 3 at 6:30 azaessaazaessa 1 4
  • Your code says that x has to be more than 10 to destroy the game object. Did you mean to use >= instead of >? – Brian Rogers Commented Mar 3 at 6:53
  • my main condition was going to be, if x is more than 10 and x is less than -10. If the condition is met, the bullet should be destroyed. But the thing is, when transform.position.x reaches a value of 10 it never destroys the bullet – azaessa Commented Mar 3 at 6:58
  • 1 Have you attached a debugger and checked that your if (transform.position.x > 10) actually gets executed and that transform.position.x actually exceeds 10? – MindSwipe Commented Mar 3 at 8:22
  • 1 Are you sure that transform.position.x is the actual position of the bullet? Did you mean target? – Jeroen van Langen Commented Mar 3 at 8:51
Add a comment  | 

1 Answer 1

Reset to default 0

The Destroy checking for that position makes no sense. If the bullet is shot towards -x it will never reach x > 10 so you have two options:

  • Check if the bullet has travel certain distance
private Vector3 startPos;

private void Start(){
    startPos = transform.position;
}


private void FixedUpdate(){
    if(!target){
        return;
    }
    Vector2 direction = (target.position - transform.position).normalized;
    rb.linearVelocity= direction * bulletSpeed;
    
    float dist = Vector3.Distance(transform.position, startPos);
    if(dist > 10){
        Destroy(gameObject);
    }
}

  • Check its lifetime passes some duration.
[SerializeField] private float lifetime = 10f; //Seconds


private void FixedUpdate(){
    if(!target){
        return;
    }
    Vector2 direction = (target.position - transform.position).normalized;
    rb.linearVelocity= direction * bulletSpeed;
    
    lifetime-= Time.fixedDeltaTime;
    if(lifetime <= 0){
        Destroy(gameObject);
    }
}

发布评论

评论列表(0)

  1. 暂无评论