So I'm making a simple "puzzle" using lights. Simply, there are 3 buttons with lights on them (red, blue, green). Each button has it's own trigger volume but when I go to play, nothing prints that I even enter, stay, or leave the trigger. I've never used Collider Variables before so I feel like I'm doing something wrong (obviously or it would be working!). But then I just did "Collider entity" in the OnTriggerStay/Enter/Exit Method and it still didn't print to the console that my player was entering. Why are my Triggers not working?
Click here for the code I'm trying
Click here to see how I have it in the Unity Scene
Triggers only respond to other colliders that have rigid bodies on them.
Try adding a Ridgidbody component to your player and set it to kinematic.
OnTriggerEnter/Stay/Exit works when the Object has a Collider Component and BluePuzzle2 doesn't have that.
Also OnTrigger function gets a Collider as parameter. Check the reference page
So in order to make that work put a script on every light and on that script copy this function
void OnTriggerEnter(Collider col) {
if (col.CompareTag("Player")) {
print("Entered the trigger");
}
}
Hope it helps.
Related
Im making a gun pickup script, but when carrying the gun i don't want it to move from the guncontainer so i constrained its position, but when i run the script, it shows it as constrained in the inspector, but still moves. Afterwards when i try the same thing except i do it "physicly" only using the inspecter to check on the constrains, it works... help
you need to check that your gun container is also constrained, because if that moves the gun will also move.
i am trying to develop a melee combat game i am using edge collider and below code
i noticed that when i move or enable and disable the collider from the inspector everything goes but when i stand in my palace and use AttackCol.enabled = !AttackCol.enabled;
i cannot hit and the trigger function do not called
the only difference i see is the collider color when i add it from the inspector or while i am moving its color is normal but when i enable and disable it by code its color goes pale and do not do anything
public virtual void OnTriggerEnter2D(Collider2D collision)
{
if (DamageSources.Contains( collision.tag ))
{
StartCoroutine(TakeDamage());
}
}
Make sure that your Trigger Game Object is not marked as Static.
Remove virtual from function definition.
Create another Game Object for Trigger Component and try to change activation of whole the Game Object, not collider component
Stuff like AttackCol.enabled = !AttackCol.enabled; is clever, but it can go wrong when that one is called (accidentally) more than once. I suggest trying it out in the simplest form AttackCol.enabled = true; to make sure the error is not there. Later you can still make it more elegant again! :)
i added AttackCol.isTrigger = AttackCol.enabled;
after AttackCol.enabled = !AttackCol.enabled;
and everything geos ok right now
but i think it is a work around
but i need to know why the color goes pale ?
I know this is a really simple question but I can't figure out how to archive this:
I have a UI button in my scene and I want Vuforia to instantiate one AR Model ONLY when I press a button.
Following the tutorial on the net I was able to instantiate a model on the screen when I touch it but I need to know how to set up Vuforia for archive the same result only when I press a button.
I have to disable the "Anchor Input Listener Behaviour"?
And then?
I want to call for the PositionContentAtPlaneAnchor but I can't figure out how to call it in the right way in the OnClick field of the button. I need to make a custom script for this?
Thanks for any answer.
Ok, sorry for the delay.
I deduce that you are working with the ground plane, if you have the Ground Plane Stage and the Plane Finder in the scene and works, we're at a good point.
Now, you must only add a button to the scene and into the script add something like this:
public PlaneFinderBehaviour plane;
void Start()
{
...
buttonOnTheScene.onClick.AddListener(TaskOnClick);
...
}
void TaskOnClick()
{
Vector2 aPosition = new Vector2(0,0);
...
plane.PerformHitTest(aPosition);
}
What does it mean?
First of all, you must move the Plane Finder from Hierarchy to the script variable, so we have a reference to the plane into the script.
Then when you click (or tap) on the button you simulate the click (or tap) on the display with the PerformHitTest.
If you're wondering why my question in the comment, it's because the Plane Finder Behaviour Script has two modes type: Interactive and Automatic. The Interactive intercept the tap on the display and shows the object (on the ground plane) in the exact position of the tap, the automatic shows the object in the center of the plane.
So if you want the object in an exact position you can pass a Vector2 position in the PerformHitTest and if you want to show an object programmatically or do something when it shows the object you can call a custom method OnInteractiveHitTest.
That's all.
For my recent project i need a Door to open if the user is in front of it and has pressed "e".
This is the Code for the Button:
using UnityEngine;
public class Button : MonoBehaviour
{
public Animator Door; // In the editor, give a reference to your door. It must have an Animator script for this to work
void OnTriggerEnter(Collider c)
{
if (c.gameObject.tag == "Player")
{
//Text = "E to interact!"
if (Input.GetKeyDown("e"))
{
print("´Test");
GetComponent<Animator>().SetTrigger("OnPress"); // The button's animator goes to "pressed" state
Door.SetTrigger("Open"); // The door's animator goes to "open" state
}
}
}
}
The Animator looks like this:
The Transition from Idle to Open:
The Button has the Door attached to it:
So i got two issues here, the first is - With the above Code nothing happens if i press E. No Error, No Action, whatsoever.
If i delete the Input.GetKeyDown("e") and make the Button-Mesh to a Trigger and run in it, it says
MissingComponentException: There is no 'Animator' attached to the
"Button" game object, but a script is trying to access it. You
probably need to add a Animator to the game object "Button". Or your
script needs to check if the component is attached before using it.
If you need more Information, just let me know. Thanks!
Ok, your approach to the interaction is wrong. First of all, On trigger enter will work only once. That is when you enter the trigger. So it won't take the Keypress event. You will have to move the code in OnTriggerStay(Collider col) where it will constantly be called. There if it takes any key event, then it will fire up. That will solve your problem 1.
As for the second problem, I'm pretty sure you copy paste this code from somewhere else. Only the canvas UI Button has animator state. Your button is Normal mesh and not the UI one. So setting the state button in that code won't work, ie
GetComponent<Animator>().SetTrigger("OnPress"); // The button's animator goes to "pressed" state
Unless you are using the Canvas Button that script does not work. And the error description already mentioned the error you Button Mesh Does not have an animator. Try commenting the line and see how it works.
Edit:
I went through your transition your code should be
door.GetComponent<Animator>().SetTrigger("OnPress");
and not
GetComponent<Animator>().SetTrigger("OnPress");
As it is trying to call the OnPress state of door animator. If any trouble comes comment below with more info. Sorry did not notice this first.
Tried to find this kind of problem around the net but failed so...
Here's the thing - I have a prefab gameobject that is to represent a unit, portrait more specifically. It has several scripts attached and an Animation component with two animations: Static and Selected.
The prefab is instantiated, moved freely and, after placing, it can be clicked to select it, which should, aside from executing a bit of code, start the Selected animation.
Using this code:
void OnMouseDown(){
//
//Some inside stuff
//
if (this.GetComponent<UnitHandling> ().thisUnit.Selected)
this.animation.Play("Selected");
if(this.animation.IsPlaying ("Selected"))
Debug.Log("Animation of selection is playing");
}
I checked that the animation seems to be playing (at least the Debug message is showing) but I see no animation... What am I doing wrong?
Try making an animation state using mechanim, and play it using this:
GetComponent<Animator>().CrossFade("Selected", 0);
https://docs.unity3d.com/Documentation/ScriptReference/Animator.CrossFade.html
https://docs.unity3d.com/Documentation/Manual/MecanimAnimationSystem.html