I have a player (sphere) and 4 enemies (capsules). All enemies have a character controller added. They also all have a child GameObject, which has a RigidBody (no mass, no gravity) and a Box Collider attached. The Box Collider is large enough so you can see the sphere model inside of the box, even with a little space in between.
The player also has a character controller, but no RigidBody. It does have a sphere attached with a mesh collider.
All of the enemies have the IsTrigger box checked. Why don't they call the OnTriggerEnter() function? Note that when enemies collide, OnTriggerEnter() is called. But it doesn't work on the player.
Is the sphere a child GameObject to the player, or is it the GameObject itself? You might want to check where the script is attached. If the script is attached to the player but the collider is on a child GameObject, that isn't quite going to work. Collider and script need to be on the same GameObject.
Related
I want to Instantiate my prefab on top of my cube gameobject when I press E on my cube gameobject. I know how to instantiate and get the
position of the cube:
GameObject cube;
GameObject prefab;
// Function>>
//blahblah
Instantiate(prefab, cube.transform.position, cube.transform.rotation);
But I have no idea how to check if the player pressed E while looking at the cube and then make the prefab spawn on top of the cube.
I know Input.GetKeyDown(KeyCode.E) checks if the key is pressed but how can I check if its pressed while looking at the box?
A way of checking if the player is looking at the cube is using raycasts. A raycast is basically an imaginary line that originates from a given point and goes towards a direction - it then tells you which objects it hit and what order.
To check if the player is looking at the cube, perform a raycast that originates from the player's head and goes towards the direction the player is facing, if the first object it his is the cube - then it means the player is looking at the cube.
This is the API for creating physics-based raycasts: https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
Hey so i am making a 2d game that is very similar to flappy bird (you jump when tapping the screen). The point of the game is to catch the green squares coming to you.
The problem i am encountering is that when my player hits the green square it seems to collide with it (it throws the player in some random direction or even stop into place like it hit a wall) even if on collision the green square is being destroyed.
So whenever the player hits the green square prefab it increments the score and destroys the square but sometimes when i jump right before hitting the square it stops like it collided with it insted of just passing right through.
The player prefab has got a rigidbody component and a 2d square collider.
The green square prefab has got only a 2d square collider component attached to it.
I am very new to unity and this is the first game i started developing.
If you need any further information please don't hesitate to ask.
Here is the player's rigidbody and 2d collider settings
Here is the green square's 2d collider settings
Maybe this picture of the gameplay can help. Imagine the blue square is the player and the green squares are the points you have to hit the red things are just the enemies that kill you so don't mind them. The player is standing still and the green square prefab have a script attached to it that adds a constant force to them. Hopefully this made it a lot clearer.
There is a really useful unity function that will detect when a collider touches another collider.
It is: void OnCollisionEnter(...)
You also have to pass in a Collider or Collider2d. So it will look like:
void OnCollisionEnter(Collider col)
or void OnCollisionEnter(Collider2D col)
Of course you have to have the necessary import statements for unity engine at the top as well.
You may want to use a trigger on your green Squares. then the bird is allowed to pass true but the square will activate your OnTriggerEnter code in your script.
so what you can try is:
public void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
//Run your code here!!!
//for example
Rigidbody rb = other.gameObject.GetComponent<Rigidbody>();
rb.AddForce(new Vector3(1, 1, 1));
}
}
this will add a force to your player/bird. but you can run any code in that method.
OnTriggerEnter and OnCollisionEnter run both when the object is hit by a rigidbody. but with OntriggerEnter you can pass thru the object. because it does not get effected by other physics objects. but onCollision usually does unless you freeze it in position.
side note OnTriggerEnter runs in FixedUpdate, so it is run at the end of each frame
I could really need some help with my GameObjects.
I am working on a game in which I want a pick-up Item to create a Physics Force explosion to blow away the enemies. I made a simple Bomb-Object to test this idea. I added a straightforward code, using a loop to collect all the colliders within its radius, to then addForce to these colliders. Now the code is working properly, but not all my GameObjects are reacting properly.
I looked into why this is, and I noticed that my Collider keeps falling away from the GameObject as soon as I add a RigidBody component to the object. The RigidBody is needed for the AddForce impact. When I remove the rb, the collider stays in place, but the object does not react to the Physics Force.
I added some images to illustrate what I mean:
Image of the Collider of the leaf sinking away..
Example image of objects which DO react to the AddForce.
I already tried to:
Copy/Paste all component settings from the reacting Cube and stone
Gameobjects to the Leaf Gameobject while disabling all other code
such as the c# scripts. The Collider & RB do not fall through the
floor but when Physics Force hits the Collider is blown away while
the GameObject keeps its position.
Try different GameObjects/Collider types.
Removed the water.
Play with amount of force/radius of the bomb.
Edited 'Tag' and 'Layerstyle' of GameObject.
The 'Explosion Code' is added below, however, I don't think the problem is in the code. I added the code to the Main Camera of my scene, and added a simple sphere as the 'bomb' GameObject.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Explosion : MonoBehaviour
{
public GameObject bomb; //set the position of the explosion
public float power = 10.0f;
public float radius = 10.0f;
public float upForce = 0.0f;
private void FixedUpdate()
{
if (Input.GetKeyDown("space"))
{
print("space key was pressed");
Invoke("Detonate", 1);
}
}
void Detonate()
{
Vector3 explosionPosition = bomb.transform.position; //set the position of our explosion to the position of the bomb.
Collider[] colliders = Physics.OverlapSphere(explosionPosition, radius); //collects all colliders within the radius.
foreach (Collider hit in colliders) { //for each individual collider the following code is ran.
Rigidbody rb = hit.GetComponent<Rigidbody>(); //declare'rb' rigidbody. Get rb component from each collider
if (rb != null)
{
print("BOOM!");
rb.AddExplosionForce(power, explosionPosition, radius, upForce, ForceMode.Impulse); //add force to each collider
}
}
}
}
How do I make the Rigidbody, Collider and GameObject of the leaf hold onto each other like the standard 3D object 'cube', so that I can make these blow away with the Physics Force just like the other models?
Thank you for your time, I have been trying things and looking around on the Internet for hours now but can't seem to find any solution.
What happens if you add the rigidbody in stop mode and press play? Does it move away in a similar manner? This may, and is expected to happen, if your colliders intersect with each other. As soon as you add a rigidbody, they find themselves trapped in a serious collision.
If you don't want to modify the scene, you can fiddle with the collision matrix in project settings / physics
Currently I am working on a networked 2d platform game.
My player is an empty object which serves as a parent to my actual graphics etc. The empty object has a rigidbody (3d) attached to it as it needs to have one in order to use the configurable joint component ( note: I move my player around using this 3d rigid body).The player body however, a child to the graphics, has a box collider 2d attached to it as it after all it is a 2d game and has to collide with other 2d objects.
My platforms have polygon collider 2ds attached to them (which is connected to a platform effector if that is worth mentioning).
Now, even though both my player body (which is a child to graphics which is a child to my player object) and my platforms have 2d colliders on them they do not collide and can simply go inside each other (neither of them is marked as isTrigger).
In order to solve this I thought I would add a rigidbody 2D to the player body and see if that would do anything. Now upon adding the rigidbody 2D collisions did work but as soon as I made the rigidbody 2D have all position and rotation constraints ticked or as soon as I made it kinematic or static it would cease to collide with my platforms. The problem is I need to have the rigidbody 2D be static or not be able to move as I am currently moving my player object using the rigidbody (3d) attached to it and do not want any additional movement of the player body upon colliding with objects.
I know this is quite a lot of information, so if you have any questions or would like further information just comment and I will be quick to respond. Thank you :-)!
Edit:
2d ray casts are also unable to hit the player body box collider 2D
Edit 2:
So to recap:
If either the player body or the platform has a rigidbody 2d that is not static (dynamic) and can move collisions work. However currently I only have a rigidbody 2D on my player body which has to be static though (as explained earlier) as well as a box collider 2d. My platform(s) on the other hand currently only has a polygon collider 2d as I do not see why it would need a rigidbody 2D.
A static collider won't detect the collision with another collider if it doesn't have a rigidbody, if it is static or if it has a kinematic rigidbody. At least one of your two objects needs to not be static and to have a rigidbody which is not kinematic to be correctly detected. So you do need to add a Rigidbody2D to your platform.
When you have a doubt about why a collider wouldn't collide with another one, always refer to this page of Unity's documentation which sums up which kind of collider will collide with another one.
Rigidbody2D can't collide with Rigidbody3D, here's a workaround though
http://answers.unity3d.com/questions/580769/can-rigidbody-2d-collide-with-3d-colliders.html
I have a cube tagged "player", which needs to jump at certain locations, represented by Empty GameObjects.
I want to make a point system and that why I put on some of the places other game objects tagged "Coin"
The cube has an animation which plays each time it moves to another point recognized by clicking with the mouse on it.
When I jump on the coin location I want the Coin object to get destroyed.
I use this function:
void OnTriggerEnter(Collider col){
if (col.gameObject.tag == "Coin") {
Debug.Log ("Plm");
stroy (col.gameObject);
}
The problem is that when I'm jumping at the Coin location nothing happens.
I have box colliders on both objects and OnTrigger checked on both.
1) Make sure one gameobject has a rigidbody
To have the OnTriggerEnter happen you need at least one of the two object to have a RigidBody attached to it.
2) Static Trigger Collider
An object that has a collider and isTrigger checked but no rigibBody is considered a Static Trigger Collider which are From Unity Documentation:
This is a GameObject that has a Collider but no Rigidbody. Static colliders are used for level geometry which always stays at the same place and never moves around. Incoming rigidbody objects will collide with the static collider but will not move it.
This type of collider interactions is perfect for your coins because they do not move and stay static.
3) Rigidbody Trigger Collider
The next type of collider interactions is a Rigidbody Trigger Collider which are :
This is a GameObject with a Collider and a normal, non-kinematic Rigidbody attached. Rigidbody colliders are fully simulated by the physics engine and can react to collisions and forces applied from a script. They can collide with other objects (including static colliders)
This type of collider is what you need on your player and as it says in the Unity Documentation it will be able to collide with the Static trigger collider.
Important observation:
Having IsTrigger set to true on your player collider will cause your player to not respond as a solid object
A collider configured as a Trigger (using the Is Trigger property) does not behave as a solid object and will simply allow other colliders to pass through.
which might not be the most optimal solution for you because most of the time you want your player to be able to collide with other things like the ground. So you can set the isTrigger to false and just have a rigidbody on you player and it will react to the coins if they're set as Static Trigger Collider.
If you want an easy way to know what can collide with what Read the colliders documentation and take a look at this chart that shows it in a more simpler way.