Making an object move through another and keep going? - c#

I am currently making a sidescrolling game in Unity 2D where an enemy rises up, and then dives down to the player, but then keeps going until it is off screen, like how this image shows. 1 I can get the enemy to collide with the player but I can't make it keeping in that direction until off screen. How would I be able to do this?

You have to make your collider to trigger (it's a checkbox on your collider)
That means your object will not be affected by physics when it touch another collider, but it can calls functions.
You have to replace OnCollisionEnter/OnCollisionEnter2D, etc by OnTriggerEnter/OnTriggerEnter2D, etc.
Be careful, the parameter of the function may change too (Collision/Collider).

Related

Sibling game objects not colliding in Unity2D

I have a game in which waves of enemies are spawned by storing an arrangement of enemies as children of an empty game object, which I can then save as a prefab and instantiate with an enemy spawner script, but there is no collision between the enemies, and the only reason I can think of is because of some physics optimization due to them being siblings. These are my collider and rigidbody settings for the enemies: Any idea why this happens? Thanks!
This is likely because your Rigidbody constraints are set to freeze movement on both the X and Y axes:
This means that any movement you would normally expect from simulated physical interactions (for example, changing velocity from colliding with another enemy) will be ignored for this Rigidbody. See the Unity docs for more details on what they do.
The simple fix here would be to uncheck those checkboxes, so that you rely on the physics system to handle these interactions. If you want to fully control them through code though, you could technically write code in OnCollisionEnter() to handle the collision between two enemies - but that would be a much more involved solution.

Unity3D Rigidbodies acts like objects is elastic and falling inside eachother

I was making a game about stacking objects on top of eachother but when I spawn a new object above it falls inside in other object and behaves like elastic material.
I didn't specify any Physics Material to the Rigidbody, It's just a vanilla Rigidbody component.
Here is a gif about my problem
You might have forgotten to add a Box collider to the cube prefab.
Make sure that the collision detection method on the rigidbody is set to either continuous or continuous dynamic. Make sure there's a collider as well.

What is the best way to move an object with the player in Unity C#?

I am trying to get a player pull and push objects in my 3D game. I am mainly focusing on pulling and pushing a box. Now when a player collides with a box and the E key is pressed the isKinematic of the box becomes false, and the player is able to move the box with its own mass.
I am trying to implement a basic pull and push interaction of the object. I have been thinking moving the box along with the movement of the player based on the WASD input but it doesn't sound stable, which road should I take here? Should I use a Fixed Joint connect them? I tried putting the fixed joint the box and making the connected body the player, but now the player is not able to move like there is a collision around it.
What is the best way to implement this? Should I use Fixed Joint or take a different way? Thanks.
if you really want to work with physics, i would say use a rigidbody. Use the rigidbody.AddForce or rigidbody.velocity to move your player. or use the transform by lerping to the position, but this is a bit messy and could not register all of the collisions if you are working on a fast object.

How to make my button change on collision

Hey I am trying to make my sprite button go from green to red and backwards when my player collides with it. I have a boxcollider2d as is trigger, and a script
script : https://gyazo.com/ec64a2bca5b23526c6949bf18cb50a0d
I think this is some of it but I dont get anything at all when colliding with the button can anyone enlighten me on this problem would be very appreciated.
Thanks in advance
It is important to distinguish the difference between Colliders and Triggers.
Colliders are used by the Physics system in order to make it so object do not penetrate or overlap each other. Triggers on the other hand are made so that you can check for overlapping areas, an example is a Ring in Sonic. Sonic collects the rings but they do not stop him from passing, think of these as triggers. If they were colliders sonic would jump into them and bounce off.
In Unity the process for creating a trigger is the same for creating a collider, with 1 additional step, and that is marking a check box.
As such it is also important to know which functions you should use in each case.
OnCollision... For Colliders, (Or Colliders not marked as triggers)
OnTrigger... for Triggers, (Marking a collider in unity as a trigger makes it a trigger.)
To just add a bit more detail, It is also important to use the correct collider types for the correct dimensions of the game, 2d Colliders/Triggers do not interact with 3d Colliders/Triggers. More so physics components: 2d Rigidbodies do not works with 3d colliders, and 3d rigidbodies do not work with 2d colliders/triggers.
As such there are functions for each 2d, 3d, collision, and trigger.
Another important factor is to ensure if you want to use these functions atleast one of the objects have to have a rigidbody of the correct type(2d or 3d).
As such the problem you were running into was using OnCollisionEnter2D for a "collider" that was marked as a trigger, in this case OnCollisionEnter2D is not called, but OnTriggerEnter2D is called.

3rd person controller, allow camera through wall and prevent player

im working for 3rd person fighting game between two characters.
I've setup camera to focus enemy and player in same time like Naruto Ultimate Ninja Storm 4, but when camera collide with wall the view angle will be change and both characters will not appear on screen.
in Naruto Ultimate Ninja Storm 4 there is transparent wall around arena which leave empty space between visible object (wall,rock..) and player but allows camera to pass it.
my problem I don't find method to let the camera pass this transparent wall and stop player to pass it.
I tried to get tag of object in collision and disable collider of object when is camera or vice versa but that allow character also to go tought transparent wall
You should create 3 layers, if you haven't already:
Player
Camera
Wall
Put each GameObject in the corresponding layer, then go to Edit->Project Settings->Physics, scroll to the "Layer Collision Matrix" and uncheck the collisions you don't want to have. In your case, you want the collision between the wall and the player, but you have to uncheck the collision between the camera and the wall.
Also, to make this work correctly, make it so that none of the characters, walls, and camera are separate gameobjects.
This is also very useful to remove any unnecessary collision and boost your game performance.

Categories

Resources