Making Destroyer (UnityC#) - c#

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.

Related

Unity 3D shoot and destroy enemy do not work

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 can't freeze my camera by a script even though it shows that it's freezing all the axis

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

ThirdPersonController collision not calling OnTriggerEnter event

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour {
public Transform target;
// Update is called once per frame
void Update ()
{
}
private void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "Test")
{
this.transform.position = target.position;
}
}
}
I have a ThirdPersonController and i want it to collide with a cube or cylinder.
The script is attached to the ThirdPersonController.
I tried to add either to the cylinder or the cube Rigidbody turned on/off the Use Gravity and the Is Kinematic but nothing. It's not getting to the event.
ThirdPersonController uses CharacterController and OnControllerColliderHit is used for that not OnTriggerEnter.
Note that you must move it with the Move function not directly by its transform in order for OnControllerColliderHit to be called.
void OnControllerColliderHit(ControllerColliderHit hit)
{
}
Every thing is correct but the thing wrong here is you are changing the position of the object which is colliding with a second object , but the thing id the thing is the collider is already there.....
As it's alternative try to print any statement when collision happens like this
private void OnTriggerEnter(Collider other)
{
if (other.tag==your_tag)
{
print("message");
}
}

How do i get object position and transform position to it

How do i localize other GameObject position
and Move to it (like teleport to that object) how can i do it?
BotController.Player
it's object what i want to get position of it and move to it (teleport)
i work on game hack
In case you already have reference to the target object you can just use this.position = BotController.Player.position anywhere in the object you wanna teleport.
public class EasyTeleporter : MonoBehavior
{
...
public void SomeFunction()
{
position = BotController.Player.position
}
}
If you are creating first person game and want to achieve something like teleporter to any object you should use raycasting for that.
For example you can take Unity default asset FirstPersonCharacter (available on asset store or you can add it when starting a new project) and add the following script to the FirstPersonCharacter gameobject (wich is child of FPSController prefab):
using UnityEngine;
using System.Collections;
public class PlayerTeleporter : MonoBehaviour
{
bool shooting = false;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
shooting = true;
}
}
void FixedUpdate()
{
if (shooting)
{
shooting = false;
RaycastHit hit;
// you are casting a ray in front of your camera wich hits the first collider in its path
if (Physics.Raycast(transform.position, transform.forward, out hit, 100f))
{
// normally you shouldn't teleport directly into the trget object
transform.position = hit.transform.position;
}
}
}
}
Generally you should clarify you question. What game you are creating, what are the target and object of teleportation and how do you wanna trigger that.

Spawning object and Destroing player character

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.

Categories

Resources