2016-01-14 133 views
1

我有一个手臂,它工作的很好,但是,当我尝试抛出一个物体时,它不起作用。投掷物体

Example

经过一番思考,我做出这样的:

cube.GetComponent<Rigidbody2D>().velocity = player.GetComponent<Rigidbody2D>().velocity; 

而发生这种情况:

Example

,我打算给刚体连接到手臂,但他从以重力,我试图扭转重力,但它不起作用。

我的问题是,我如何使它与手臂一起工作?

编辑:为更好地解释整个代码(这是附着在手):

void Update() { 
     Collider2D touch = Physics2D.OverlapCircle(touchDetect.position, 0.01f, objectLayer); 
     mao1.SetBool("Ligado", ligado); 


     if (Input.GetKey(KeyCode.LeftShift)) { 
      ligado = true; 
     } else { 
      ligado = false; 
     } 

     if (Input.GetKey(KeyCode.LeftShift) && touch != null && ligado == true) 
     { 
      touch.gameObject.transform.parent = this.transform; 
      cubo.GetComponent<Rigidbody2D>().isKinematic = true; 
      Physics2D.IgnoreLayerCollision(10, 12, true); 
      cubo.GetComponent<Rigidbody2D>().velocity = player.GetComponent<Rigidbody2D>().velocity; 

     } 
     else if (touch != null && ligado == false) 
     { 
      touch.gameObject.transform.parent = null; 
      cubo.GetComponent<Rigidbody2D>().isKinematic = false; 
      Physics2D.IgnoreLayerCollision(10, 12, false); 

     }   

    } 
+0

不幸的是,对于这个网站,这个问题太基本,一般和含糊。我真的只是在寻找基本的Unity教程。请务必检查answers.unity3d.com网站,您可以在其中询问(甚至已经是谷歌)更基本的“非常一般的启动器”问题 – Fattie

回答

1

如果我没有理解好,你按LeftShift当立方体与你的手臂,它的尖端接触“粘贴“立方体到手臂,然后当你释放LeftShift时,你想把立方体放在某个方向。

可以实现在使用AddForce功能Rigidbody2D与精心计算的向量:

// In the script outside of a function 
public Transform greenCubeCenter; 

// In your Update function 
else if (touch != null && ligado == false) 
{ 
    touch.gameObject.transform.parent = null; 
    cubo.GetComponent<Rigidbody2D>().isKinematic = false; 
    Physics2D.IgnoreLayerCollision(10, 12, false); 

    // Below the important part 
    Vector2 force = (touchDetect.position - greenCubeCenter.position).normalized * ThrowForceMagnitude; // ThrowForceMagnitude needs to be defined somewhere 
    touch.GetComponent<Rigidbody2D>().AddForce(force); 
} 

,只是把一个变换是在greenCubeCenter变量立方体的中心。

该算法应该(如果我再次明白了一切好)抛出的对象从立方体远去的方向立方体 - 与ThrowForceMagnitude的力提示手臂(你需要的地方定义)

UPDATE:好吧了解一切后,

远一点这里是一个更完整的答案:

public Transform greenCubeCenter; 
public float ThrowForceMagnitude = 50.0f;  

Transform touchedObject = null; // this will hold the touched object 

void Update() { 
    Collider2D touch = Physics2D.OverlapCircle(touchDetect.position, 0.01f, objectLayer); 

    if(touch == null) // we don't do anything if we don't touch anything 
     return; 

    if (Input.GetKey(KeyCode.LeftShift)) { // let's do everything in one if 
     ligado = true; // if the arm touch the cube we set ligado to true 

     if(touchedObject == null) // we didn't have anything grabbed 
     { 
      touchedObject = touch.gameObject.transform; 
      touchedObject.parent = this.transform; // "glue" the object to the arm 
      touchedObject.GetComponent<Rigidbody2D>().isKinematic = true; // forbid other forces to move our object 
      Physics2D.IgnoreLayerCollision(10, 12, true); // I let this thing here, don't really know what it does in this context 
     } 

    } else { 
     ligado = false; // in any case we're not in "grab mode" anymore so let's set ligado to false 

     if(touchedObject != null) // if we were grabing something 
     { 
      touchedObject.parent = null; // let the object free 
      touchedObject.GetComponent<Rigidbody2D>().isKinematic = false; // let it be affected by reality again 
      Physics2D.IgnoreLayerCollision(10, 12, false); // restoring the think I didn't understand before hand 

      // Below the actual throwing part 
      Vector2 force = (touchDetect.position - greenCubeCenter.position).normalized * ThrowForceMagnitude; 
      touchedObject.GetComponent<Rigidbody2D>().AddForce(force); // actually throwing the object 

      touchedObject = null; // we let the object go so we set touchedObject to null 
     } 
    } 

    mao1.SetBool("Ligado", ligado); // updating display 
} 

这不是一个完美的代码,从算法上讲(即使是一个字)它应该是正确的,但你应该逐行深入并理解一切(我认为我已经评论足够了)。

请注意,我删除了对Cubo的引用,我认为你可以在objectLayer中获取任何东西。

+0

Define ThrowForceMagnitude您的意思是价值吗?如果是这样,我设置200(例如),这是发生:![例子](https://i.gyazo.com/a3eaea525d2e3a880a6b09544faae4ce.gif)我甚至没有按LeftShift –

+0

是的只是给它一个值^ ^我试图猜测你的变量在哪里,是玩家移动立方体还是interogation立方体? – Sharundaar

+0

审问立方哈哈抱歉,没有把这个清楚 –