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
Instantiate(Coin,transform.position, transform.rotation );
rb.AddForce (new Vector3(0,CoinJump ,0), ForceMode.Acceleration) ; // this method causes coins to jump out of the side of container box
Coin.transform.position = Coin.transform.up * CoinJump; // These two methods appear to do nothing.
Coin.transform.Translate (new Vector3(0.0f, 1.0f, 0.0f), Space.World);
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.
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.
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?
@Simpso_1 , as Ryiah pointed out, AddForce should be using world coordinates. Maybe you could post a few screenshots of your scene hierarchy, Inspector window, etc…
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:
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…
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.
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.
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?
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.
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.
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.