Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试在2D中拾取和投掷对象_C#_Unity3d - Fatal编程技术网

C# 尝试在2D中拾取和投掷对象

C# 尝试在2D中拾取和投掷对象,c#,unity3d,C#,Unity3d,我和我的同事正在用C#开发一款2D侧滚游戏。 玩家应该能够拾取物体(通过触摸它们)并投掷它们。 我们的计划是: 1) 为对象、对象的布尔值和对象的刚体创建变量。 2) 确认物体是否接触播放器 3) 如果为真,则对象将成为玩家的父对象(将其位置设置为玩家的手)。 4) 要抛出,代码将检查玩家是否拥有该对象(通过使用布尔值),然后它将通过addforce分离并抛出。 代码没有任何错误,除了投掷部分(玩家可以抓取和取消抓取,但不能投掷)外,其他部分都有效。 玩家可以拾取和取消拾取,但不能投掷,我不明白

我和我的同事正在用C#开发一款2D侧滚游戏。 玩家应该能够拾取物体(通过触摸它们)并投掷它们。 我们的计划是: 1) 为对象、对象的布尔值和对象的刚体创建变量。 2) 确认物体是否接触播放器 3) 如果为真,则对象将成为玩家的父对象(将其位置设置为玩家的手)。 4) 要抛出,代码将检查玩家是否拥有该对象(通过使用布尔值),然后它将通过addforce分离并抛出。 代码没有任何错误,除了投掷部分(玩家可以抓取和取消抓取,但不能投掷)外,其他部分都有效。 玩家可以拾取和取消拾取,但不能投掷,我不明白为什么,因为代码看起来正确,并且控制台没有显示任何错误:/ 请查看我的代码:

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Inventory : MonoBehaviour
{
    public bool ispicked1 = false;
    public GameObject pickable1;
    public Rigidbody2D pickable1rb;
    public GameObject Parent;
    public float force;

    void Start() {
        pickable1rb = GetComponent<Rigidbody2D>();
    }

     void Update() {
        if (ispicked1 == true) {
            pickable1.transform.position = Parent.transform.position;
        } 
      else if (ispicked1 == false) {
            pickable1.transform.parent = null;
        }

        if (Input.GetMouseButton(1) && ispicked1 == true) {
            ispicked1 = false;
            pickable1rb.AddForce(transform.up * force, ForceMode2D.Impulse);
        }
    }
    private void OnCollisionEnter2D(Collision2D collision) {

        if (collision.gameObject.name == "pickable1") {
            Debug.Log("Tocou em objecto");
            ispicked1 = true;
            pickable1.transform.SetParent(Parent.transform);
        }
    }

}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类清单:单一行为
{
公共bool ispicked1=假;
公共游戏对象可拾取1;
公共刚体2d可拾取1rb;
公共游戏对象父对象;
公众浮力;
void Start(){
pickable1rb=GetComponent();
}
无效更新(){
如果(ispicked1==true){
pickable1.transform.position=Parent.transform.position;
} 
否则如果(ispicked1==false){
pickable1.transform.parent=null;
}
if(Input.GetMouseButton(1)和&ispicked1==true){
ispicked1=假;
pickable1rb.AddForce(transform.up*力,ForceMode2D.pulse);
}
}
专用空心OnCollisionInter2D(碰撞2D碰撞){
如果(collision.gameObject.name==“pickable1”){
Log(“Tocou em objecto”);
ispicked1=真;
pickable1.transform.SetParent(Parent.transform);
}
}
}
旁白:我想让球员朝他所面对的方向投,什么是最好的方式?我只能在右侧、左侧或向上选择:/

更新: 我解决了所有的问题,并为要抛出的对象创建了一个侧脚本,它们100%正常工作!代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Inventory : MonoBehaviour
{
    public bool ispicked1 = false;
    public bool ispicked2 = false;
    public GameObject pickable1;
    public GameObject pickable2;
    public GameObject Parent;
    public bool isThrown = false;
    public ThrowableObject throwableinstance1;
    public ThrowableObject throwableinstance2;
    public bool isfull = false; 

    void Start() {
        throwableinstance1 = GameObject.Find("pickable1").GetComponent<ThrowableObject>();
        throwableinstance2 = GameObject.Find("pickable2").GetComponent<ThrowableObject>();
    }

     void Update() {
        if (ispicked1 == true) {
            pickable1.transform.position = Parent.transform.position;
            isfull = true;
        } 
      else if (ispicked1 == false) {
            pickable1.transform.parent = null;
        }

        if (ispicked2 == true) {
            pickable2.transform.position = Parent.transform.position;
            isfull = true;
        } else if (ispicked2 == false) {
            pickable2.transform.parent = null;
        }

        if (Input.GetMouseButton(1) && ispicked1 == true) {
            ispicked1 = false;
            isThrown = true;
            throwableinstance1.Throw();
            isfull = false;
        }

        if (Input.GetMouseButton(1) && ispicked2 == true) {
            ispicked2 = false;
            isThrown = true;
            throwableinstance2.Throw();
            isfull = false;
        }
    }
    private void OnCollisionEnter2D(Collision2D collision) {

        if (collision.gameObject.name == "pickable1" && isfull == false) {
            ispicked1 = true;
            pickable1.transform.SetParent(Parent.transform);
        }

        if (collision.gameObject.name == "pickable2" && isfull == false) {
            ispicked2 = true;
            pickable2.transform.SetParent(Parent.transform);
        }
    }

}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类清单:单一行为
{
公共bool ispicked1=假;
公共bool ispicked2=假;
公共游戏对象可拾取1;
公共游戏对象可拾取2;
公共游戏对象父对象;
public bool isThrown=false;
公共ThrowableObject throwableinstance1;
公共可丢弃对象可丢弃实例2;
公共bool isfull=false;
void Start(){
throwableinstance1=GameObject.Find(“pickable1”).GetComponent();
throwableinstance2=GameObject.Find(“pickable2”).GetComponent();
}
无效更新(){
如果(ispicked1==true){
pickable1.transform.position=Parent.transform.position;
isfull=true;
} 
否则如果(ispicked1==false){
pickable1.transform.parent=null;
}
如果(ispicked2==true){
pickable2.transform.position=父级.transform.position;
isfull=true;
}否则如果(ispicked2==false){
pickable2.transform.parent=null;
}
if(Input.GetMouseButton(1)和&ispicked1==true){
ispicked1=假;
isThrown=true;
throwableinstance1.Throw();
isfull=false;
}
if(Input.GetMouseButton(1)和&ispicked2==true){
ispicked2=假;
isThrown=true;
throwableinstance2.Throw();
isfull=false;
}
}
专用空心OnCollisionInter2D(碰撞2D碰撞){
if(collision.gameObject.name==“pickable1”&&isfull==false){
ispicked1=真;
pickable1.transform.SetParent(Parent.transform);
}
if(collision.gameObject.name==“pickable2”&&isfull==false){
ispicked2=真;
pickable2.transform.SetParent(Parent.transform);
}
}
}
下面是抛出可拾取对象的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ThrowableObject : MonoBehaviour
{

    public Inventory inventoryinstance;
    public Rigidbody2D throwablerb;
    public Transform Player;
    public GameObject PickableObject;

    public EnemyHealth1 enemyhealth1instance;
    public EnemyHealth2 enemyhealth2instance;
    public EnemyHealth3 enemyhealth3instance;

    void Start()
    {
        inventoryinstance = GameObject.Find("Player").GetComponent<Inventory>();

        enemyhealth1instance = GameObject.Find("enemy1").GetComponent<EnemyHealth1>();
        enemyhealth2instance = GameObject.Find("enemy2").GetComponent<EnemyHealth2>();
        enemyhealth3instance = GameObject.Find("enemy3_leper").GetComponent<EnemyHealth3>();
    }

    public void Throw()
    {
        if(Player.localScale.x < 1)
        {
            throwablerb.AddForce(transform.right * -1);
        } else if(Player.localScale.x > 0)
        {
            throwablerb.AddForce(transform.right);
        }
    }

     private void OnCollisionEnter2D(Collision2D collision) {
        if (collision.gameObject.name == "enemy1") {
                enemyhealth1instance.GetComponent<EnemyHealth1>().EnemyHealthbar1-= 1;
                Destroy(PickableObject);
    }
     if (collision.gameObject.name == "enemy2") {
            enemyhealth2instance.GetComponent<EnemyHealth2>().EnemyHealthbar2-=1;
            Destroy(PickableObject);
    }
 if (collision.gameObject.name == "enemy3_leper") {
            enemyhealth3instance.GetComponent<EnemyHealth3>().EnemyHealthbar3-=1;
            Destroy(PickableObject);
    }
    }

    private void OnTriggerEnter2D(Collider2D col)
    {
         if (col.gameObject.name == "enemy1_hitbox") {
            enemyhealth1instance.GetComponent<EnemyHealth1>().EnemyHealthbar1-=1;
            Destroy(PickableObject);
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类ThrowableObject:单行为
{
公共库存清单实例;
公共刚体2d一次性b;
公共转换播放器;
公共游戏对象PickableObject;
公共EnemyHealth1 EnemyHealth1机构;
公共EnemyHealth2 EnemyHealth2机构;
公共EnemyHealth3 EnemyHealth3实例;
void Start()
{
inventoryinstance=GameObject.Find(“Player”).GetComponent();
enemyhealth1instance=GameObject.Find(“enemy1”).GetComponent();
enemyhealth2instance=GameObject.Find(“enemy2”).GetComponent();
enemyhealth3instance=GameObject.Find(“enemy3_leper”).GetComponent();
}
公开作废
{
if(Player.localScale.x<1)
{
throwablerb.AddForce(transform.right*-1);
}else if(Player.localScale.x>0)
{
throwablerb.AddForce(transform.right);
}
}
专用空心OnCollisionInter2D(碰撞2D碰撞){
如果(collision.gameObject.name==“enemy1”){
enemyhealth1instance.GetComponent().EnemyHealthbar1-=1;
销毁(可拾取物体);
}
如果(collision.gameObject.name==“enemy2”){
enemyhealth2instance.GetComponent().EnemyHealthbar2-=1;
销毁(可拾取物体);
}
if(collision.gameObject.name==“enemy3_leper”){
enemyhealth3instance.GetComponent().EnemyHealthbar3-=1;
销毁(可拾取物体);
}
}
私有无效OnTriggerEnter2D(冲突R2D列)
{
if(col.gameObject.name==“enemy1\u hitbox”){
enemyhealth1instance.GetComponent().EnemyHealthbar1-=1;
销毁(可拾取物体);
public class Inventory : MonoBehaviour
{
    private bool carryingItem => pickedUpItem == null;
    private PickableItem pickedUpItem;
    [SerializeField] private Vector2 throwingForce;
    [SerializeField] private Transform spawnPosition; //Should be a child of the player gameobject. This will be position of the object
    [SerializeField] private Transform handTransform; //Should also be a child of the player gameobject. This needs to be animated for the keyframes that move the hand

    void Update()
    {
        if (carryingItem && Input.GetMouseButtonDown(1))
        {
            pickedUpItem.transform.SetParent(null);
            pickedUpItem.transform.position = spawnPosition.position;
            pickedUpItem.GetComponent<Rigidbody2D>().AddForce(transform.right * throwingForce.x + transform.up * throwingForce.y);
            pickedUpItem = null;
        }
    }

    private void OnCollisionEnter2D(Collision2D other)
    {
        var pickable = other.transform.GetComponent<PickableItem>();
        if (pickable && pickedUpItem == null) //This kind of depends on the game design because maybe you want to pick it up if you're carrying something already
        {
            pickedUpItem = pickable;
            pickable.transform.SetParent(handTransform);
        }

        pickable.transform.localPosition.Set(0,0,0);
    }
}