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 ?
Related
I am trying to create an AR app using Unity & Vuforia. I have a 3D model that needs to be spawned when ground plane is detected.But this needs to happen only once.
The way Vuforia work is, it keeps on spawning objects when new plane is detected. So what i need to do is either detect plane only once or spawn the object only once. As i am new to Unity, i need help doing this. Great if someone could tell me what i need to do to achieve this.
Vuforia has updated.Now There is no DeploymentStageOnce script.Inorder to stop duplicating while we touch, we have to turn off Duplicate Stage in Content Positioning Behaviour (Script)Check the Inspector when we click Plane Finder.
In your app you should have a Plane Finder object somewhere with the following properties set by default
The Plane Finder object has a Behaviour component attached that calls a Position Content method if a plane was found. That method belongs to the Content Positioning Behaviour and it makes an instance (Clone) of your Ground Plane Stage. In order to avoid more than one instance you should import the vuforia Deploy Stage Once script located here: https://library.vuforia.com/articles/Solution/ground-plane-guide.html and you should change the Plane Finder Behaviour as the following:
I struggled a long with it, in short we must disable AnchorInputListenerBehaviour after hit.
I attached a new script on PlaneFinder with this code below:
<!-- language-all: c# -->
public void OnInteractiveHitTest(HitTestResult result)
{
var listenerBehaviour = GetComponent<AnchorInputListenerBehaviour>();
if (listenerBehaviour != null)
{
listenerBehaviour.enabled = false;
}
}
I added event on Plane Finder Behavior
That's all, I hope it will be useful.
For Updated Versions:
go to "Advanced" setting and "On Interactive Hit Test" script -> Select "Off" option for the script.
Most of the answers are correct but kind of obsolete, the correct way to do that is by code.
Create for example a gameObject called GameManager and pass the GroundPlaneStage and a prefab of the object you want to spawn to a script attached to that GameManager for example call it GameManagerScript.cs, and create a small function called spawnObjects which does the following:
public class SceneManagerScript : MonoBehaviour {
public GameObject objPrefab;
public GameObject ground;
private int count = 0;
public void spawnObject() {
Instantiate(objPrefab, new Vector3(count, 0, 0), Quaternion.identity, ground.transform);
count += 2;
}
}
then after that go to the PlaneFinder specifically to the PlaneFinderBehaviour.cs component you will have callbacks for OnInteractiveHitTest and OnAutomaticHitTest, in your case you need the OnAutomativeHitTest, click + and add a new callback (the function spawnObject in code above like in the image below)
also when you instantiate the object of your preference via the prefab don't forget to write the proper position updates to prevent the objects from getting added in the same position
also don't forget to make the GroundPlaneStage the parent of the object and realize that the position you are adding in the Instantiate() function is relative to that parent (GroundPlaneStage which is represented in the code above with the variable ground)
Finally don't forget to uncheck Duplicate Stage from the "Content Positioning Behaviour" component in the Plane Finder as shown in the picture below:
I hope that helps
please try the vuforia website for this problem
Introduction to Ground Plane in Unity
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.
I have scenes where the player instantiates lots of prefabs and I need them to persist. Can I use EditorSceneManager.SaveScene to do this? Or is this completely wrong?
If not, is there something similar? I need a way to save instantiated prefabs in its scene.
Can I use EditorSceneManager.SaveScene to do this?
No
If not, is there something similar?
No
There is no built-in function to save instantiated objects during gameplay. You have to implement this yourself as the code to do this will be big and a little bit complicated.
You need to write an Editor extension plugin for this. When Object is instantiated, save that GameObject into a List with it's information such as the type of the object, position, rotation, color and components that are attached to that GameObject.
When stop is pressed, use the Editor mode and that List to re-instantiate those GameObjects back by looping over the List.
Detect Scene Play and Stop:
void Start()
{
EditorApplication.playmodeStateChanged = OnPlayModeEnter;
}
void OnPlayModeEnter()
{
if (EditorApplication.isPlayingOrWillChangePlaymode)
{
Debug.Log("Playing!");
}else{
Debug.Log("Stopped Playing!");
}
}
You do the rest!
Still not possible in 2018.2.
My work around is to save a prefab using PrefabUtility.CreatePrefab().
I'm trying to make a generic "Snake" game using Unity in order to reinforce my knowledge of C# and Unity fundamentals.
The problem I'm having is that I can't seem to get the player to Game Over when colliding with the body. Currently I have the collision check set up like this:
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Border") || other.gameObject.CompareTag("Body")) //if the player collides with a Border tag or Body tag...
{
//Game Over Sequence
GameOver();
}
else if (other.gameObject.CompareTag("Collectable")) //otherwise if the player collides with a Collectable tag...
{
//Snek needs to grow!
shouldGrow = true;
}
}
Here's a picture of the snake body's prefab inspector as well. I checked to make sure the tag is set to Body.
The border collision is working fine, and the strange part is setting the border object to the "Body" tag is also working fine, so I'm not sure why the body segments aren't triggering the Game Over sequence.
I'm new to programming, so apologies if this question is obvious, but I don't understand why this isn't working.
Thanks in advance!
EDIT: Per Programmer's request, here's an image of one of the Border's inspectors:
Tick 'is Trigger' on the Snake_Body's box collider. Without this the OnTriggerEnter2D function is not called. You have done this correctly on the border box collider.
I'm currently working on a script that can pick up an item, and drop it with the click of the left mouse button. I'm also planning on addint rotating of an item and some icons to display whenever i do one of those actions.
I'm currently very new to this, so i might be trowing myself out in the depths. But i would like to try.
Here's my code:
public class PickUp : MonoBehaviour {
public Transform onHand;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButton(1)){
this.transform.position = onHand.position;
}
}
void OnMouseDown () {
GetComponent<Rigidbody>().useGravity = false;
this.transform.position = onHand.position;
this.transform.parent = GameObject.Find("Player").transform;
}
void OnMouseUp () {
this.transform.parent = null;
GetComponent<Rigidbody>().useGravity = true;
}
}
So far it kind of works.. I've some trouble picking up my object, it does not always let me. I have to click a couple of times, before it actually gets a hold on the object. When it does, the objects starts flying upwards for some weird reason i do not understand. I still have a hold on it, i can still walk around with it and as soon as i let go it falls down.
Based on the limited amount of data you have shown, I can suggest some common things to check / try. If you update with more details I'll try and help you further.
What value is assigned to your public Transform onHand variable?
Is there a reason why you are doing click detection in two places? Try deleting the lines inside the "Update" method.
The OnMouseDown method should be enough. However, for OnMouseDown to work, your object needs to have a physics collider setup. Check to see if you have a collider and that it's dimensions match what you would expect.
For debug purposes, try setting "isKinematic" to true on your pickup's rigidbody (from the inspector, permanently) this should disable gravity and other forces from moving your pickup object, so you can test out the rest of your code.
Also when you pick it up, set it's transform position to 0,0,0
This should make the object follow the player at the exact center spot of the player object.
Once you verify this works correctly, put features back in and see if any of them break the setup.
Start by setting the position back from 0,0,0 to onHand and then check if the pickup actually appears on the player's hand.
If not, check the value of the onHand variable.
Then you can turn off isKinematic and see if everything is still okay.
As a side note:
You might want to keep using isKinematic instead of disabling gravity. You could set it as kinematic when it's picked up to stop any forces from affecting the pickup's position, while the pickup itself will still have an effect on other rigidbodies.
Then when you drop it on mouse up, just turn off isKinematic again