On-collision script not working? - c#

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:

Related

How can I check multiple collider Triggers at once?

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

Why does my script, which aims to remove gravity when the game starts, not work?

I'm trying to create a script in Unity wherein the gravity will go to zero when the game starts. The problem is, the gravity was still there after I launched the game.
This is for a Unity3D game. I've already tried to change the variable name, add using System; and moved my script to the top of the Rigidbody component, but none of these things worked.
public class PlayerGrav : MonoBehaviour
{
public Rigidbody rb;
void Start()
{
rb.useGravity = false;
}
void Update()
{
}
}
I expected the gravity to be removed when the game launches, but in the actual output the gravity still remained, and the cube still fell downwards.
create a new scene
create a new cube
add rb = this.GetComponent<Rigidbody>() to your start function in your PlayerGrav script
attach your PlayerGrav script to the new cube
Hit play, and take look at the "use gravity" property from your new cube's inspector window (make sure the inspector isn't locked to other game object)
If the above tryout succeeds, then you need to check the other code/objects in your original scene. If there are massive other objects in your original scene, then I will suggest disable other objects first, and re-enable them back group by group to see which object/monobhevaiour is causing the issue.
I know this is an old question but I looked forever trying to find an answer to this. I am new to unity so it took me a long time to figure this out but I am using a Rigidbody 2d so this is what worked for me
gameObject.GetComponent<Rigidbody2D>().gravityScale = 0;

detecting collisions and destroying gameObjects in Unity

So I'm attempting to create a 2D game in Unity where the gameObjects would be destroyed upon hitting the walls. So i decided to use the isTrigger function and add the script to each of the walls. However, I do not know why the gameObjects don't get destroyed. I have tried to set Collision type as both discrete and continuous for the walls and gameobjects and I've also added static rigidbodys to the walls to see if that would help and even tried changing the size of the collisionbodys of the walls.
Here is the code for the wallscript
public class wallScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void OnTrigger2D(Collider2D other)
{
if (other.tag == "player01")
{
Destroy(other.gameObject);
}
}}
p.s. Even if I remove the conditional statement Destroy() still does not work
You should use void OnCollisionEnter2D(Collision2D other) {...} instead of void OnTrigger2D(Collider2D other).
And uncheck Is Trigger checkbox on your object's Collider.
To Fix The Problem You Are Facing, First Select The Wall Sprite In The Scene, Scroll Down To The Collider And Make Sure Is Trigger Is Checked.
Other Than That Just Check If The Tag You Typed Into The Code Matches The Player You Are Trying To Destroy. Remember, It's Case Sensitive!
After That The Code Should Run Just Fine.

Jump OnCollision() Script not working

basically, I'm trying to make a game that is based off automatically hopping as soon as the ground is hit(the Controller I'm using is the pre-made one that can be imported).In order to do this, I removed the jump function from the controller script and added a script called "Cube" which reads as the following:
using UnityEngine;
using System.Collections;
public class Cube : MonoBehaviour {
void OnCollisionStay (Collision col)
{
if (col.gameObject.name == "Blue") {
Rigidbody rig;
rig= GetComponent<Rigidbody> ();
rig.velocity = new Vector3 (0, 8, 0);
print ("collison detected");
}
}
}
after doing this, I expected a controllable character which jumps as soon as you hit the ground because of the "OnCollisionStay()" trigger. However, instead I get a rapid jump that happens even when I'm in the air which looks like this:
https://youtu.be/ILtRac_RgLg
First of all, undo everything modification you performed to the RigidbodyFirstPersonController script. If possible, delete it and re-import a clean one from Unity.
Select your RigidBodyFPSController GameObject, Look at the RigidbodyFirstPersonController script attached to it in the Editor. Under it there is a setting called Advanced Settings. Under Advanced Settings, there is variable called Shell Offset. Change Shell Offset from its default value of 0 to 0.5. Play again and this problem should be gone. If that didn't work, bump it up more. This should solve your problem.
You are not in the air as the gravity is effecting on it. Whenever you are exiting the collision your y-axis upward velocity is not working and gravity force is taking place downward.
Disable gravity in FPSController's Rigidbody component if you don't want to use gravity.

OnTriggerEnter2D is not being called

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.

Categories

Resources