Why onmousedown is not working on empty gameobject? - c#

I added a box collider to the gameobject.
And yet the onmousedown in the script never trigger.
And the script Back
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Back : MonoBehaviour
{
public GameObject[] objectstexts;
public Rotate rotate;
private void OnMouseDown()
{
rotate.StartRotations();
}
}
The gameobject back is not a cube it's not the cube I want to click on.
I'm using the Back empty gameobject just to get it's name for switching the name on the bottom cube. From Quit to Back.
The back empty gameobject is positioned at the same place of the Quit cube.

Make sure the script is attached to the game object you want to be clickable.
Make sure you have an enabled collider
Make sure you have not resized or changed the position of the collider
Make sure there are no other colliders in the way. OnMouseDown() works just like a Raycast()...the first thing hit is what gets the OnMouseDown().
If everything looks okay and it still does not work, delete the object and start over by adding the new object, adding the script...

The very first statement on the API of OnMouseDown says
OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider.
Your empty GameObject didn't have any those.
You could for example add a BoxCollider to it, though the usability of an invisible button is very questionable. The main issue now might however be that the other cube is in front of this collider so you never actually hit it.
It is blocked by the other cube

Related

cant play an animation when pressing a button

screen shot of the animator panel
so as you can see I am trying to play an animation called Attack when triggering the parameter Attack but whenever I press the triggering button that I chose the animation just doesn't work and the trigger does not get activated
here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class attack : MonoBehaviour
{
public Animator animator;
void Update()
{
if (Input.GetKeyDown(KeyCode.C))
{
Attack();
}
}
void Attack()
{
animator.SetTrigger("Attack");
}
}
by the way, I am trying to follow this guy's video
https://youtu.be/sPiVz1k-fEs
Suggestions:
Make sure you have assigned this script to a game object.
Assign the right animator that you want to animate in inspector of this script.
Check if you have added 'Attack' parameter in that animator and the spelling is same like the one mentioned as parameter in your code.
and lastly check if you have set the trigger for the transition of animation in the animator( in animator its the arrow that is pointing towards your animation ).
Let me know if you don't understand any of this I'll breifly explain.
Hopefully that will help... Happy coding :)

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.

cant make my gameobjects change position after collision [duplicate]

First, I know that this question has been asked a lot, but I cant find a solution, so mi problem is, Im making an educational game, and I have a vein and the blood flow (with many box colliders) and a single blood cell (also with a box collider) however i want the cell to destroy when it reaches the wall collider, but it doesn't it just stays there, here is the project!
http://tinypic.com/r/10706es/9
(cant upload images because of my reputation, sorry)
The collider where I want to destroy my cell is the pink collider, however when it touches it it just does nothing, here's my script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class collision : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void OnCollisionEnter(Collision col)
{
print("hihi");
if (col.gameObject.tag == "Collider")
{
Destroy(gameObject);
}
}
}
Also, here is the AddForce script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AddForce : MonoBehaviour {
public float thrust;
public Rigidbody rb;
private Vector3 up;
private bool move;
void Start()
{
rb = GetComponent<Rigidbody>();
up = new Vector3(0, 1, 0);
move = false;
}
void FixedUpdate()
{
if (Input.GetKey("space"))
{
if (rb.velocity.magnitude < 5)
rb.AddForce(up * thrust);
move = true;
}
else
{
if (move == true)
rb.velocity = new Vector3(0, -0.5F, 0);
}
}
}
thanks for your help guys! :D
It can be several things, whether you are using OnTriggerEnter or OnCollisionEnter:
Missing RigidBody (the most common). At least one of the GameObjects involved needs to have a RigidBody. (check if at least one of them have a RigidBody attached and, if you are using OnCollisionEnter, does not have the "Is Kinematic" checked). See the below collision matrix for more information.
Missing tag. The GameObject from collision does not have a "Collider" tag (try to remove the if statement to test it) (to compare tags, use collider.gameObject.CompareTag("Collider"), it has a better performance)
Undetectable collision. The physics Layer Collision Matrix is set to not detect collision between the layers the objects are (enter Edit > Project > Phisics and check if the encounter of the layer of both GameObjects are checked inside Layer Collision Matrix)
Wrong Collider configuration. one or both of the GameObjects have a small/wrong placed or absent Collider (check if they both have a Collider component and if their size are correct)
If it's working, you should be able to press play and drag one GameObject into the other one and your Debug.Log will appear.
As an advice, use tag names that better describe the group of GameObjects that will be part of it, like "RedCells" or "WhiteCells". It'll be easier to configure the Layer Collision Matrix and improve the performance of your game.
Another advice: for colliders that just destroys another GameObject (don't react, like bump or actually collide) I use triggers. That way, the collision between them will not alter anything in the remaining GameObject (like direction/velocity/etc). To do that, check the Is Trigger in the Collider and use OnTriggerEnter instead of OnCollisionEnter.
Source
some times you added Nav Mesh Agent component to your game object ( to auto route operation in strategic game and ...).
in this case, this game object does not attend to collider.
So, if you really need this Nav Mesh Agent, you should add Nav Mesh Obstacle to other fixed game object and also add Nav Mesh Agent to other movable game object.
I have a few followup questions which might lead to a solution.
First, does the object holding your 'collision' script have a rigidbody and a collider on it?
Second, does the wall have both a rigidbody and collider?
Usually if those conditions are met, then collisions will work.
A couple other things that could be the problem:
Check if you have istrigger checked for either object and make sure it is unchecked.
Check and make sure the rigidbodies on both are non-kinematic.
I've finally fixed it, I dont really know if this was the problem, but i just removed the rigidbody from the parent of the wall and it started working!, I dont know what the rigidbody did, but just with that the problem was fixed, thank you all for your help! :D
Ensure the following things are considered in your code,
All gameobject should contain collider attached and the player
gameobject should contain the rigidbody component in it.
The collider size should change to the width and height of the
component instead of default (1,1) values.

Why won't Unity 2017.3.0f3 recognise Ontrigger2D /OnCollision2D?

I have a 2d box collider on a "Magnet" sprite and a circle collider on a "Ball" sprite. On the Magnet sprite, I turned on "Is Trigger" and tried a few rigidbody settings: Kinematic, Static and Dynamic. The Ball also has a Dynamic rigidbody on it. I'm using Unity 2017.3.0f3.
For the time being I just want to check if an object has entered the Magnets collider and log the object's name.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Magnetism : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log(other.name);
}
}
I also tried using the OnCollisionEnter(Collision other) and it still doesn't do anything. Oddly I have no errors as well. Oh, also in one forum, I found someone mention that using a onCollisionEnter/onTriggerEnter. That didn't work for me either.
Here is the checklist to make sure your collision works
Make sure the layers your gameObjects are in are set to collider under Project Settings -> Layer Collision Matrix.
Make sure at least one of the colliding bodies have a Rigidbody2D (not rigidbody, must be 2D) attached to it
Both of the objects must have a Collider2D
Make sure the Magnet's collider has IsTrigger set to true
The script in which the IsTriggerEnter2D function is present must be on the same object which has the collider2D (Also make sure this collider is set as a trigger)
Make sure the size of the colliders isn't 0 or is big enough. This can happen if you create a 2D sprite first and put the actual sprite image later
Make sure all of the components and gameObjects are enabled/active
I know some of them are trivial but still easy to miss
Edit: One more thing; the rigidbody2D must have simulated set to true.
I've got two objects (magnet with box2D and ball with circle). The magnet object has a script attached that does the same as you. Everything works as it should. (Tested with your version.)
Just try to double check everything and otherwise, try to create a new project and see if it occurs. If that doesn't work, test with 2017.3.1f1. If all that does not work, with I highly doubt, send a bug report to Unity and they'll help you.
Ensure that all colliders and rigidbodies you're using are 2D.
In order for the OnTriggerEnter2D to work, at least one of the colliders must have the Is Trigger active.
To make a quick test, you can disable all rigidbodies and check with only the colliders active.
The script code is perfectly fine and should work.

How do i use invisible walls properly ? Only after the player is walking out the terrain edge for some it's triggering

I created a new empty gameobject in the hierarchy.
Attached to it a Box Collider:
And changed this settings on the Box Collider:
I set it's center property to 0,0,0
And set the size to x = 500 y = 600 z = 500
I also set the IsTrigger to be on( the checkbox is checked ).
And this is the Terrian details:
Width 500 Length 500 Height 600
When i'm looking at the scene window it seems like the box is around the terrain edges as it should be: ( Maybe in the right side there some space between the box and the terrain ? )
Scene Screenshot
This is the script attached to the empty gameobject ( InvisibleWalls ).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoxCollider : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerExit(Collider c)
{
Destroy(c.gameObject);
}
}
This is just for the test.
I added a break point on the line:
Destroy(c.gameObject);
Just to check when the event trigger.
The player ( ThirdPersonController ) is walking at speed 10.
When it's getting to the terrain edge the event is not triggering. The player keep walking on the air for some more seconds and only then the event is trigger and stop on the break point but then the player is already out the terrain area.
Event Triggered
What i want to do is once the player is touching the wall trigger the event and do something for example keep the player walking on place so the wall is blocking him. But now the event is trigger when the player is out of the terrain area.
Well this looks like the result of starting the player inside the collider, and using OnTriggerExit. That method won't be called until all of the player's collider is outside the defined box.
You could make the box smaller, so that there is some "padding" terrain around the outside of it. This is probably the quickest, but I would recommend option 2 instead.
You could make 4 box colliders, one for each wall. Instead of having one gigantic collider and waiting for the player to step out, think of these as skinny walls placed one to each side of your terrain. Don't make them triggers, and use the OnCollisionEnter event instead. This will automatically stop the player if you are moving him via physics, and you get the event as well. You can still make them triggers and use OnTriggerEnter if you want, but why not use the physics system since its there!
I hope this helps, good luck with your project!
Edit: Found a video that explains method 2
1) You Can call OnTriggerEnter if you want the player to be detected before it crosses the Trigger.
2) Another way will be To make the triggers Colliders and they will automatically block the player to cross the terrain boundary

Categories