On Trigger enter Unity VR function - c#

So me and some people are developing a VR game and we need to have it when the VR hand Presses the button Some objects are hidden so when the trigger on the collider is entered
So I have the function
public void update()
{
//Called once per frame
}
Private void Ontriggerenter(collider, other)
{
if ( other.tag == "Hand")
{
Copper.SetActive(false);
}
Where copper is a public game object
I still have the start and update functions not sure if that's the problem but the button clicks but the function is never triggered dose someone maybe know the problem

the "T" and "E" of OnTriggerEnter should be capitalized and OnTriggerEnter takes a variableof type Collider. I am not sure if you have configured the colliders correctly. Here is how the code should look like
Private void OnTriggerEnter(collider other)
{
if ( other.tag == "Hand")
{
Copper.SetActive(false);
}
}
You can check out this article on Unity Collision basics to get some basic idea.

In order for a Trigger collision to happen, both objects need to have colliders, at least one needs to have a non-kinematic Rigidbody component on the same object as the collider, at least one of the colliders needs to have the "IsTrigger" property set to true (checked).
Here is a link to a Chart about how to get different types of collisions.
Note that there is a difference between a Trigger and a Collision (OnTriggerEnter vs OnCollisionEnter).

I removed the trigger and just using the positions of the VR button and called the setactive when the pos changed. Thank you all for your help tho.

Related

OnTriggerEnter unity3d

Please tell me, there is a function OnTriggerEnter:
void OnTriggerEnter(Collider other) {}
This function matches if an element is in the trigger of another element.
Now this script is on the character.
How can I perform this function for my character by hanging a script for example on terrain?
There is a lot of left out context in this question, and I hate to make assumptions, but I can't yet comment so I will try my best to answer. Assuming that you want to detect if the character is colliding with the terrain, the easiest way to do this, would be to instead use void OnCollisionEnter (). If your player character has a Rigidbody and a collider component, and your terrain has a collider component, you can tag your terrain "Terrain" and detect for collisions with it using the following function in your code:
//Detects a collision and grabs the collider
void OnCollisionEnter (Collider other)
{
//Detects if the object collided with has the "Terrain" tag
if (other.gameObject.tag == "Terrain")
{
//Your code here
}
}
Looks like you want to know how to use OnTriggerEnter
Here are the steps.
Attach Colliders to the objects that are expected to collider. At
least one of the Collider should be marked as trigger.
Attach a non-Kinematic Rigidbody to one of the objects.
attach the script with OnTriggerEnter function to one of the objects. The
function will be called if the collision is detected.
You can read this tutorial on Unity Collision for detailed explanation

Collider with a boolean

I have been working on this problem for some days now and would appreciate it if anyone can help me out. So, I am trying to make a simple board game on unity and trying to trigger an event (ex. getting a card) when a player stops at a specific waypoint. I have a boolean for hasStopped in another class called (Moving) that becomes false when the dice are clicked and true when the player has stopped moving.
Now, my game used to work with this code:
public void OnTriggerEnter2D(Collider2D collision)
{
if (hasStopped)
{
GetCardmethod();
}
}
but I'm not sure what I changed that this method stopped working. I checked in other methods to see that my boolean is getting updated properly, and I'm not sure what the problem is. I think my collider is detecting the boolean too early before it changes, but I don't know what to do to make this work. Like how would I get this game to only trigger the event if the player lands on the specific waypoint with the dice? Thank you
Check this solutions if they work for you:
If you have a 3d collider then you should use OnTriggerEnter() rather than OnTriggerEnter2D()
If your boolean hasStoped is in another cs script then you should make a reference to that script and access the boolean from there.
You might also want to check of the collision detection is continuous.

Animate an object by pressing Key / or Mouse Click Unity3D

currently i'm trying to animate an object by clicking or pressing a key. Unfortunately, script won't work and I have tried soooooo many other ways to do it.
Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TurnBoard : MonoBehaviour
{
public Animator anim;
void Start()
{
anim = GetComponent<Animator>();
}
void OnTriggerStay(Collider player) {
if (player.tag == "Player" && Input.GetKeyDown(KeyCode.G)){
//Debug.Log("touchyy");
anim.SetTrigger("turn");
}
}}
I have assigned this script file to object itself. Object has a box collider. Animation has a trigger named "turn". When player enters to the collider zone, I want player to be able to activate animation of the object with a click/or keypress.
I do get "Debug.Log" when player enters the zone. So I believe that there is no problem in detecting the collision. But just can't manage to animate object in any way.
Any help? Thank you!
OnTriggerStay is run like FixedUpdate (since it involves physics), so none of the Input events will work correctly inside it for the same reason that they won't work right in FixedUpdate. All Input functions must only be used in Update method.

What's stopping my camera movement script from receiving input events?

Thanks for reading. I tried posting this on unity answers, but it's been 24 hours and it's still not out of moderation, so maybe I'll have better luck here.
Background
I am very new to unity and know I must be missing some critical piece of knowledge to tie this together. I've created a blank game with some placeholder terrain and added a main camera to the 3d orthographic scene. I would like the user to be able to move the camera along a parallel plane to the ground.
What I've Done
I've added a starter camera codebehind I found online along with an event system, rigid body, ray caster, and a bunch of other things I saw online for this sort of thing. All of these components are in the same layer.
The Problem
My camera script's update method is hit when I put that in for testing but it doesn't receive any other events even though it implements the appropriate interfaces. I'm guessing I have something blocking the input from hitting the script or I do not have a component I need in order for those events to fire.
I'm testing on desktop for now but I want this to run on mobile (although from what I've read the events I'm subscribing to should be universal). Can anyone spot any oversights or deficiencies in my setup so far? Here are some screenshots.
Thanks!
My Setup
Edit: by request, here is the camera script I'm using. I know it doesn't do what I want yet but the issue I'd like to tackle right now is why it doesn't seem to be wired up correctly. Like I said, if I put an update method in here it is hit but none of my other logs are hit.
using UnityEngine;
using UnityEngine.EventSystems;
// The touch is in 2D but the scene is in 3D, so touch.x => scene.x, touch.y => scene.z, and nothing change (0) => scene.y since y is up
public class TouchCamera : MonoBehaviour,
IPointerDownHandler,
IDragHandler,
IPointerUpHandler
{
private Vector3 prevPointWorldSpace;
private Vector3 thisPointWorldSpace;
private Vector3 realWorldTravel;
private int drawFinger;
private bool drawFingerAlreadyDown;
public void OnPointerDown(PointerEventData data)
{
Debug.Log("Pointer down");
if (drawFingerAlreadyDown == true)
return;
drawFinger = data.pointerId;
drawFingerAlreadyDown = true;
prevPointWorldSpace = data.pointerCurrentRaycast.worldPosition;
// in this example we'll put it under finger control...
GetComponent<Rigidbody>().isKinematic = false;
}
public void OnDrag(PointerEventData data)
{
Debug.Log("Pointer drag");
if (drawFingerAlreadyDown == false)
return;
if (drawFinger != data.pointerId)
return;
thisPointWorldSpace = data.pointerCurrentRaycast.worldPosition;
realWorldTravel = thisPointWorldSpace - prevPointWorldSpace;
_processRealWorldtravel();
prevPointWorldSpace = thisPointWorldSpace;
}
public void OnPointerUp(PointerEventData data)
{
Debug.Log("Pointer up");
if (drawFinger != data.pointerId)
return;
drawFingerAlreadyDown = false;
GetComponent<Rigidbody>().isKinematic = false;
}
private void _processRealWorldtravel()
{
Debug.Log("Updating camera position");
Vector3 pot = transform.position;
pot.x += realWorldTravel.x;
pot.z += realWorldTravel.y;
transform.position = pot;
}
}
Final Edit for the Solution
Thank you very much to Juan Bayona Beriso for helping me in chat with this configuration. Here is what we ended up with that now works:
The camera object has a Rigidbody and Physics 2D Raycaster attached to it. The scene now has a new Canvas object with a Graphics Raycaster (blocking mask set to everything and blocking objects set to none) and an event system with the standalone input module enabled. That canvas also has a child UI object that covers the screen and has the camera script attached. As a curiosity, it required a text component in order for the script to start receiving the events -- this only made a difference when the Raycast Target option was selected. Screenshots below.
I think you are misusing the EventSystem, OnPointerDown, OnDrag and all those functions, they are called when you have a PhysicsRaycaster in your Camera object and you press or drag over an UI Element or and object with a collider.
In your code you don't have any of these, you have a camera so you are not actually pointer down over it or dragging on it.
You have two options:
Either put this script in a plane with a collider or GUI element that is inside the camera and moves with it, or use Input.GetMouseButtonDown, Input.GetMouseButtonUp in the Update function

How do you make a 2d object go through another and detect there was a collision?

I'm creating a 2D snowboard game in which there will be snowballs that you will have to dodge, if you get hit by the snowball it will disappear and subtract the score.
I tried creating two game objects called Player and Ball and the code to detect the collision looks like this.
void Update
{
if (Player.transform.position.x == Ball.transform.position.x)
score--;
}
I'm not sure if I should create a OnCollisionEnter but i'm not sure how to do it.
Use OnTriggerEnter or OnCollisionEnter
http://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html

Categories