public class Blogs : MonoBehaviour { private bool isMoving = false; private Vector2 moveDirection; private float moveDistance; public float speed; float timeTemp; public Vector2 clickPosition; public Transform targetPoint; public Vector3 localPosition; // Start is called before the first frame update void Start() { moveDirection = clickPosition - (Vector2)transform.position; moveDistance = moveDirection.magnitude; moveDirection.Normalize(); isMoving = true; Destroy(gameObject, 0.3f); } // 子弹方向 void Update() { if (isMoving) { transform.position += (Vector3)moveDirection * speed * Time.deltaTime; moveDistance -= speed * Time.deltaTime; if (moveDistance <= 0f) { isMoving = false; Destroy(gameObject); } } } }优化不和特定物品发生碰撞
时间: 2023-06-16 22:04:11 浏览: 167
crypto_blogs:有关CryptographySecurity的博客
可以使用Physics2D.IgnoreCollision()函数来忽略与特定物品的碰撞。在Start()函数中,可以添加以下代码:
```csharp
// 获取特定物品的Collider2D组件
Collider2D targetCollider = targetPoint.GetComponent<Collider2D>();
// 忽略与特定物品的碰撞
Physics2D.IgnoreCollision(GetComponent<Collider2D>(), targetCollider);
```
这样就可以确保子弹不会与特定物品发生碰撞了。当然,如果需要忽略多个物品的碰撞,可以在代码中添加更多的Physics2D.IgnoreCollision()函数。
阅读全文