I would like my player to collide with an invisible collider and restart the scene right away but it seems like my script isn't working for some reason.
Added the tag 'Fall' on the invisible collider.
Added the script on the Invisible Collider Game Object.
Invisible collider object has Box Collider 2D Attached and is set ti: is Trigger
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class sceneRestart : MonoBehaviour
{
private void OnCollisionEnter2D(Collision2D platform)
{
if (platform.gameObject.tag == "Fall")
{
SceneManager.LoadScene("Main");
Debug.Log("Scene Restarted");
}
}
}
The script is attached to the invisible collider, so it needs to check if the colliding object is the player:
public class sceneRestart : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D Col)
{
if (Col.gameObject.tag == "Player")
{
SceneManager.LoadScene("Main");
Debug.Log("Scene Restarted");
}
}
Related
I am making a script on an elevator that is supposed to teleport the player when they are next to it and press e
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class interactible : MonoBehaviour
{
public Transform player;
public Transform pos;
private bool collide;
void Start()
{
collide = false;
}
void OnCollisionEnter2D(Collision2D collider)
{
collide = true;
}
void OnCollisionExit2D(Collision2D collider)
{
collide = false;
}
void Update()
{
if (Input.GetKeyDown("e") && collide)
{
player.position = pos.position;
}
}
}
I have defined both player and pos and have made a 2d box collider for the elevator and set "is trigger" to true.
What am I doing wrong?
Your issue is that 2 triggers can't collide, only triggers and colliders can work with each other.
UPDATE: The main problem turned out to be that when you're dealing with triggers, you need to use OnTriggerEnter2D instead of OnCollisionEnter2D
Input.GetKeyDown accept enum so it should be
if(Input.GetKeyDown(KeyCode.E) && collide)
I have this script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoundPlayer : MonoBehaviour
{
public Transform Player;
public AudioSource SoundThatPlayed;
void OnTriggerEnter(Collider Player) => SoundThatPlayed.Play();
}
It works perfectly, but the sound plays when ANYTHING touches the part.
OnTriggerEnter fires when ANY collider enters the trigger. You can check for a specific tag, component, name, layer, etc. to filter out the unwanted triggers.
void OnTriggerEnter(Collider collider)
{
var playerScript = collider.GetComponent<Player>();
if (playerScript)
{
// An object with the Player script on it entered the trigger
//
}
var isPlayerByName = collider.name == "My Player Name";
var isPlayerByTag = collider.tag == "My Player Tag";
}
I have a Player who uses a weapon to shoot and destroy enemies. I have a code for a gun:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gun : MonoBehaviour {
float bulletSpeed = 60;
public GameObject bullet;
void Fire(){
GameObject tempBullet = Instantiate (bullet, transform.position, transform.rotation) as GameObject;
Rigidbody tempRigidBodyBullet = tempBullet.GetComponent<Rigidbody>();
tempRigidBodyBullet.AddForce(tempRigidBodyBullet.transform.forward * bulletSpeed);
Destroy(tempBullet, 5f);
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Fire();
}
}
}
and the code for bullets:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Enemy")
{
Destroy(gameObject);
}
}
}
even though my enemy is tagged as 'enemy' and has a box collider triggered it doesnt disappear. The bullet prefab has rigidbody and sphere collider. Please help :)
If you use Destroy(gameObject) you are destroying the bullet.
In order to destroy the enemy you should do a
Destroy(other.gameObject)
So you will destroy the object that actually triggered, the enemy
You are telling the bullet to destroy itself. You probably rather wanted
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy"))
{
// Destroy the thing tagged enemy, not youself
Destroy(other.gameObject);
// Could still destroy the bullet itself as well
Destroy (gameObject);
}
}
I'm new to coding and got stuck need help.
Im trying to make a small game involving trampolines and i used a script to send the character with force but i dont want the character to bounce normally when on the trampoline because they might go to high, but still bounce around on normal non- trampoline ground.
I have 2 Phys mats set up i just dont know how to switch them. when on different ground
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PhysMatChange : MonoBehaviour
{
void OnCollisionEnter(Collision collisionInfo)
{
Debug.Log(collisionInfo.collider.name);
if (collisionInfo.collider.tag == "trampup") ;
}
}
Changing the physics material of a collider col:
https://docs.unity3d.com/ScriptReference/Collider.html
https://docs.unity3d.com/ScriptReference/Collider-material.html
https://docs.unity3d.com/ScriptReference/PhysicMaterial.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PhysMatChange : MonoBehaviour
{
public PhysicsMaterial materialOne;
public PhysicsMaterial materialTwo;
private Collider col;
private void Start()
{
col = GetComponent<Collider>();
}
void OnCollisionEnter(Collision collisionInfo)
{
Debug.Log(collisionInfo.collider.name);
if (collisionInfo.collider.tag == "trampup")
{
// change to the first material
col.material = materialOne;
}
else if (collisionInfo.collider.tag == "trampdown")
{
// change to second material
col.material = materialTwo;
}
}
}
You can configure the bounciness of a physics material in the inspector
I making simple runner game.
I want do have some block over which player which jump.
I make a prefab and quad.
Attached Spawn script:
using UnityEngine;
using System.Collections;
public class SpawnScript : MonoBehaviour {
public GameObject[] obj;
public float spawnMin = 1f;
public float spawnMax = 1f;
// Use this for initialization
void Start ()
{
Spawn();
}
void Spawn()
{
Instantiate(obj[Random.Range(0, obj.GetLength(0))], transform.position, Quaternion.identity);
Invoke("Spawn", Random.Range(spawnMin, spawnMax));
}
}
Also I attached Destroyer script:
using UnityEngine;
using System.Collections;
public class DestroyerScript : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Application.LoadLevel(1);
return;
}
if (other.gameObject.transform.parent)
{
Destroy(other.gameObject.transform.parent.gameObject);
}
else
{
Destroy(other.gameObject);
}
}
}
But when player enter this object nothing happens.
Screen of my Quad:
Where is my mistake?
You use OnTriggerEnter2D. If your collider not ticked isTrigger field, you can use OnCollisionEnter2D.
And also if your object has Normal (3D) collider you need to use 3D versions of them.
OnTriggerEnter or OnCollisionEnter.
And also you should read this.
UPDATE
After discussion and looking your project problem is your character doesnt hit the destroyer object's collider. It moves with your main camera. (Destroyer object is child object of camera). Because of that when you take your destroyer from camera's child object it works.
Any child object in the heirarchy inherits it's parents movement. So if the collider is on a child object of the camera, it will move when the camera moves.