c# - 想拿起工具或枪支,但想在统一 c# 中一次只选择一个
问题描述
想一次只捡起一把枪,但当我按下按钮时玩家正在捡起两把枪,所以请有人解决这个问题我想在玩家按下特定键但玩家放下枪时只捡起一把枪然后唯一的玩家可以选择另一把枪
public void pickup() //this is a function when player presses to pick up guns
{
if (Input.GetKeyDown(KeyCode.E) && Vector3.Distance(s.transform.position, transform.position) <= 5 && dontpickup)
{
//var a = gameObject.FindWithTag("player");
transform.SetParent(s);
Vector3 temp = transform.position;
temp.x = player.transform.position.x;
temp.y = player.transform.position.y;
transform.position = temp + new Vector3(4, 0, 0);
Rigidbody2D rb = GetComponent<Rigidbody2D>();
rb.isKinematic = true;
Collider2D c = GetComponent<Collider2D>();
c.isTrigger = true;
needdropdown = true;
}
}
public void dropdown() //this is a function when player presses to drop down the guns
{
if (Input.GetKeyDown(KeyCode.Q) && needdropdown)
{
//var a = gameObject.FindWithTag("player");
transform.SetParent(null);
Rigidbody2D rb = GetComponent<Rigidbody2D>();
rb.isKinematic = false;
Collider2D c = GetComponent<Collider2D>();
c.isTrigger = false;
}
}
void OnTriggerStay2D(Collider2D trig)
{
if(f.gameObject != null && gameObject.tag == "player")
{
dontpickup = true;
}
}
解决方案
我猜你应该在这两种方法中修改dontpickup的值:
public void pickup() //this is a function when player presses to pick up guns
{
if (Input.GetKeyDown(KeyCode.E) && Vector3.Distance(s.transform.position, transform.position) <= 5 && dontpickup)
{
//before code
needdropdown = true;
dontpickup = false;
}
}
public void dropdown() //this is a function when player presses to drop down the guns
{
if (Input.GetKeyDown(KeyCode.Q) && needdropdown)
{
//before code
needdropdown = false;
dontpickup = true;
}
}
推荐阅读
- php - 为什么这不起作用?Php 数组自动替换值和短数组
- java - Solr DIH 无法在 java.library.path 中出现错误 no sqljdbc_auth
- python - 如何注册与来自同一类的对象的碰撞,而没有来自我组中另一个类的不需要的对象?
- javascript - 在数组变量中查找变量
- javascript - 在 console.log TypeScript 中使用 Object.assign 获得 Object {_object: Array(0)} 推送选项卡
- python-3.x - 从 [pandas.core.frame.DataFrame] 变量中提取特定数据
- google-cloud-platform - Google Cloud Composer Worker 的 CPU 使用率很高
- javascript - WebRTC 录制视频时出错
- terraform - 每次添加配置块时,Terraform 都会尝试在循环中重新排序和重新创建配置块
- linux - 'cat /proc/self/maps' 返回的地址为什么以及如何在再次执行时发生变化