I'm trying to create a system when my player gets hit the player ignores all enemy collisions for a brief moment so the player could have a chance to escape. Then when the time period is over he can get hit again. The problem is that when the player gets hit by the enemy, the same enemy that hit him can never touch him again other enemies can, but once they do the can never touch him again either. I have been trying my best to fix this please help me; heres the code.
if (ghostmode && coll.gameObject.tag == "charger" ) {
Physics2D.IgnoreCollision (gameObject.GetComponent (),coll.collider);
}
if(ghostmode = false){
Physics2D.IgnoreCollision (gameObject.GetComponent (), coll.collider,false);
}
↧
ignoreing enemy collisions when hit, and turning the coliders back on
↧
UNET: How to ignore collisions
Ok so I am instantiating a prefab, which works fine on both the host and all clients. Now my issue is as soon as I add a BoxCollider2D the instantiated prefab will right away collide with the player.
In order to solve that I use `Physics2D.IgnoreCollision` - however this only works on the host, not on any other clients since it's a Command.
My code is as followed:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class player_projectile : NetworkBehaviour
{
public GameObject projectile;
public float ProjectileSpeed = 5f;
GameObject square;
[Client]
void Update()
{
if (Input.GetMouseButtonDown(0))
{
GetComponent().Play ();
Vector2 direction = (Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z +10)) - transform.position).normalized;
CmdShootProjectile(direction, ProjectileSpeed);
}
}
[Command]
void CmdShootProjectile(Vector2 direction, float speed)
{
square = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
NetworkServer.Spawn(square);
Physics2D.IgnoreCollision(gameObject.GetComponent(), square.GetComponent());
square.GetComponent().velocity = direction * speed;
Destroy(square, 2);
}
}
As you can see I have `Physics2D.IgnoreCollision` under `CmdShootProjectile` but this only makes it work for the host. How can I also make it work for every other client connected so they don't collide with their own projectile?
↧
↧
projectile must ignore collision with itself
i have this scirpt
var projectile : Rigidbody;
var speed = 100;
var barrel : Transform;
var shots : int = 0 ;
var maxShots : int = 1 ;
function Update () {
fwd = transform.TransformDirection(Vector3.forward);
if ( Input.GetButtonDown ("Fire1") && shots < maxShots){
Shoot();
}
else if (shots >= maxShots)
{
Reload();
}
}
function Shoot(){
var bullet1 : Rigidbody;
Physics.IgnoreLayerCollision(8,8, true);
bullet1 = Instantiate(projectile, barrel.position, barrel.rotation) as Rigidbody;
bullet1.GetComponent(Rigidbody).useGravity = true;
bullet1.GetComponent(Rigidbody).AddForce(barrel.forward * speed, ForceMode.Impulse);
shots ++;
}
function Reload() {
yield WaitForSeconds(0.5);
shots = 0;
}
which will instantiate a projectile and fire it while ignoring it' s own collsion
this is because my projectile will spawn inside itself in the game
if someone is going to say that i could just move the spawn out of the projectile my answer is
i can't do that because of a animation i have for throwing the object (this animation is not in the script just yet ill implement it later)
so i used a ignorelayercollision to prevent it from colliding with himself
but whe i use it, it gives this error and wont spawn the clones
NullReferenceException: Object reference not set to an instance of an object
StoneFire.Shoot () (at Assets/MY Assets/Scripts/StoneFire.js:21)
StoneFire.Update () (at Assets/MY Assets/Scripts/StoneFire.js:11)
i have this same error on line 18 too but that one does not mess up my in game actions so i wont fix that now (if someone knows how to fix it you can say that too)
can someone please tell me why my projectile won't spawn and give that error
projectile information:
it is a rigidbody
is kanematic is off
use gravity is off in editor but script enables it
uses sphere collider and not a mesh collider because then i had to put is kanematic on and that would lead to a not moving bullet.
↧
IgnoreCollision not working
Is there anything I'm missing? Anything in the unity editor I need to check/uncheck? This just won't work for me.
↧
Can't get Physics2D.IgnoreCollision to work on multiple objects
I'm making a 2D side scroller and the player character has a dashing action. I'd also like to have this dashing action ignore the colliders on specific objects, so the player can pass through them.
For a while I was simply disabling the player collider while dashing for prototyping purposes but I obviously can't keep it this way because the player can pass right through the scenery and fall off the game. So this is what I tried to do to create my own ignore collision mask kind of thing:
private Collider2D col;
public Collider2D[] dashMasks;
void Awake()
{
col = GetComponent();
dashMasks = GetComponents();
}
void Update()
{
if(dashing)
{
foreach(Collider2D mask in dashMasks)
{
Physics2D.IgnoreCollision(col, mask);
}
}
No errors but it just doesn't work. Player will still collide with whatever objects I put in the inspector field. What did I do wrong?
↧
↧
Physics.IgnoreCollision between rigidebody2d+polygone collider 2d and edge collider 2d doesn't work?
Hi, I'm new at Unity. And I try to ignore collision between my gamer(rigidebody+polygone collider 2d) and a simple edge collider 2d. I've try to put this code in my gamer and the bullet is the edge collider. I've try in the other side too but it's doesn't work... do you know what I've doing wrong?
public class ExampleClass : MonoBehaviour {
public Transform bulletPrefab;
void Start() {
Transform bullet = Instantiate(bulletPrefab) as Transform;
Physics.IgnoreCollision(bullet.GetComponent(), GetComponent());
}
}
↧
No errors console but Physics2D.IgnoreCollision does not work
Hi,
I have no errors on my console but it doesn't ignore collision... I don't understand why. There is my code:
public class gamer : MonoBehaviour
{
Rigidbody2D body;
public Transform bulletPrefab;
void Start ()
{
Transform bullet = Instantiate(bulletPrefab) as Transform;
Physics2D.IgnoreCollision(bullet.GetComponent(), GetComponent());
body = GetComponent();
}
}
Any help will be welcome :)))
Thanks
↧
Object not passing through a collider properly
I'm doing a game where the player switches between layers and the level changes depending on the current layer. The game hides everything that isn't in the same layer as the player.
Here is the problem: Because the layers have objects that need to collide with each other I can't disable the colliders when the player is in another layer.
I've tried using both kinds of collision ignore but neither work. When the player isn't in a matching layer collisions still happen. Although if the player is standing on a ignored collider, the collider acts like quicksand. The player will go through if you walk back and forth on the collider.
The player has a Rigidbody and a CapsuleCollider and the ignored objects have BoxColliders either in parent or a child. and some of them have rigidbodies also. CollisionIgnore doesn't work with any kind of object.
↧
How do I call Physics.IgnoreCollision properly in c# / Physics layer is not being calling the collider
I have sphere collider on two gameobject and through when they get near each and the player. I went to project setting and uncheck the physics manager box that still didn't help and I also made script and attach to the enemy ai. Here is my script :
using UnityEngine;
using System.Collections;
public class dd : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Physics.IgnoreCollision(gameObject.collider, anotherGameObject.collider);
Physics.IgnoreLayerCollision(1, 2);
}
}
↧
↧
Trigger with object only from top side.
Hello.
I have a little problem with detect collision (or trigger) only from top side of the object. I will try to explain what I mean using picture.
![alt text][1]
When I detect collision between objects 1 and 2 they should merge. So I made child object in objects 1 and 2 with box collider (red circles). The problem is if I moved object 1 from the bottom of object 2 they collide too and merge (that shouldn't happend). I will try to show you it on second picture.
![alt text][2]
So I tried to make another collider in 2nd object (under the existing one) and use Physics.IgnoreCollision when they collide, but it didn't worked. This is my code so far:
void OnTriggerEnter(Collider c) {
if(c.GetComponent().name == mergeWith.transform.GetChild(0).gameObject.name) {
newPosition = gameObject.transform.position;
newRotation = gameObject.transform.rotation;
Destroy (c.gameObject.transform.parent.gameObject);
Destroy (gameObject.transform.parent.gameObject);
Instantiate (mergedObject, newPosition, newRotation);
}
}
void OnTriggerStay(Collider c) {
if(c.GetComponent().name == "TriggerToIgnore") {
Physics.IgnoreCollision (gameObject.GetComponent(), c, true);
}
}
void OnTriggerExit(Collider c) {
if(c.GetComponent().name == "TriggerToIgnore") {
Physics.IgnoreCollision (gameObject.GetComponent(), c, false);
}
}
As I see the problem is that program is calling OnTriggerExit all the time, so IgnoreCollision is setting to false. Is there any better way to get that (detect collision only form specific side)?
[1]: /storage/temp/76501-example.png
[2]: /storage/temp/76502-example-2.png
↧
Aim ignoring script
As i said before, i am making an Aim System and i am getting humiliated here because my knowledge about C# is poor and i can't do this in Javascript apparently.
So here is my Script:
using UnityEngine;
using System.Collections;
public class AimSys : MonoBehaviour
{
bool Click = false; //This is a trigger.
bool target = false;
public float fireRate = 0.5F;
private float nextFire = 0.0F;
public GameObject Flash;
void Update()
{
if (target = true)
nextFire -= Time.deltaTime;
if (Input.GetButton("Fire1") && nextFire <= 0)
{
Flash.SetActive(true);
Debug.Log("Target Hit!");
nextFire = fireRate;
}
}
void OnCollisionEnter(Collision collision)
{
{
if (collision.gameObject.tag == "Enemy")
target = true;
}
}
void OnCollisionExit(Collision collision)
{
{
if (collision.gameObject.tag == "Enemy")
target = false;
Flash.SetActive(false);
}
}
}
This was supposed to do the following: There is a cillinder in front of my player's weapon that is an Aim. In every frame the Aim will try to detect if there is something colliding with it. If there is something colliding with the Aim, it will search the object for the tag "Enemy". If it has the tag "Enemy", then for now a simple Log will show. Later i plan to replace the log by a small life reduce in the Enemy life bar.
The problem is that my Aim is detecting EVERYTHING as an Enemy somehow. It doesn't ignore my fire rate, so it properly shoots every two seconds if i am pressing the mouse button (Auto-Fire). I think there is something wrong with the Update Function. I can shoot everywhere, no matter if i am hitting a collider or not, and the log will be shown. It seems that the collision is being ignored but still the log is being displaced. Also the flash that i am using only disappears when i hit something else.
Any idea to how to solve this? I must be missing something basic here, but as i said i don't know much about C# yet.
![alt text][1]
[1]: /storage/temp/81696-airshoot-min.png
↧
Boolean and IgnoreCollision not working correctly
Heya, I'm trying to figure out why a certain bool isn't working right. What I'm trying to do is that if `bounceEnabled` is true (the instance starts as true), the player can bounce, and then it toggles to false permanently. So here's what I'm expecting should work fine; `eutaFollow` is another script, and `eutaCollected` is a boolean that helps count +1 to the score:
void Bounce()
{
objectInRange = dr2dcollider.collisionUp;
if (objectInRange && bounceEnabled)
{
RaycastHit2D hit = new RaycastHit2D();
hit = Physics2D.Raycast(transform.position, Vector2.up, 1.0f, whatIsPartial);
if (hit.collider != null && hit.collider.tag == "Player")
{
hit.collider.gameObject.transform.GetComponent().Bounce();
Physics2D.IgnoreCollision(playerPrefab.GetComponent(), GetComponent());
//References another script so that the object follows the player.
eutaFollow.followTarget = hit.collider.gameObject.transform;
eutaFollow.LateUpdate();
bounceEnabled = false;
eutaCollected = true;
}
}
}
So it seems like that should work fine, but instead the function is never called with the bool in there. If `bounceEnabled` is taken out, the player can bounce on the object at any time even when `bounceEnabled` is false.
The other issue I can't figure out is that IgnoreCollision doesn't work, and I've been working with it for a few hours and still can't figure out why. :/ Any help is appreciated. Thank you.
↧
Physics2D.IgnoreCollision not triggering properly
I have a code where I want to be able to jump from under a platform above the platform and be able to land on top of it. My trigger is a little lower and wider than the collider to allow me to do this properly.
void OnTriggerEnter2D(Collider2D other) {
if (other.gameObject.name == "Player")
{
Debug.Log("Platform Disabled");
Physics2D.IgnoreCollision(platformCollider, playerCollider, true);
}
} void OnTriggerExit2D(Collider2D other) {
if (other.gameObject.name == "Player")
{
Debug.Log("Platform Enabled");
Physics2D.IgnoreCollision(platformCollider, playerCollider, false);
}
}
In this current configuration, the platform collider will be disabled, but it will not be re-enabled, so I can jump through it, but I won't land back on it (At the start, I am able to land on it, so the collider does work). The Debug.Log outputs for both so I know it is triggering properly. The weird glitch is if I switch the true and false values around, and I land on top of the platform, it will rapidly vibrate between the trigger and collider, enabling and disabling the platform rapidly as the character enters and exits the trigger. So basically, the first Physics2D.IgnoreCollision can turn the collider on and off, and the second Physics2D.IgnoreCollision is only able to turn it off, not back on.
↧
↧
Check if two objects are ignoring collision with each other
After using Physics2D.IgnoreCollision(collider1, collider2), is there a way to check whether those same two objects are ignoring collision with one another?
↧
How to ignore collisions while using collider trigger ?
i am working on 2d platform stealth game. My enemy detect me by collider2d, is mean if player go in to the circle collider 2d (trigger) my detect system is going to work. So the problem is if i use Physics.IgnoreLayerCollision function to take my player go through an enemy , my detect system will not work. I hope someone can give me a solution for this problem
Many thanks and sorry for my pool english.
↧
Physics2D.IgnoreLayerCollision not working?
Hi guys. So I already did a pretty intensive google search and I couldn't really find anything of good help, so here I am asking you guys if you could help me out a little. I'm also pretty new to this, so if it's a really simple solution, please forgive my lack of knowledge.
So what I have is a game made using Unity's 2D feature. My game is isometric, and due to it, I have to use polygon colliders as my go-to choice of collider. Now, my main character has a dodge roll feature, where if I press a button, he rolls in a direction. What I want, is for him to ignore collisions with enemies in order to roll past them if he needs to. So I set my player to a "Player" layer, and my enemies to an "Enemy" layer, and I use Physics2D.IgnoreLayerCollision(8, 9, true) when the button is pressed to roll. And I then set it to false when he stops rolling.
Now so far, it works fine if my player is already rolling before he touches the enemy, but if he's near the enemy, or already touching him, and then begins a roll, then IgnoreLayerCollision seems to run, but my player still pushes the enemy as if he isn't ignoring it. Anyone know why this happens?
↧
Layer Collision Matrix, Don't Ignore Triggers
I'm currently working on a 2D game, and I was wondering if there is anyway I can use the Layer Collision Matrix to disable collisions between layers, but not ignore triggers?
![alt text][1]
[1]: https://puu.sh/vMjPn/66d787f5a5.png
Right now, I need the Player layer and the Item layer to ignore collisions, that way the player doesn't push items around when he touches them before he picks them up. However, I need the player to be able to pick up items, so it would be excellent if I could disable collisions, but not the triggers.
↧
↧
Make one object able to go through an object. But the other unable too?
Hello! I have a vent cover like object that has a collider. I want the player to be able to get through the collider. But make the enemys unable to. How do i do this?
↧
Bullets colliding with Character Controller
Hi,
I'm not even sure the title is the issue but that's my best guess so far.
I have a first person shooter where I'm using a character controller for my player and I can shoot bullets from the gun which have a SphereCollider set to Trigger.
When I jump, if I shoot down, there a weird physics behavior which I can't understand. Here's a video [demonstration][1].
I tried these things already to no avail:
- disabling all the layer flags in physics so that player and bullets don't collide
- using Physics.IgnoreCollisions (Player's character controller, bullets sphere collider)
- I tried printing CharacterController.OnControllerColliderHit to the console, it shows floor, walls, etc. but bullets don't show up for some reason.
If I disable the sphere collider on bullets, then the behavior disappears, which means those sphere colliders (set to trigger) collide with something (I suspect my character controller) but I don't know how to disable this collision.
Help is appreciated!
[1]: https://www.youtube.com/watch?v=wxuJ9qOJfeU&feature=youtu.be
↧
making Navmesh Agents able to walk through each other?
im needing the Agents to not only ignore each other, but to walk through each other without altering each other's transform. a good example would be something like an mmo. everyone can walk through everyone including NPCs. i've tried making the radius very small, while putting obstacle avoidance to none, but that doesnt seem to do what i am looking for. in fact it still rotates and moves my NPCs when trying to walk through them.
Q1: Is there a way to do this without digging through the NavMeshAgent code?
Q2: if no to Q1, is there a way to change the NavmeshAgent code without purchasing the Unity Source Code? (ive not found any files for it as of yet)
Bump.
↧