I have a GameObject with a box collider and a Player script attached to it.
I want a Function Like This (CheckAnyRaycastIntersection()):
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField] BoxCollider bxCollider;
void Update()
{
bool i = bxCollider.CheckAnyRaycastIntersection();
print(i); //Prints TRUE if any Raycast intersects with bxCollider
//Otherwise prints FALSE
}
}
This is useful for me when I'm trying to find out if any gun is targeting the Player. So I want the 'Player' script to calculate this.
Check the GIF
I know there are many ways to do it, but can I do it this way?
I don't understand why you want to do this,
If you want to see gameObject colliding with something else, use the spherecast or boxcast.
In this scenario you should explain who is throwing raycast and who should or should not intersect with the ray.
If you can explain more, i can help.
Related
I have an aircraft in my game that is controlled by the player. I want it to get destroyed after it collides with something. The aircraft has many colliders on it, so im using an array. Heres my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AircraftDamage : MonoBehaviour
{
public Collider[] Colliders;
public GameObject Aircraft;
private void OnTriggerEnter(Colliders collision)
{
DestroyAircraft();
}
public void DestroyAircraft()
{
Destroy(Aircraft);
}
}
Im getting the obvious error here, type or namespace 'Colliders' could not be found. How can I check if any collider within my array gets triggered?
a better approach is to use OnCollisionEnter instead of OnTriggerEnter
You are missing the basics of Collision here. You don't need to reference the Colliders using an array.
Both OnCollisionEnter and OnTriggerEnter are called when the gameobject to which the script is attached collides with another collider. They return the collider with which it has collided.
So in your case its better to have a single collider on the parent gameobject rather than separate Colliders.
If you really want to have separate colliders for the airplane's body part then you need to add a collision checking script to each of the gameobject or you can add the collision checking script to your obstacles like building and have all your Airplane parts in a single tag.
Check out this guide on Unity collision
i have a trouble with a collision, i know this is not the way to do "bullets" , but for now im very new and I'm exploring and trying some stuff. I have in my little "bullet" a script with the "onCollisionEnter" function, but when i shoot to make the cube dissapear I have to take 2 shots(two fasts) instead one to that occur, and i do not understand why.
my code says:
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "Cube")
{
Destroy(collision.gameObject);
}
Debug.Log(this.gameObject.name);
}
VIDEO
THANKS!
Here two things you need to check for the collision to work properly in Unity3D.
1. The two objects that are being collided in your case the bullet and the cube that the bullet is gonna collide with should have a collider (box or capsule) anything. And make sure isTrigger is off.
2. This is very important. Either one of the gameObject i.e., your bullet or the cube should have rigidbody component for collision to actually take place. If you dont have a rigidbody in the one of the components your collision wont work. So, I would suggest you to add rigidbody component to your bullet and that should do the magic for you.
Let me know if these worked or if you have any other problem.
And to which gameObject have you attached the following script to?
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
in advanced I would like to say if this is a really simple question with a simple answer, I apologize as I have just now gotten into programming.Basically, I'm trying to create a script that a block named blue(picture below) on collision with the FPSController, will get destroyed, here is my script:
using UnityEngine;
using System.Collections;
public class Cube : MonoBehaviour {
void OnCollisionEnter (Collision col) {
if(col.gameObject.name == "Blue") {
Destroy(col.gameObject);
print ("collison detected");
}
}
}
for some reason, though, whenever the fps controller collides with the object known as "Blue" nothing happens, the print() function is not triggered nor is the destroy() function
Thank you in advaned ;)
Rigidbody` is missing from your Cubes.
1.Attach Rigidbody component to both cubes.
2.Also,set both Cubes Rigidbody to Is-kinematic. You must set both Cubes Rigidbody to Is-kinematic so that the Character Controller wont be able to move it. Note that if your cube is falling down after adding Rigidbody, simply disable Use Graivty on the Rigidbody.
Important:
3.Delete FPSController. Since you will be interacting with other Rigidbody GameObjects, use RigidBodyFPSController. It can be found in Assets\Standard Assets\Characters\FirstPersonCharacter\Prefabs. Drag RigidBodyFPSController to the Scene then attach Cube script to it.
You will notice there is a Rigidbody attached to RigidBodyFPSController. Don't modify the settings of it.
That's it. Everything should be working as expected.
Cube settings:
RigidBodyFPSController Settings:
I trouble trying to get this to work. here is the problem.
When I spawn I bomb with a collider, it presses my character out of the way, forcing him either in the wall or to a side.
Now my character instantiates a Bomb gameobject with a bomb script.
What I want and my thought process:
I was thinking it might be possible having a triggercollider on the instantiated bomb, and no collider, but when my character "leaves" the bomb radius it spawn a regular collider.
Then He can't get stuck, and when he has exited he can't just past through it again.
But I don't know how to write this, any suggestions?
Or any better ideas? Thanks for all help :)
ps: ( I use c#)
You almost get a solution.
1. in bomb collider check trigger;
2. in bomb script at start get reference to collider
3. in OnTriggerExit set isTrigger to false
public class bomb : MonoBehaviour {
BoxCollider collider;
void Start () {
collider = gameObject.GetComponent<BoxCollider> ();
}
void OnTriggerExit(Collider other)
{
collider.isTrigger = false;
}
}
P.S. don't mix 3d and 2d colliders. Code sample for 3d, in 2d it slightly different.