What I want is to destroy the cube when the third person camera touches it...but anything i have tried so far fails...
Here is the code i have tried:
#pragma strict
var other : GameObject;
function Start () {
}
function Update () {
}
function OnCollisionEnter ( collision : Collision) {
if (collision.tag == "Character")
Destroy (collision.gameObject);
}
Thanks for any suggestions!
There are two simple ways to do this. One of them is attaching a script to the character to destroy specified objects and the other one is to attach a script to the object to be destroyed on inpact with the character, but on both of those ways you NEED the Rigidbody component to be attached too.
Adding this to the object to be destroyed and tagging the character:
[RequireComponent (typeof (Rigidbody))]
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "Character")
Destroy(this.gameObject);
}
OR
Adding this to the character and tagging the objects to be destroyed:
[RequireComponent (typeof (Rigidbody))]
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "ToBeDestroyed")
Destroy(col.gameObject);
}
remember: this code is in C#, you`ll need to convert to javascript if you are going to add to an existing script
Related
Here is the code in which I was talking about. I can't figure out why my collider isn't working:
using System.Collections.Generic;
using UnityEngine;
public class SphereCollider : MonoBehaviour
{
GameObject obj;
void Awake ()
{
obj = GameObject.FindGameObjectWithTag("Player");
}
// Update is called once per frame
void Update()
{
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "Player")
{
obj.GetComponent<Health>().health -= 25;
}
}
}
}
Rather than using the code you have, make sure to change some code. This is what you should change: get rid of the obj variable and the awake function. Then instead of finding the object with that name, use tag. Also get rid of Update(){} and take the OnCollisionEnter(){} out of it.
A few more changes are shown in code:
void OnCollisionEnter(Collision collision){
var obj = collision.GameObject;
if (obj.Tag == “Player”){
obj.GetComponent<Health>().health -= 25;
}
}
This will work as long as: the script is called Health, the variable inside Health is called health, and it is set to a float, and the player’s tag is set to Player.
Not sure if the code pasted in the question is just copied over wrong, but OnCollisionEnter() is its own unique method and should not be nested in Update().
If the player will be colliding with this Sphere object often, it is good to cache a component rather than using GetComponent frequently. However I assume in your case this Sphere will be one of many obstacles, so calling the GetComponent in the collision is fine. The issue with your current code is the gameObject you are colliding with might not have the name exactly spelled as Player, which would make your collision check fail. Caching the object that has the tag of Player is a bit redundant when you can just check it inside of the collision check.
I will use your entire snippet of code so there is no confusion
using System.Collections.Generic;
using UnityEngine;
public class SphereCollider : MonoBehaviour
{
void OnCollisionEnter(Collision col)
{
// compare if the collider's gameObjects tag is equal to our target tag (Player)
if (col.gameObject.tag == "Player")
{
// get the component of Health on our gameObject that we collided with that has the tag of Player
obj.GetComponent<Health>().health -= 25;
}
}
}
I have the below script attached to the ground game object to detect collisions from my players' child objects but for some reason, the collisions are not detecting.
My player (parent, empty game object) has a Rigidbody and Jump script attached to it, meanwhile, the child game objects (body & feet) have just box colliders on them.
Would love to know why this isn't working :)
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Body")
{
Debug.Log("Game over!");
}
else if (collision.gameObject.tag == "Feet")
{
Debug.Log("Alive!");
}
}
For some reason, it works when you do collision.collider.tag instead of collision.gameobject.tag, like so:
if(collision.collider.tag == "Head")
{
Debug.Log("Game over!");
}
Not exactly sure why this works, but it works! Defo going to look into this a bit more.
I've been sort of teaching myself and sort of learning from Jimmy Vegas on youtube: https://www.youtube.com/channel/UCRMXHQ2rJ9_0CHS7mhL7erg
If you haven't seen those tutorials or don't want to look, one of the things he does is create a small script that destroys a coin when the player collider hits it, but mine isn't working. Code below (a little mis-formatted, sorry, couldn't get it to format correctly):
function OnCollisionEnter (collision : Collision) {
if(collision.gameObject.tag == "coinCollect") {
Destroy(this.gameObject);
}
}
I applied the script to a prefab and placed a bunch of coins around a little area, additionally, I made a capsule collider in a first person controller tagged "coinCollect", and ticked "Is Trigger"
Also, I'm trying to make a teleporter that teleports the first person character from one teleporter to another. Code below:
var warptarget001 : GameObject;
var warptarget002 : GameObject;
function OnTriggerEnter (col : Collider) {
if (col.gameObject.tag == "warp001") {
this.transform.position = warptarget002.position;
} else if (col.gameObject.tag == "warp002") {
this.transform.position = warptarget001.position;
}
}
I have four objects here, two warp pads and two warp targets. The two warp pads are tagged "warp001" and "warp002", respectively and the two warp targets are not assigned anything in the code, but assigned by dragging and dropping an empty object into the Serialized Field the script provides. Both pads have capsule colliders with "Is Trigger" unticked but it doesn't work either way, ticked or unticked.
Can anyone tell me what I might be doing wrong? Thank you.
The script was all correct, my problem was that my parent "FPSController" object didn't have a Rigidbody applied to it and should be the only object (as opposed to the "FirstPersonCharacter" object I had nested inside of it) that the scripts are applied to. That seemed to fix the problem.
The correct code is:
/* coincollect.cs */
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class coincollect : MonoBehaviour {
private int _score;
[SerializeField]
private Text _text;
void OnTriggerEnter ( Collider collision ){
if(collision.gameObject.tag == "coin"){
Destroy(collision.gameObject);
_score++;
_text.text = "Score: " + _score;
}
}
}
and:
/* warp.js */
var warptarget001 : GameObject;
var warptarget002 : GameObject;
function OnTriggerEnter (col : Collider) {
if (col.gameObject.tag == "warp001") {
this.transform.position = warptarget002.transform.position;
}
if (col.gameObject.tag == "warp002") {
this.transform.position = warptarget001.transform.position;
}
}
The only thing I can think of for your first problem is that it shouldn't need IsTrigger ticked. Other than that, it sounds like it should work (unless I'm missing something).
For the second problem you're having (with the warps), I don't think you can use warptarget001 by dragging and dropping objects into the fields. The reason being that what you've dragged into that field isn't the same object instance that's inworld.
You should assign their values through the code (preferably in the Start method), by using GameObject.Find("name") for example. This way, warptarget001 corresponds to the inworld gameobject.
I have a platform that i want to fall once the player has stepped on it. It has two box colliders. One for it's physical being and the other as a trigger. It also has a rigidbody which is kinematic by default so it doesn't fall straight away. However when the player steps on it it does nothing. Can any one tell me what's wrong with my code?
var yourObject : GameObject;
function OnTriggerEnter(Other : Collider){
if(Other.gameObject.tag == "Player"){
yourObject.rigidbody2D.isKinematic = false;
}
}
Plase check your gameobjects tag.
and also i see you use Rigidbody2D so you don't add below
var yourObject : GameObject;
function OnTriggerEnter(Other : Collider){
if(Other.gameObject.tag == "Player"){
yourObject.rigidbody2D.isKinematic = false;
}
}
You need to use this
function OnTriggerEnter2D(other: Collider2D) {
if(other.gameObject.tag == "Player"){
yourObject.rigidbody2D.isKinematic = false;
}
}
And also please check you add tag to gameobjects (I think you mean check names)
Like
function OnTriggerEnter2D(other: Collider2D) {
if(other.gameObject.name == "Player"){ //check name
yourObject.rigidbody2D.isKinematic = false;
}
}
To achieve a falling platform of sorts you do not have to make use of the IsKinematic function.
Instead, you can just turn off the Use Gravity function. This will prevent the object from falling, until another object with a rigidbody and a mass bigger then the mass of the platform touches it. As can be seen in the GIF below.
I'm currently making a infinite runner type game for a class project and I need some help with collisions.
The game is set up as a 2d platform where a character is running (or made to look like they're running with a scrolling texture) through an infinite tunnel. The player has to avoid obstacles similar to Flappy Bird using jumps. The script I'm using at the moment is setup so that when the player collides with obstacles the game will reload.
The issue:
The issue is that the code I've used applies to all collisions so when the player hits the ground platform the game reloads.
What I want to happen:
I want the character to be able to run on a platform and die when he hits the a specific obstacle.
So I'm think I need to specify a GameObject to collide with?
This is my current code:
// Update is called once per frame
void Update ()
{
// Jump
if (Input.GetKeyUp("space"))
{
rigidbody2D.velocity = Vector2.zero;
rigidbody2D.AddForce(jumpForce);
}
// Die by being off screen
Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
if (screenPosition.y > Screen.height || screenPosition.y < 0)
{
Die();
}
}
// Die by collision
void OnCollisionEnter2D(Collision2D other)
{
Die();
}
void Die()
{
Application.LoadLevel(Application.loadedLevel);
}
}
Use tags. For example, use a terrain tag for the ground. If the object tag is terrain, then don't die.
Here is a simple example:
void OnCollisionEnter2D(Collision2D other)
{
if (other.collider.tag == "Terrain")
{
Debug.Log("Don't die!");
}
else
{
Debug.Log("Die!");
}
}