ThirdPersonController collision not calling OnTriggerEnter event - c#

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");
}
}

Related

Unity OnControllerColliderHit detecting not detecting correctly

I am trying to teleport the character when they touch a cube, but for some reason, it prints the message that it the collision is being detected, but the character's position is not changing. It works for another scenario that is almost exactly the same, but even if I put this in another script, it does not teleport the character. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class othercollision : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.gameObject.name == "teleportcube")
{
gameObject.transform.position = new Vector3(-3184.53f, 20.35f, -171.585f);
Debug.Log("Collision detected");
}
}
}
Here is the script that has something similar working:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;
public class detectcollision : MonoBehaviour
{
public GameObject tplocation1;
public GameObject player;
public GameObject entranceloc;
void OnControllerColliderHit(ControllerColliderHit hit)
{
//This if statement does Debug.Log the message and it also changes the player's position.
if (hit.gameObject.name == "trigger")
{
Debug.Log("triggered");
gameObject.transform.position = tplocation1.transform.position;
Thread.Sleep(7);
Application.Quit();
}
//This if statement does not change the player's position, but Debug.Logs the teleporting message.
if (hit.gameObject.name == "entrance")
{
gameObject.transform.position = new Vector3(-3184.53f, 20.35f, -171.585f);
Debug.Log("teleporting...");
}
}
}
Try to make the Vector3 beforehand as a variable or a temporary variable and then assign it to the position. If you did then maybe the values that you have entered are out of boundary somewhy, but that's unlikely, try changing them.

OnCollisionEnter2D not called

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)

Nothing gets destroyed?

Nothing gets destroyed? I have one enemy which is a sphere with a collider and a rigidbody and a cube with the same. I've tried enabling triggers on either one but gravity goes wack and nothing gets destroyed! Please help!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyOnCollision : MonoBehaviour
{
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.CompareTag("Player"))
{
Destroy(collision.gameObject);
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy"))
{
Destroy(other.gameObject);
}
}
}
You need to add your Script to the Player and to the Enemy GameObjects instead of putting them into one separate Script.
Because OnTriggerEnter and OnCollisionEnter get called on the Object that is colliding or entering the Trigger
You can't just make a universal script that handles all Collision, because that Script or Object isn't colliding with anything. Because of that the functions never gets called and therefore nothing gets destroyed.
Changed Player Script (Add):
// The Enemey needs to have IsTrigger enabled
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy"))
{
Destroy(other.gameObject);
}
}
Changed Enemy Script (Add):
// Player needs to have IsTrigger disabled
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.CompareTag("Player"))
{
Destroy(collision.gameObject);
}
}

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.

Making Destroyer (UnityC#)

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.

Categories

Resources