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
Related
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.
I need my player to teleport somewhere when he bumps into a (unity) gameobject with a certain tag (are you seen) on it. I already found a solution, but then I switched my player's movement to a character controller and now it doesn't work. This is my current script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class found: MonoBehaviour
{
private void OnTriggerEnter(Collider collider)
{
if (collider.gameObject.tag == "are you seen")
{
transform.position = new Vector3(-15.38f, 1.93f, 62.1f);
}
}
}
When you switched to character controller, did you remove the rigidbody and collider? An OnTriggerEnter event still requires the player to have a collider with IsTrigger = true, and one of the gameobjects to have a rigidbody. Here is what the offical documentation says.
Note: Both GameObjects must contain a Collider component. One must have Collider.isTrigger enabled, and contain a Rigidbody. If both GameObjects have Collider.isTrigger enabled, no collision happens. The same applies when both GameObjects do not have a Rigidbody component.
I have used the most traditional method of doing this but it doesn't work. No response is initiated.
I've attached a rigidbody2d collider (with 0.0001 mass and no drag or gravity) to my player sprite and a box collider 2d with is trigger checked.
Sidenote: Outdoor1" is the name of the scene I want to teleport my player to.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class TeleportToScene : MonoBehaviour
{
[SerializeField] private string newScene;
void OnTriggerStay2D(Collider2D other)
{
if (other.CompareTag ("Player"))
{
SceneManager.LoadScene("Outdoor1");
I expect it to go over the box collider and change scenes but nothing happens. No error messages either.
to make OnTriggerStay2D you need to have an collider 2D set to trigger on the object who as the script TeleportToScene and a Rigidbody2D
So you scene will be, 2 object:
1.Player with
- Deplacement
- Collider2D(not trigger)
- Tag "Player"
2.Teleporter with
- TeleportToScene.cs
- Collider2D (trigger)
- Rigidbody2D (kinematic)
Hope that help !
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 am trying to make a replica of Asteroids in Unity. The problem is that my bullets are not triggering the OnTriggerEnter2D method on the asteroids. The asteroids have the following script attached:
using UnityEngine;
using System.Collections;
public class Asteroid : MonoBehaviour {
void Start () {
print ("class initiated");
}
void onTriggerEnter2D (Collider2D collider) {
Debug.Log (collider);
}
}
The bullet GameObject has Is Kinematic and Is Trigger enabled, and has Rigidbody 2D and Box Collider 2D attached. The asteroid GameObject has Rigidbody 2D and Circle Collider 2D, and Is Kinematic and Is Trigger is unchecked.
The problem is the Spelling. The o in onTriggerEnter2D should be capitalized. Simple mistake like this one can ruin your day. I didn't even notice it until I ran your code.
void OnTriggerEnter2D(Collider2D collider)
{
Debug.Log(collider);
}
Next time if you are not sure about the spelling of the Unity callback function, right click in Visual Studio then click Implement MonoBehaviours search for the function you want, select it and click OK. Visual Studio will add that function for you.