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)
Related
The way my code works is it checks if touching the ground, then if its true, it waits for space to get put in as an input, then if both statements are true, it uses the rigid body method to propel itself into the air.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Jump : MonoBehaviour
{
public string objectName;
public Rigidbody rb;
public int h;
// Start is called before the first frame update
void OnCollisionEnter(Collision col)
{
if(col.gameObject.name == objectName){
Debug.Log("On ground statement is true.");
if(Input.GetKey("space"))
{
rb.AddForce(0, h, 0);
}
}
}
}
The problem is, when i run it, it knows its on ground, but it won't respond to key inputs.
I know it can't be a problem with my computer, as my other controls work.
It's a bad idea to be testing for player input inside a collision event - if the player is pressing space, it will only ever register during the exact same frame that the object collided with something. A better place for that would be the Update() method, which happens every frame.
You should also create a variable that stores whether the player is currently on the ground or not, based on your collision events. Your input code will check this variable when deciding if the player is allowed to jump.
Try this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Jump : MonoBehaviour
{
public string objectName;
public Rigidbody rb;
public int h;
public bool onGround = false; // new
// get your player input in here instead
void Update()
{
if(onGround && Input.GetKey("space"))
{
rb.AddForce(0, h, 0);
}
}
void OnCollisionEnter(Collision col)
{
if(col.gameObject.name == objectName){
Debug.Log("On ground statement is true.");
onGround = true; // new
}
}
// new
void OnCollisionExit(Collision col)
{
if(col.gameObject.name == objectName){
Debug.Log("No longer on the ground.");
onGround = false;
}
}
}
I have a script attached to a prefab and the script is :
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class destroyer : MonoBehaviour
{
Circles circles;
CircleCollider2D collider1;
Collider2D collider2;
private void Start()
{
circles = FindObjectOfType<Circles>();
}
private void OnEnable()
{
collider1 = gameObject.transform.GetChild(0).GetComponent<CircleCollider2D>();
collider2 = gameObject.transform.GetChild(1).GetComponent<Collider2D>();
}
private void Update()
{
if (transform.position.y < 2)
{
Destroy(gameObject);
circles.instantiator();
}
}
void OnTriggerStay2D(Collider2D other)
{
if (collider1.bounds.Contains(other.bounds.max) && collider1.bounds.Contains(other.bounds.min))
{
if (other.bounds.Contains(collider2.bounds.max) && other.bounds.Contains(collider2.bounds.min))
{
if (transform.position.y > 3)
{
Destroy(other.gameObject);
Destroy(gameObject);
circles.instantiator();
}
}
}
}
}
When I instantiate the prefab the if condition is never running even if the condition is true.
But when I put the prefab on the scene and play the game then this if condition is running fine according to the condition. I am not able to figure out what is the problem in this.
Did you add the children of the prefabs in order. The GameObject with the 2D circle collider should be first and the 2nd child should have a 2D collider.
Try:
collider1 = GetComponentInChildren<CircleCollider2D>();
collider2 = GetComponentInChildren<Collider2D>();
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
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");
}
}
I've been trying to make a sliding door for my unity level and I've managed to set up the animations but the scripting that's supposed to link up the functions with the objects isn't working.
Here's the script for the key card:
using UnityEngine;
using System.Collections;
public class Key_Pickup_1 : MonoBehaviour {
public GameObject player;
private Player_inventory playerinventory;
void Awake ()
{
playerinventory = player.GetComponent<Player_inventory>();
}
// Update is called once per frame
void onTriggerEnter()
{
if (gameObject == player)
{
playerinventory.hasKey_1 = true;
Destroy(gameObject);
}
}
}
Here's the script for the Door animation:
using UnityEngine;
using System.Collections;
public class Door_Animation_1 : MonoBehaviour {
public string Open;
private Animator anim_1;
public GameObject player;
private Player_inventory playerInventory;
void Start()
{
anim_1 = GetComponent<Animator>();
player = GameObject.FindGameObjectWithTag("Player");
playerInventory = player.GetComponent<Player_inventory>();
}
void OntriggerEnter (Collider other)
{
if(other.gameObject == player)
{
if (playerInventory.hasKey_1)
{
anim_1.SetTrigger(Open);
}
}
}
Any Ideas?
You don't have the proper capitalization for the OnTriggerEnter methods in your code. You have two different spellings and they are both wrong. It must be exactly OnTriggerEnter (or OnTriggerEnter2D for objects with a Collider2D instead of a Collider).