选择特定的预制件
Selecting specific prefab
我创建了多个相同的预制克隆体 like that.,我想销毁其中一个 select。
首先,我使用 raycast2d 检测命中并与预制件碰撞
if(Input.GetMouseButtonDown(1))
{
RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint((Input.mousePosition)), Vector2.zero);
if(hit.collider != null && hit.collider.gameObject==gameObject){
health-=PlayerScript.damage;
if(health<=0){
Destroy(hit.collider.gameObject);
}
}
}
此脚本影响所有预制件的健康。所以我决定用对撞机创建一个游戏对象,如果这个游戏对象与预制件发生碰撞,预制件会失去健康然后销毁。
我像这样实例化游戏对象
GameObject object=Instantiate(selectOb,Camera.main.ScreenToWorldPoint(Input.mousePosition), Quaternion.identity) as GameObject;
然后我使用 void OnCollision2DEnter() 检查游戏对象和预制件之间是否存在碰撞。
现在有不同的问题。游戏对象不会与其他预制件发生碰撞,因为
area sizes of collider
我希望,我可以描述我的问题。
我正在等待您关于 select 特定预制件或我的碰撞问题的建议
场景中的游戏对象(可能是用作控制器的空游戏对象)上的类似内容:
请注意,如果您使用默认相机方向,光线投射的方向应为 -Vector2.up
。
public class RayShooter : MonoBehaviour
{
void Update()
{
if(Input.GetMouseButtonDown(1)) // 1 is rightclick, don't know if you might want leftclick instead
{
RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint((Input.mousePosition)), -Vector2.up);
if(hit.collider != null)
{
GameObject target = hit.collider.gameObject;
target.GetComponent<Enemy>().dealDamage(PlayerScript.damage);
// "Enemy" is the name of the script on the things you shoot
}
}
}
}
关于你拍的东西(这里叫"Enemy"):
public class Enemy : MonoBehaviour
{
int health = 10;
public void dealDamage(int value)
{
health -= value;
if(health <= 0)
{
Destroy(gameObject);
}
}
}
这将是一个非常基本的设置,显然你要拍摄的东西需要一个对撞机才能工作。此外,在光线投射中包含图层和图层蒙版可能是个好主意。
我创建了多个相同的预制克隆体 like that.,我想销毁其中一个 select。
首先,我使用 raycast2d 检测命中并与预制件碰撞
if(Input.GetMouseButtonDown(1))
{
RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint((Input.mousePosition)), Vector2.zero);
if(hit.collider != null && hit.collider.gameObject==gameObject){
health-=PlayerScript.damage;
if(health<=0){
Destroy(hit.collider.gameObject);
}
}
}
此脚本影响所有预制件的健康。所以我决定用对撞机创建一个游戏对象,如果这个游戏对象与预制件发生碰撞,预制件会失去健康然后销毁。 我像这样实例化游戏对象
GameObject object=Instantiate(selectOb,Camera.main.ScreenToWorldPoint(Input.mousePosition), Quaternion.identity) as GameObject;
然后我使用 void OnCollision2DEnter() 检查游戏对象和预制件之间是否存在碰撞。
现在有不同的问题。游戏对象不会与其他预制件发生碰撞,因为 area sizes of collider
我希望,我可以描述我的问题。 我正在等待您关于 select 特定预制件或我的碰撞问题的建议
场景中的游戏对象(可能是用作控制器的空游戏对象)上的类似内容:
请注意,如果您使用默认相机方向,光线投射的方向应为 -Vector2.up
。
public class RayShooter : MonoBehaviour
{
void Update()
{
if(Input.GetMouseButtonDown(1)) // 1 is rightclick, don't know if you might want leftclick instead
{
RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint((Input.mousePosition)), -Vector2.up);
if(hit.collider != null)
{
GameObject target = hit.collider.gameObject;
target.GetComponent<Enemy>().dealDamage(PlayerScript.damage);
// "Enemy" is the name of the script on the things you shoot
}
}
}
}
关于你拍的东西(这里叫"Enemy"):
public class Enemy : MonoBehaviour
{
int health = 10;
public void dealDamage(int value)
{
health -= value;
if(health <= 0)
{
Destroy(gameObject);
}
}
}
这将是一个非常基本的设置,显然你要拍摄的东西需要一个对撞机才能工作。此外,在光线投射中包含图层和图层蒙版可能是个好主意。