I'm making a 2D Unity game, in which you can move over 3 lanes and fire arrows that only affects their specific lane.
In order to do that I'm changing the layer of the arrow prefab every time you move from one lane to another.
Arrows change to children of the terrain on hit, but when the arrow prefab change his layer, clone arrows do the same(even the ones that are parented to terrain in other lane)
How can I make arrows that are parented to terrain not to be affected by the arrow prefab change of layer?
Thanks
You say you're changing the prefab layer, if that's correct, you're basically changing every clone made from that prefab. If you want to change each lane's arrows by themselves, you should change the clones directly, not the prefab.
Maybe you could add 3 lists of arrows, one for each lane, and each time you fire an arrow, have it added to its list. Then you can iterate over that list changing every GameObject layer.
Is your arrow have a class? Add property on it like .laneNumber. Initialize the value of it when instantiating depends on the lane you're in. For example in first lane. .laneNumber = 0; And in collision method check if laneNumber is equal to the monster/obstacle laneNumber. Something like that.
Related
I have a 3d building model in my Unity project. It has many children like doors, walls etc. The problem is, all of the children points to same position in the Unity world (24.97, -2.08, 19.35). Their transforms show this position. And this position is far away from their actual one. How can i fix this?
I tried freeing all children from parent but this didn't change anything.
I want them to show their real position, which appears with move tool when we click upon them.
Here is the image link
It seems that this is simply their pivot point exported "wrongly" from the 3D editor your model was made with.
This won't change until you export it correctly from a 3D editor (Blender, Maya, etc).
Unity is not made for 3D mesh modeling and therefore in Unity itself you can't change the pivot points.
There is a very simple fix
Add a new empty GameObject
In the Inspector go to the Transform component, click on the context menu and hit Reset (you also simply set it to position 0,0,0 rotation 0,0,0 and scale 1,1,1) assuming the pivot should be at 0,0,0
Now drag and drop all objects into the empty GameObject
=> You have now one parent object with correct pivot.
By wrapping it in a parent object the childrens pivots don't matter anymore. You can simply do all translation, rotation and scaling on the parent object with the correct pivot and don't have to care about that "wrong" position at all.
In my game I have 2 objects. I want them to be able to collide and not go through each other. Currently Collision is working, but when one object pushes the other, he other starts floating away. I don't want that. How can I get collisions(not going through each other) without the physics(floating away, pushing, etc)?
The component that makes the gameobject react to external forces applied on it is the Rigidbody
-You can configure constraints on your rigidbody so the passive physics (forces coming outside the object, like gravity and collisions) won't work in the axis you block. Only active forces will (like AddForce() method)
Ridigbody Component in Editor with all constraints enabled
-In static objects (like walls, a tree) you can remove the rigidbody components. It will enhance performance too. Only use rigidbody in dynamic objects like characters, vehicles, balls, bullets
-Between A and B object, at least one of them must have rigidbody, or the collision detection won't be possible (in the object which contains the method OnCollisionEnter (or Stay)
But be careful. Without the rigidbody, you won't be able to use the AddForce() method to move object. If you use simply the Translate method on the Transform, the collision detection will become a such unaccurate
I am making a chemistry game in Unity where each atom is represented as a 3D GameObject sphere. I want the user to be able to drag an atom around and if it enters some set radius of another item that it can combine with, the two atoms "stick" together (or become "suctioned" together) and become one entity that can be dragged around and interacted with.
How it looks now:
How I want it to look after interaction:
Now comes the hard part trying to figure out how to do this. First, I attached a script to the Oxygen atom (red sphere). I want to see if the Hydrogen atom entered some radius. If it does, I create a new GameObject, make both the oxygen and hydrogen children of that object, give the new GameObject a rigidbody and collider, and turn off the rigidbodies of the children. So the problem with this is that once I do that, both spheres fall through the ground. Also, they still remain as separate spheres and don't look "stuck" together. If I drag the red sphere around, the white doesn't follow and vice versa.
Any help would be greatly appreciated.
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.name == "HydrogenPrefab(Clone)")
{
Debug.Log("Hydrogen entered");
GameObject HOCombo = new GameObject();
//put both atoms under the same parent
collision.gameObject.transform.parent = HOCombo.transform;
this.transform.parent = HOCombo.transform;
//remove the rigidbodies
this.GetComponent<Rigidbody>().isKinematic = true;
collision.gameObject.GetComponent<Rigidbody>().isKinematic = true;
// make the parent a rigidbody;
HOCombo.AddComponent<Rigidbody>();
HOCombo.AddComponent<MeshCollider>();
}
}
It looks like a Fixed Joint could suits you.
Also make sure that you get the right object when you Raycast it. You'll probably need to move the children to a layer ignored by your raycast or use RaycastAll to get all hits (not just the first one) and manage to find the desired parent from there.
I am starting with Unity 5 and I am straggling a bit with its 2D collisions. Looking a bit into it I found that there are three types of objects that can be defined:
- Static: Just a 2D collider (2D collision box component for example).
- Dynamic: 2D collider + 2D rigid body.
- Kinematic: 2D collider + 2D rigid body set to kinematic.
And as far as I know they collide this way:
Static: Only collides with dynamics.
Dynamic: Only collides with statics and kinematics.
Kinematic: Only collides with dynamics.
I am trying to make a simple Space Invaders and I am struggling to define the collision types of the different elements (aliens, player, alien bullets and player bullets).
I imagine I can set objects to dynamic and disable the gravity to match the correct collision types.
But my question is, for example I want to make a simple game with a few enemies of the same type (instantiating a prefab), and I want those enemies to detect collision with each other. How I am supposed to setup the enemy collision properties to achieve that?
Many thanks in advance!
Personally I would set the enemies in the same layer with each other, and make sure they are colliding with each other. You can set the layer of the prefab in the inspector, where it is located underneath the name, as you can see here:
Once you click on the layer, there is an option to add new layers as well, at the complete bottom.
Then I would set the Layer Collision Matrix through menu Edit - Project Settings - Physics 2D to match the layers that need or need not to collide with each other, like so:
In this example the Enemy layer won't collide with any other layer than itself.
I have a parent object, which has four children cubes. The parent has rigidbody, no collider. The four cubes only have colliders, which trigger tags are set true. I want to handle collision in the parent object. However, it didn't work. I know use onCollisionEnter may work, but I must use trigger. Could anybody give me some ideas?
Trigger colliders are unable to use OnColliderEnter because things pass through them instead of colliding. If you wish to detect when an object enters a trigger collider, use OnTriggerEnter instead.
https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
If however you wish to keep the trigger colliders and make them collide only with specific kind of objects, you can add a second set of colliders that are not triggers and use layers to differentiate which objects can collide with them and which ones pass through. You can then go to Edit->Project Settings->Physics and at the bottom at Layer Collision Matrix configure which layers can collide with each other.
https://docs.unity3d.com/Manual/LayerBasedCollision.html