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
|
1 Answer
Reset to default 0The 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);
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745107107a4611625.html
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:53if (transform.position.x > 10)
actually gets executed and thattransform.position.x
actually exceeds 10? – MindSwipe Commented Mar 3 at 8:22transform.position.x
is the actual position of the bullet? Did you meantarget
? – Jeroen van Langen Commented Mar 3 at 8:51