Search Unity

Moving Object Up

Discussion in 'Getting Started' started by Simpso, Apr 29, 2015.

  1. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    ARgghhh Tears.

    I have been trying to fling an object up out of a box ( Think Mario's coin box)
    I am running into issues with the 'coin' coming out of the side of the box or not at all.
    Any help

    Have include code below
    Coins are instantiated into a none rigidbody cube

    Code (csharp):
    1. Instantiate(Coin,transform.position, transform.rotation );
    2.      rb.AddForce (new Vector3(0,CoinJump ,0), ForceMode.Acceleration) ; // this method causes coins to jump out of the side of container box
    3.      Coin.transform.position = Coin.transform.up * CoinJump; // These two methods appear to do nothing.
    4.      Coin.transform.Translate (new Vector3(0.0f, 1.0f, 0.0f), Space.World);
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    AddForce adds force in the local axis of the object being applied to. So despite Y being up for the world, the object may not be oriented with Y pointing up. If the object was modeled in Blender, for example, the Z axis is up and down, and importing the model into Unity, things may have gotten out-of-whack.

    Experiment by applying the force to either the X or Z axis, and if it's moving along the correct path, but the wrong direction, flip it (apply negative force). That's not a great solution, I suppose, as it doesn't really solve the issue, but rather Band-Aids it. Still, it should point you in the right direction.
     
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,205
    I believe you're thinking of AddRelativeForce. A couple quick experiments, with a cube turned up and then turned side ways, seems to confirm that AddForce is world and AddRelativeForce is local.

    http://docs.unity3d.com/ScriptReference/Rigidbody.AddRelativeForce.html
     
    Last edited: Apr 29, 2015
    BrandyStarbrite and Schneider21 like this.
  4. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    I never even thought about that. The object is a simple unity cube but I did rotate it on all axis before prefab it. so that would explain why possibly the box is flying out the side as that is along the box y axis.
    So is there anyway to get the cube to use world co ordinates?
     
  5. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    D'oh!

    @Simpso, as Ryiah pointed out, AddForce should be using world coordinates. Maybe you could post a few screenshots of your scene hierarchy, Inspector window, etc...
     
  6. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    As requested.
    Image one inspector for the coin object


    Image 2 - Inspector for the box the Coin ' live' in



    Image 3 the Hierarchy of the scene. The coins are instantiated and build up in the scene when the fly out of the left of the box instead of the top.
     
  7. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    So I'm wondering... if the box has a collider on it, is that just pushing the coin out in whatever direction it feels like (maybe X axis is checked first?) as soon as it's instantiated?

    Also, the reason why those two lines do nothing in your code is because they're not referencing the instance of the coin. I'm assuming "Coin" is a public GameObject variable where you dragged the coin prefab. (PSA: avoid capitalized variable names). Not sure what rb is pointing at... Suggested it's a rigidbody, but don't see where that's being set. So to actually affect the coin, you'd have to do something like:
    Code (CSharp):
    1. GameObject coinInstance = Instantiate(Coin,transform.position, transform.rotation);
    2. coinInstance.GetComponent<RigidBody>().AddForce(Vector3.up, ForceMode.Acceleration);
    This is also assuming this script is attached to the cube, not the coin prefab. Which I'm thinking is where you actually have it attached, now that I consider that rb variable...
     
  8. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    The script is attached to the cube. The rB gigidbody call is up above under start() .

    I had a feeling that was the issue with the bottom two line but I couldn't figure out they untag for referencing the coin game object.

    So simple as adding coinInstance. Before the rest of the code?
     
  9. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Instantiation returns the object created, so storing it in a variable allows you to perform further actions, like renaming, changing materials, or in this case, adding force. The variable name (coinInstance) is irrelevant, but yes, doing that should give you something closer to what you're expecting, I think.
     
  10. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,205
    Yes, that's exactly what is happening. I threw together a quick prototype consisting of a block that spawns and shoots a coin-shaped cylinder upwards when the space bar is pressed. With the block's collider turned off, it goes up. With the block's collider turned on, it goes sideways.
     
    BrandyStarbrite and Schneider21 like this.
  11. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Excellent detective work, Ryiah.
     
    BrandyStarbrite likes this.
  12. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    That is great thanks for your help Riyah And Schneider21, i will try this out tonight.
    Just so i know, why would the collider cause the object to shoot out in different directions depending on if its on or not?
     
  13. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,205
    You're spawning one object within another. Colliders for both objects are thus interacting with each other. The initial burst of force may be trying to force it upwards, but the colliders interacting cause the coin to ricochet off on a different path.

    If you want to launch the coin from within the block, you'll need to put it on another collision layer so it doesn't try to interact with the block's collider. Or alternatively you could simply not use physics with the coin. If the coin is simply going to appear, go up a specified distance, and vanish it may not really need physics.

    http://docs.unity3d.com/Manual/LayerBasedCollision.html
     
    Last edited: Apr 30, 2015
    Schneider21 likes this.
  14. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    Oh right i get it. The thought of that that may have been what was happening did cross my mind at one point.
    I didnt look into it further though as the cube was still coming out of the box. My train of thought was if it was bouncing around inside it would never actually fall out of the bigger cube.
     
  15. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,205
    Yes, in a real world situation the block would have to be hollow to allow the coin to start from within it, but the physics system is not that complex and thus only sees it as two colliders trying to occupy the same space.
     
    Last edited: Apr 30, 2015
    Simpso likes this.
  16. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    Thanks for the heads up there. Missed i wasnt pointint to the PREFAB.
    I added the parts to my code as suggested but now i get the following error:


    Code (csharp):
    1.  
    2.       coinIn = Instantiate(Coin,transform.position, transform.rotation) as GameObject  ;
    3.      coinIn.GetComponent<Rigidbody>().AddForce (Vector3.up , ForceMode.Acceleration) ;
    However the box is simply falling (using Gravity) instead of springing up.

    EDIT : Changed my code to this :

    Code (csharp):
    1. CoinIn.GetComponent<Rigidbody> ().velocity = transform.up * CoinJump;
    all appears to be working as i wanted now :)
     
    Last edited: May 1, 2015