Merge colliders of different gameobjects - c#

I'm trying to create an auto spawned of game object out of predefined prefab.
The prefab contains a polygon collider.
The spawned instantiates the new object right at the end of the previous one, in a way that it looks like a single piece.
I'm trying to understand how to merge both of their colliders to be like a single polygon collider defined on the merged object.
Thank you for helping.
Btw if there's a code involved (which I'm sure there is), I would be glad if you could write it in c#.
Thank you!!

So from reading your Q and information provided i would say that you want one polygon collider to be exactly on the other. If this is your goal, when they collide, you can us the transform function and move it to the exact position of the other.

Related

Instantiate prefabs on endless runner top of previous one

I am developing 2D Endless racer game, insted of constant background; i will use prefabs as background, i have 3 different prefabs as background: (Please Check Image)
Straight Road
Right Curved Road
Left Curved Road
I want to instantiate endless prefabs, according to instantiate point of each prefab,
I would appreciate for your support
Goal Image and Prefabs
One simple solution come straight from the example image you posted. Each road piece has its own "exit point", symbolized by that yellow circle. Since each road piece is its own prefab, you could give each of them an empty child object, acting like your yellow circle.
Each object would then have a reference to that object, and would use that to spawn the next object.
public class RoadPiece : Monobehaviour
{
public Transform nextInstantiatePoint;
To make the next road piece line up correctly, you could take advantage of the Sprite's Import Settings. Set the Pivot to "Bottom", or try the others for different effects.
The algorithm that you use to chose which piece to instantiate each time is up to you, but to start you could have an array of all the road pieces and pick one from there.
Ideally you want each object to wait some time before it instantiates the next, otherwise it will quickly cause the scene to be overcrowded. The easiest way to do it is like this:
void Start()
{
Invoke(nameof(SpawnNext), waitTime);
}
void SpawnNext()
{
Instantiate(roadPiece, nextInstantiatePoint.position, nextInstantiatePoint.rotation);
}
}

Objects having same distance/radius from the center (camera) in Unity 3D

It's a 360 Video application on Unity 3D.
I want to place several objects around the camera (which has a fixed position), but I need this objects to have the same distance (same radius) from the Camera (which is the center). How can I do this? Either on Editor or by code.
I have been manually displacing objects around the camera, by dragging them by arrow tool. But it's as inaccurate as a pain to do. :)
Any light on this would help me a lot! Not only me, but anyone working with 360 videos in Unity.
Thank you all in advance!
To solve your problem, an easy solution would be to add a child "child_of_camera" to your camera and then add a child "child_of_child" to the "child_of_camera".
Now that you've done this, move the "child_of_child" away to however far from the camera you'd like it to be. After this, apply the random rotation you'd like to "child_of_camera".
Duplicate "child_of_camera" to however many objects you'd like on screen and then rotate them all to your preference.
Now, when you're moving around the camera, these children will follow along with the camera.
If you're looking so that the rotation of the camera doesn't affect the objects there are two ways you can handle this:
Place camera and "child_of_camera" (this name would now be misleading and should be renamed) under an empty GameObject and move "empty_GO" on the X,Y,Z axis instead of the camera.
or
Create a quick script to attach onto "child_of_camera" so that it always sets the "child_of_camera"s world space rotation to Vector3.zero.
As I stated in the comments, this solution is most likely not the optimal way to fix your problem but it is definitely a very simple solution that is easy to understand and implement. I hope it helps.

My character associated to Image Target barely moves

I'm developing an android augmented reality game using unity 3d engine and vuforia extension where I need to move a character over a image target.
The problem is when I associate the character to the image target (as a child of image target) the movement is like the character is "glued" to the plane, it barely moves from his position, it moves very slowly.
I already tested without using augmented reality and the moving of the character is completly fine, so I dont know what am I doing wrong ...
Thanks in advance.
There's no need for the object to compulsorily be a child of the ImageTarget. AR will work both ways.
Wherever you place your Object, it will calculate the relative distance of the ImageTarget and place the Object there automatically. So if your Object is on the Image Target(not a child) It will show the object on top, as it is. Just make sure you don't make the AR Camera a parent/child of any other object.
As for the object following the object, you can simply use the Tracking functions to Enable and Disable tracking, so the object disappears
I really hope this solves your problem.
I think I know what it is! Check your Inspector Panel to see if you've got a Rigidbody Element. They're usually very difficult to handle, try unchecking the Rigidbody element and see if it's solves anything.

Unity 2D line collider

I try to do the following: I have a point A at -4x-4y and a point B at 4x 0y. I want to make a colliding line from position A to position B.
I tried to do it with a linerenderer but I can't get the line to collide with my other 2d objects.
My other tought was to calculate the center of the points and the rotation and do it with a box collider but that seems to be really complicated and hacky.
Is there a simple way to achieve this?
Thanks in advance
You can use PolygonCollider2D, it's automatically create collider for sprites, and if you are not satisfied results, you can edit it by clicking Edit Collider in inspector, or trought Unity's API.
I think you must have a Rigidbody2D attached with your other 2D object. Then this will work 100%. You can use any collider it doesn't matter.

How to determine if there is an object lying on the Plane at particular coordinates?

I am implementing A* algorithm in 3D environment and I have came to a point where I need to determine whether there is something lying on a plane (on which my characters will be walking) at particular coordinates.
I have created a class Board which holds the map of of Nodes (each holds center of its coordinates). So we can say I have discretized the Plane to something similar like a chessborad. Now I need to know whether there is something on each Node to create a walkable/unwalkable map on this plane.
How can I do this in Unity3D ? Raycasting ?
EDIT
There is one thing that I can think of but I think it's a bit inefficient :
Create a temporary collider ( take area of Board's tile and some height ) and check whether there is something colliding with it and then keep translating it for every tile in the Board.
Do you think this would be a good way ?
You could use a raycast (Physics.Raycast) from each node coordinate. Make sure objects you are checking for have colliders. This will only check for a single point, however, not for the entire area of the node.
To check for an area, not just a point, above each node, you can use a sphere or capsule cast or check. See the choices in the list of class functions for Physics.
Another approach is to have a game object with an appropriately shaped trigger collider on each node in your scene. You could keep track of how many other objects (with colliders) are on each node by incrementing and decrementing a counter in the OnTriggerEnter and OnTriggerExit methods of a script (i.e. a MonoBehaviour subclass).
Thanks to Ghopper21's answer I came up with ShpereCasting each Node from above it e.g. Node (0,0,0) would be Spherecasted from (0,100,0).

Categories

Resources