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);
}
}
Related
I was trying to find the answer for the last 3 hours and I couldn't find anything that would help me. It shows that the camera already has constraints when it enters the cube, so it works, but it doesn't because it's not freezing the camera. I don't know what to do already.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LCOE : MonoBehaviour
{
public GameObject cam;
Rigidbody rig;
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
cam.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
cam.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
}
}
}
Changing constrains or isKinematic on Rigidbody will only disable physics movement, but it will still move together with parent object. Your camera should not be child of player and script attached to camera should look like that:
class MyCamera : MonoBehaviour{
public Transform target;
public Vector3 offset;
public bool isMovementDisabled;
void LateUpdate(){
if(isMovementDisabled)
return;
transform.position = target.position + offset;
}
}
Or use cinemachine
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");
}
}
I writing runner.
I have two problems
I have spawning object (quad).
1) I try to spawn my object many times , but it spawning once.
My Spawn script:
using UnityEngine;
using System.Collections;
public class SpawnScript : MonoBehaviour {
public GameObject[] obj;
public float spawnMin = 1f;
public float spawnMax = 2f;
// 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));
}
}
I try to make spawning script, like here Spawning but facing second problem
And second problem
2) I have destroyer script, I use it on spawning quad. On first object Player character destroys, on second object it through it.
Destroyer script
using UnityEngine;
using System.Collections;
public class DestroyerScript : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player") {
Destroy(other.gameObject);
Application.LoadLevel(1);
return;
}
For the spawning problem, you can use a coroutine.
void Start()
{
StartCoroutine(Spawn());
}
IEnumerator Spawn()
{
// Instantiate your game objects
Instantiate(obj[Random.Range(0, obj.GetLength(0))], transform.position, Quaternion.identity);
// Wait for a random time interval
yield return new WaitForSeconds(Random.Range(spawnMin, spawnMax));
}
For the collision problem, my guess is, the quad is destroyed by the player in player's collision script.
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.
I have been working on enemies in my game, but feel the code is clunky as is and also does not seem to work. I put code into my player script and enemy script. the player script is as follows:
EDIT: sorry for not being clear enough, here are the lines of code that do not work and what I want them to do:
void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.tag == "Enemy")
{
Destroy (this.gameObject);
}
}
What i want in this code is for when the player encounters an enemy or object with the "Enemy" tag, touching that object will destroy or kill the player object.
void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.tag == "Killable")
{
Debug.Log ("entering killzone!");
Destroy (other.gameObject);
Instantiate (DeadStar, transform.position, Quaternion.identity);
this.gameObject.SetActive (false);
}
}
}
This code was loosely copied from a previous game, this has bits of it's killzone and was made to destroy the player object when it touches any object that contains this script. (the player is tagged "Killable")
hope this edit helps, im a little tired and forgot to specify things, sorry about that.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float JumpSpeed;
public float Scale;
public string JumpKey;
public string LeftKey;
public string RightKey;
public float speed;
public GameObject Player;
public Rigidbody2D rb;
void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.tag == "Enemy")
{
Destroy (this.gameObject);
}
}
void Start()
{
GetComponent<Rigidbody2D>().freezeRotation = true; /*GetComponent<Rigidbody>().angularVelocity = Vector3.zero;*/
rb = GetComponent (typeof(Rigidbody2D)) as Rigidbody2D;
}
void FixedUpdate ()
{
Vector3 hold = rb.velocity;
hold.x = 0;
if (Input.GetKey (LeftKey))
{
hold.x -= speed;
}
if (Input.GetKey (RightKey))
{
hold.x += speed;
}
if (Input.GetKeyDown (KeyCode.Space))
{
hold.y = JumpSpeed;
}
rb.velocity = hold;
}
}
The enemy script is as follows:
using UnityEngine;
using System.Collections;
public class EnemyKillzone : MonoBehaviour {
public GameObject DeadStar;
public GameObject Player;
void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.tag == "Killable")
{
Debug.Log ("entering killzone!");
Destroy (other.gameObject);
Instantiate (DeadStar, transform.position, Quaternion.identity);
this.gameObject.SetActive (false);
}
}
}